2

Im using wp_insert_post() to add products programmatically in woocommerce I got everything working except product attributes will not show on the product page view unless I click update from the admin.

Im adding tons of products so is there any way to get variations to show in the dropdown on the product page.

Heres my code it works fine everything gets placed in the correct fields it just wont show options on the product page without clicking update.

$new_post = array(
      'ID' => '',
      'post_author' => 1,           
      'post_title' => 'title of product',
      'post_status' => 'publish',
      'post_type' => 'product'
    );
$post_id = wp_insert_post($new_post);

wp_set_object_terms($post_id, 'variable', 'product_type', false); 

$my_post = array(
  'post_title'    => 'Variation # of ' . esc_attr(strip_tags(  $post_title)),
  'post_name'     => 'product-' . $post_id . '-variation-',
  'post_status'   => 'publish',
  'post_parent'   => $post_id,
  'post_type'     => 'product_variation',
  'guid'          =>  home_url() . '/?product_variation=product-' . $post_id . '-variation-'
);
wp_insert_post( $my_post );
$variable_id = $post_id + 1;

update_post_meta( $variable_id, '_price', 8.50 );
update_post_meta( $variable_id, '_regular_price', '8.50');

 $product_attributes['type'] = array(
            'name' => htmlspecialchars(stripslashes('Options')),
            'value' => "black|blue",
            'position' => 1,
            'is_visible' => 1,
            'is_variation' => 1,
            'is_taxonomy' => 0
    );


update_post_meta( $post_id, '_product_attributes', $product_attributes);    
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Tcmxc
  • 481
  • 1
  • 7
  • 23
  • 1
    Instead of using the old way like before, you should use [**new CRUD methods**](https://github.com/woocommerce/woocommerce/wiki/CRUD-Objects-in-3.0) introduce with Woocommerce 3 like in [**this related thread**](https://stackoverflow.com/a/47766413/3730754) and [**this other one**](https://stackoverflow.com/a/47844054/3730754) too. So When you will use the `save()` method at the end, and everything will be sync and updated everywhere (even the cached data). – LoicTheAztec Jun 22 '18 at 22:51

1 Answers1

4

This code will create product with 2 attributes (weight, brand) and 2 variations.

//Create main product
$product = new WC_Product_Variable();

$att_var = array();
//Create the attribute object with name weight
$attribute = new WC_Product_Attribute();
$attribute->set_id( 0 );
$attribute->set_name( 'weight' );
$attribute->set_options( array(
        '50g',
        '100g',
        '150g'
) );
$attribute->set_position( 0 );
$attribute->set_visible( 1 );
$attribute->set_variation( 1 );
$att_var[] = $attribute;

//Create the attribute object with name brand
$attribute = new WC_Product_Attribute();
$attribute->set_name( 'brand' );
$attribute->set_options( array(
        'Parle-G',
        'Britania'
) );
$attribute->set_position( 1 );
$attribute->set_visible( 1 );
$attribute->set_variation( 1 );
$att_var[] = $attribute;

$product->set_attributes($att_var);
$product->set_name('Product 3');
$product->set_status('publish');
$product->set_sku(12345);

//Save main product to get its id
$product->set_category_ids([47, 56] );
$id = $product->save();

//variation 1
$variation = new WC_Product_Variation();
$variation->set_regular_price(10);
$variation->set_sale_price(10);
$variation->set_stock_quantity(12);
$variation->set_manage_stock(True);
$variation->set_weight('50g');
$variation->set_parent_id($id);

$variation->set_attributes(array(
        'weight' => '50g',
        'brand' => 'Parle-G'
));

//Save variation, returns variation id
$variation->save();

//variation 2
$variation_new = new WC_Product_Variation();
$variation_new->set_regular_price(15);
$variation_new->set_sale_price(12);
$variation_new->set_stock_quantity(20);
$variation_new->set_manage_stock(True);
$variation_new->set_weight('100g');
$variation_new->set_parent_id($id);

//Set attributes requires a key/value containing
$variation_new->set_attributes(array(
        'weight' => '100g',
        'brand' => 'Britania'
));


//Save variation, returns variation id
$variation_new->save();

Product will create like this in woocommerce product section

WhatsThePoint
  • 3,395
  • 8
  • 31
  • 53
Jayesh
  • 161
  • 11
  • 2
    The 2nd $attribute is missing `$attribute->set_id(0);` (0 makes it an individual non-global product attribute). In order to set a global attribute I also had to prefix the name with "pa_". global attribute is called color: `$attribute->set_name('pa_color');` – Ti Hausmann Oct 25 '20 at 08:04