Skip to content

Instantly share code, notes, and snippets.

@jbrinley
Last active August 18, 2020 20:05
Show Gist options
  • Select an option

  • Save jbrinley/2aa1b60ce9af449b4a7fc69858ab230d to your computer and use it in GitHub Desktop.

Select an option

Save jbrinley/2aa1b60ce9af449b4a7fc69858ab230d to your computer and use it in GitHub Desktop.

Revisions

  1. jbrinley revised this gist Aug 18, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    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:
    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
  2. jbrinley revised this gist Aug 18, 2020. 1 changed file with 1 addition and 3 deletions.
    4 changes: 1 addition & 3 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,4 @@
    Run this from WP CLI to generate sample item code data for all local products.

    Usage:
    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
  3. jbrinley revised this gist Aug 18, 2020. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    Run this from WP CLI to generate sample item code data for all local products.

    Usage:

    ```
    dev/docker/wp.sh shell
    localdev_populate_itemcodes();
    exit();
    ```
  4. jbrinley created this gist Aug 18, 2020.
    95 changes: 95 additions & 0 deletions catalog-data-sample.local.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,95 @@
    <?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' );
    }
    }