首页 文章

创建WooCommerce产品变体会添加一个空属性值

提问于
浏览
1

向WooCommerce产品添加变体时,它会添加一个空白属性 . 这意味着当我尝试添加我自己的属性时,它会被附加到现有数组 .

我正在运行的示例代码:

$product_variation = wc_get_product( $variation_id );
$product_variation->set_attributes(array('attribute_pa_varinfo' => 'blue'));
$product_variation->save();

然后当我 var_dump($product_variation); 我得到以下内容:

["attributes"]=>
array(2) {
  [0]=>
  string(0) ""
  ["pa_varinfo"]=>
  string(4) "5034"
}

因此,当我在WooCommerce管理员中查看该产品时,我的所有变体都存在,但属性仍然停留在所有这些属性的“任何选项” .

no option selected

奇怪的是,当我从wp-admin“更新”产品的所有变体然后选择正确的属性 .

有没有人遇到过这个问题,或者对我能做什么有任何想法?

作为另一个例子,如果我运行以下:

$product_variation = wc_get_product( $variation_id );
$product_variation->set_attributes( array ( 'simon' => 'confused' ) );
$product_variation->save();
var_dump($product_variation->get_attributes());

返回:

array(2) {
 [0]=> string(0) ""
 ["simon"]=> string(8) "confused"
}

第一个项目来自哪里?我似乎无法清除它 .

2 回答

  • 1

    Update (与您的更新和评论有关)

    要恢复(我们的评论):产品属性存在 . 此属性的所有术语都在父变量产品中定义和设置(在“属性”设置选项卡中)

    我有女佣一些测试:

    • 我创建了一个带有4个值(术语名称)的新“Varinfo”属性( pa_varinfo ):
      104 mm110 mm130 mm140 mm (因此术语slug就像 104-mm ......) .

    • 我创建了一个新的变量产品,其中包含一个空变量(没有为此变体定义)和更改保存选择字段显示:

    enter image description here

    使用此代码时(与您的代码类似):

    $parent_product = wc_get_product( 738 ); // Get the variable product
    $variation_ids = $parent_product->get_children(); // Get all children variations (Here only one)
    
    // Iterating through each variation
    foreach( $variation_ids as $variation_id ){
        $variation = wc_get_product($variation_id);
        $variation->set_attributes(array('pa_varinfo' => '104-mm'));
        $variation->save();
    }
    

    它只是为我工作,我得到后端的选定值这个变化:

    Note 我正在使用 taxonomy name 属性和数组中的 term SLUG ...

    所以我不知道 where you are doing something wrong ......


    当您设置不存在和/或未注册为父变量产品的后期的属性项时,会发生这种情况 . 你可以试试这个:

    // Get an instance of the WC_Product_Variation object
    $variation = wc_get_product( $variation_id );
    
    // Initialising variables
    $taxonomy = 'pa_varinfo'; // The taxonomy
    $term_name = 'Blue'; // The term "NAME"
    
    // Check if the term exist and if not we create it.
    if( ! term_exists( $term_name, $taxonomy ) )
        wp_insert_term( $term_name, $taxonomy );
    
    // Get an instance of the WP_Term object
    $term = get_term_by( 'name', $term_name, $taxonomy );
    
    // Get the post terms names from the parent variable product.
    $post_term_names =  wp_get_post_terms( $variation->get_parent_id(), $taxonomy, array('fields' => 'names') );
    
    // Check if the post term exist and if not we set it in the parent variable product.
    if( ! in_array( $term_name, $post_term_names ) )
        wp_set_post_terms( $variation->get_parent_id(), $term_name, $taxonomy, true );
    
    // Now you can set the term for the attribute in your variation
    $variation->set_attributes( array( $taxonomy => $term->slug ) );
    $variation->save(); // Registering the data
    
    // Get an instance of the parent WC_Product_Variable object
    $parent_product = wc_get_product( $variation->get_parent_id() );
    
    // Sync the data of the variation in the parent variable product
    $parent_product->sync( $variation_id );
    

    这是经过测试和运作的

    假设您已经在WooCommerce中创建了附加属性...,您将获得:

    enter image description here

  • 1

    所以事实证明问题在于对主要父产品的实际设置属性,我通过添加 wc_attribute_taxonomy_name('varinfo') => (下面的第2行)传递一个未命名的数组,这正确地保存数据并删除我有的空白数组 .

    $product_attributes = array(
          wc_attribute_taxonomy_name('varinfo') => 
          array (
            'name'         => wc_attribute_taxonomy_name( 'varinfo' ), // set attribute name
            'value'        => '', // set attribute values
            'position'     => 1,
            'is_visible'   => 1,
            'is_variation' => 1,
            'is_taxonomy'  => 1
          )
        );
    
        update_post_meta($post_id, '_product_attributes', $product_attributes);
    

相关问题