属性pa_at-cor和pa_at-Tamanho已经存在(我在前端判断了它)我正在创建一个新的变量产品,我想将这些属性考虑到它,但我不能,我看到了很多关于这一点的帖子,但他们最关注的是创建一个新属性或只是创建一个已创建的带有属性的变量产品的变体,我try 使用UPDATE_POST_META来处理属性数据,我try 了这么多组合代码,甚至从分类中删除了"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');


}

推荐答案

下面将基于您的产品属性数据,假设这两个产品属性都已经存在,并且每个属性都有关联的术语.

您可以使用术语名称,术语slugs或术语Id,因为wp_set_object_terms() WordPress function可以处理其中任何一个.

try 以下经过测试且工作正常的代码:

// Define your product attributes: Array of taxonomiy => term names (array) pairs
$attr_data  = array(
    'pa_at-cor'     => array('Azul'),
    'pa_at-tamanho' => array('GG'),
);

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

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

$attributes = array(); // Initializing
$position   = $attr_id = 0; // Initializing

// Loop through product attribute data
foreach ($attr_data as $taxonomy => $terms ) {
    $attr_id++;
    // Get an empty instance of the WC_Product_Attribute Object
    $attribute = new WC_Product_Attribute(); 
    $attribute->set_id($attr_id);
    $attribute->set_name($taxonomy);
    $attribute->set_position($position);
    $attribute->set_visible(1);
    $attribute->set_variation(1);
    $attributes[$taxonomy] = $attribute;
    $position++;
}
$product->set_attributes($attributes);
$product_id = $product->save(); // Save product that returns the product ID

// Then Loop again through the defined product attribute data (2nd loop)
foreach ($attr_data as $taxonomy => $terms ) {
    // For each attribute, assign the desired terms to the product
    wp_set_object_terms( $product_id, $terms, $taxonomy, true );
}

我已经用相关的产品属性数据测试了代码:

$attr_data  = array(
    'pa_size'  => array('Blue', 'Gray'),
    'pa_color' => array('Large', 'Small'),
);

从现有产品属性和相关术语中:

enter image description here

我成功地创建了一个变量产品,其产品属性适用于各种变化:

enter image description here

Php相关问答推荐

如何使用查询范围在Statamic中按组过滤用户?

CodeIgniter AJAX表格提交中的CSRF token 再生问题

PHP FFI—将PHP数组转换为C指针数组

从产品中获取WooCommerce产品属性单选按钮的列表WP_Query

管理员订单列表中的自定义列与WooCommerce 8.6.1一起消失

WooCommerce:在 checkout 审核单和发票中显示定制产品数据

在PHP中读取JSON

如何在微软图形API中使用用户S访问令牌或基于租户ID的访问令牌?

PHP:使用FORT从数据库获取USERS表到AUTH表

表单提交返回空白页

curl_close()未从PHP 8开始写入CURLOPT_COOKIEJAR会话文件

在 Woocommerce 邮箱订单中显示产品 GTIN

Laravel 在数据库上显示加密列

如何在 Laravel 中app文件夹之外的文件夹内创建文件路径?

使用 Composer 找不到 Google APIClient 类

Laravel Eloquent(where 子句)

循环中未序列化数组显示的问题

PHP 自动函数参数和变量

使用 splat 运算符时按引用传递 (...)

如何为多个表的id列设置不同的值