使用 WooCommerce 中变体的属性创建一个新的可变产品

问题描述 投票:0回答:1

属性 pa_at-cor 和 pa_at-tamanho 已经存在(我在前端检查过)我正在创建一个新的可变产品,我想将这些属性视为它,但我不能,我看到了很多帖子关于这一点,但他们最关心的是创建一个新属性或只是创建一个已创建的具有属性的变量产品的变体,我尝试使用 update_post_meta 关于属性数据,我尝试了很多代码组合,甚至从分类中删除“pa”,但我没成功

function go_cadastro_vo_products(){
    $product = new WC_Product_Variable();
    $product->set_name( 'Variable Product Example 3' );
    $product->set_description( 'This is a variable product example.' );
    $product->set_status( 'publish' );
    $product->set_catalog_visibility( 'visible' );

    // Set custom attributes
    $attributes = array(
        'pa_at-cor' => array(
            'name'         => 'pa_at-cor',
            'value'        => '',
            'position'     => 1,
            'visible'   => 1,
            'variation' => 1,
            'taxonomy'  => 1,
        ),
        'pa_at-tamanho' => array(
            'name'         => 'pa_at-tamanho',
            'value'        => '',
            'position'     => 2,
            'visible'   => 1,
            'variation' => 1,
            'taxonomy'  => 1,
        ),
    );

    $product->set_attributes($attributes);

    // Save the product
    $product_id = $product->save();

    // Set attribute values using wp_set_object_terms
    wp_set_object_terms($product_id, 'Azul', 'pa_at-cor');
    wp_set_object_terms($product_id, 'GG', 'pa_at-tamanho');


}

php woocommerce methods attributes product
1个回答
0
投票

以下内容将根据您的产品属性数据进行操作,假设两个产品属性均已存在并且每个关联的术语均已存在。

对于您的产品属性,最好直接设置术语 ID,而不是从术语名称中获取术语 Id...

尝试以下经过测试且有效的代码:

// Define your product attributes: Array of taxonomy => term names (array) pairs
$attr_data  = array(
    'pa_size'  => array('Blue', 'Gray'),
    'pa_color' => array('Large', 'Small'),
);

// Get an empty instance of the WC_Product_Variable Object
$product = new WC_Product_Variable();

$product->set_name( 'Variable Product 1' );
$product->set_description( 'This the variable product description…' );
$product->set_status('publish');
$product->set_stock_status('instock');
$product->set_catalog_visibility('visible');

$attributes = array(); // Initializing
$attr_count = 0; // Initializing

// Loop through product attribute data
foreach ($attr_data as $taxonomy => $term_names ) {
    $options  = array(); // Initializing
    $position = $attr_count;
    $attr_count++;

    // Get an empty instance of the WC_Product_Attribute Object
    $attribute = new WC_Product_Attribute(); 
    $attribute->set_id($attr_count);
    $attribute->set_name($taxonomy);
    $attribute->set_position($position);
    $attribute->set_visible(1);
    $attribute->set_variation(1);

    // Loop through product attribute term names
    foreach ( $term_names as $term_name ) {
        // Set the term ID to the options array
        $options[] = get_term_by('name', $term_name, $taxonomy)->term_id;
    }
    $attribute->set_options($options); // Set the attribute options (term IDs array)

    $attributes[$taxonomy] = $attribute; // Set the WC_Product_Attribute Object to the attributes array
}
$product->set_attributes($attributes); // Set the product attributes for variations

$product_id = $product->save(); // Sync and save the product data to the database
© www.soinside.com 2019 - 2024. All rights reserved.