Skip to content

Instantly share code, notes, and snippets.

@jbrinley
Last active August 18, 2020 20:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbrinley/2aa1b60ce9af449b4a7fc69858ab230d to your computer and use it in GitHub Desktop.
Save jbrinley/2aa1b60ce9af449b4a7fc69858ab230d to your computer and use it in GitHub Desktop.
Run this from a WP CLI shell to generate sample item code data for all local products.

Run this from WP CLI to generate sample item code data for all local products. Drop the file in your mu-plugins directory, then run:

dev/docker/wp.sh shell
localdev_populate_itemcodes();
exit();
<?php
use Tribe\Project\Post_Meta\Catalog_Integration;
use Tribe\Project\Post_Types\Product\Product;
use Tribe\Project\Taxonomies\Item_Code\Item_Code;
use Tribe\Project\Taxonomies\Spec_Field\Spec_Field;
use Tribe\Project\Taxonomies\Warranty\Warranty;
use Tribe\Project\Taxonomy_Meta\Spec_Field_Attributes;
function localdev_populate_itemcodes() {
$fields = [
'height' => 'Height',
'width' => 'Width',
'depth' => 'Depth',
'seatheight' => 'Seat Height',
'weight' => 'Product Weight',
'steelcasestyle' => 'Steelcase Style',
'manufacturerstyle' => 'Manufacturer Style',
'leadtime' => 'Lead Time',
'warranty' => 'Warranty',
'material' => 'Material',
'certifications' => 'Certifications',
'countryavailability' => 'Country Availability',
];
foreach ( $fields as $key => $label ) {
if ( ! term_exists( $key, Spec_Field::NAME ) ) {
$term = wp_insert_term( $label, Spec_Field::NAME, [
'slug' => $key,
] );
update_term_meta( $term['term_id'], Spec_Field_Attributes::GROUPING, Spec_Field_Attributes::INFO );
}
}
$products_without_item_codes = get_posts( [
'fields' => 'ids',
'posts_per_page' => - 1,
'post_type' => Product::NAME,
'post_status' => 'publish',
'tax_query' => [
[
'taxonomy' => Item_Code::NAME,
'operator' => 'NOT EXISTS',
],
],
] );
foreach ( [ 'Steelcase Standard', 'Manufacturer' ] as $warranty ) {
if ( ! term_exists( $warranty, Warranty::NAME ) ) {
wp_insert_term( $warranty, Warranty::NAME );
}
}
foreach ( $products_without_item_codes as $post_id ) {
$title = get_the_title( $post_id );
\WP_CLI::log( 'Generating item code for ' . $title );
$item_code = preg_replace( '/\W/', '_', strtoupper( $title ) );
$term_data = term_exists( $item_code, Item_Code::NAME );
if ( $term_data ) {
\WP_CLI::warning( sprintf( 'Item code %s already exists. Skipping...', $item_code ) );
continue;
}
$term_data = wp_insert_term( $item_code, Item_Code::NAME );
$item_code_id = (int) $term_data['term_id'];
if ( ! $item_code_id ) {
\WP_CLI::warning( sprintf( 'Unable to create item code %s. Skipping...', $item_code ) );
continue;
}
wp_set_object_terms( $post_id, [ $item_code_id ], Item_Code::NAME );
$specs = [
'depth' => [ sprintf( '%d in', random_int( 10, 40 ) ) ],
'weight' => [ sprintf( '%d lb', random_int( 10, 40 ) ) ],
'width' => [ sprintf( '%d in', random_int( 10, 40 ) ) ],
'warranty' => [ [ 'Steelcase Standard', 'Manufacturer' ][ random_int( 0, 1 ) ] ],
'material' => [ [ 'Plastic', 'Fabric' ][ random_int( 0, 1 ) ] ],
'manufacturerstyle' => [ $item_code ],
'certifications' => [ [ 'BIFMA - Yes', 'SCS Indoor Advantage Gold' ][ random_int( 0, 1 ) ] ],
'countryavailability' => [ [ 'USA', 'CAN', 'MEX' ][ random_int( 0, 2 ) ] ],
'color' => [ [ 'Red', 'Green', 'Blue' ][ random_int( 0, 2 ) ] ],
'style' => [ [ 'Modern', 'Classic', 'Victorian' ][ random_int( 0, 2 ) ] ],
'leadtime' => [ sprintf( '%d weeks delivered', random_int( 2, 10 ) ) ],
];
update_term_meta( $item_code_id, Catalog_Integration::SPECS, $specs );
$list_price = random_int( 800, 1500 );
$cost_price = random_int( 200, $list_price );
$price_label = [ 'Retail', 'List' ][ random_int( 0, 1 ) ];
update_term_meta( $item_code_id, Catalog_Integration::LIST_PRICE, ( (string) $list_price ) . '.00' );
update_term_meta( $item_code_id, Catalog_Integration::LIST_PRICE_LABEL, $price_label );
update_term_meta( $item_code_id, Catalog_Integration::COST_PRICE, ( (string) $cost_price ) . '.00' );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment