0

I can insert variant options/attributes with no default values(variant name) in order to display following Variant options area with selected attribute names(Renk,Beden) on the product, enter image description here

by using following code,

$productID = 3131;
update_post_meta($productID, '_product_attributes', [
    'pa_renk' => [
      'name' => 'pa_renk',
      'value' => '',
      'position' => 0,
      'is_visible' => 1,
      'is_variation' => 1,
      'is_taxonomy' => 1,
    ],
    'pa_beden' => [
      'name' => 'pa_beden',
      'value' => '',
      'position' => 1,
      'is_visible' => 1,
      'is_variation' => 1,
      'is_taxonomy' => 1,
    ]
  ]);

But I would like to insert them their selected values in order to achieve this result, enter image description here

Because, I need to have this default variant values already there in order to be able to create variations on the product with following code,

$productID = 3131;
$variation = new WC_Product_Variation();
  $variation->set_parent_id( $productID );
  $variation->set_sku( 'test' . rand(0, 1000) );
  $variation->set_attributes( array(  'pa_renk' => 'beyaz', 'pa_beden' => 50 ) );

  $variation->set_regular_price( '100' );
  $variation->set_sale_price( '42' );
  $variation->set_price( '42' );

  $variation->save();

Because if there is no default variant options/attributes value selected, I cannot be able to create product variations.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.