Как программно создать вариативный товар и добавить в него вариации товаров - в WooCommerce?

Пытаюсь создать вариативный товар и добавить его вариации:

// создаю новый вариативный товар:
$this->_product = new WC_Product_Variable();

$this->_product->set_name( (string) $this->offer->name );
$this->_product->set_status('publish');
$this->_product->set_featured(true); 
$this->_product->set_description( (string) $this->offer->description );
$this->_product->set_sku( (string) $this->offer->vendorCode );
$this->_product->set_price( (int) $this->offer->price );
$this->_product->set_regular_price( (int) $this->offer->price ); 
$this->_product->set_manage_stock(true);
$this->_product->set_stock_status('instock');
$this->_product->set_backorders('notify');
$this->_product->set_sold_individually(false); 

$this->product_id = $this->_product->save(); 

// задаю атрибуты:
foreach ($atts as $_name => $_value) {

    $attribute_id = wc_create_attribute(array(
        'name' => $_name,
    ));

    $_attribute  = new WC_Product_Attribute();
    $_attribute->set_name( $_name );
    $_attribute->set_options( [ $_value ] );
    $_attribute->set_visible( true );
    $_attributes[$_name] = $_attribute; 

}

$this->_product->set_attributes( array_values( $_attributes ) );
$this->_product->save();


// создаю вариации товара:
foreach ($variations as $_variation) {
    $this->_variation = new WC_Product_Variation();
    $this->_variation->set_regular_price((int) $this->offer->price);
    $this->_variation->set_parent_id($this->parent_product_id);
    $this->_variation->set_manage_stock(true);
    $this->_variation->set_stock_quantity( (int) $this->offer->quantity ); 
    $this->_variation->set_stock_status('instock');
    $this->_variation->set_backorders('notify');
    $this->_variation->set_sold_individually(false); 

    // задаю вариативный атрибут:
    $attribute = new WC_Product_Attribute();
    $attribute->set_id( 0 );
    $attribute->set_name('Размер');
    $attribute->set_options( array(
       (string) $this->offer->size,
    ) );
    $attribute->set_position( 0 );
    $attribute->set_visible( 1 );
    $attribute->set_variation( 1 );

    // сохряню изменения в вариативном товаре
    $this->_variation->set_attributes(array($attribute));
    $this->variation_id = $this->_variation->save();
}

вариации почему-то не добавляются:

  1. Для вариации мы только вариативный атрибут задаем?
  2. Для основного товара нужно задавать вариативный атрибут?
  3. В $attribute->set_name('Размер'); я пишу кириллицей - так можно? Мне же не нужно чтобы на сайте этот атрибут назывался: "razmer"

Ответы (0 шт):