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

Php 创建产品变体会添加一个空属性值,php,wordpress,woocommerce,custom-taxonomy,variations,Php,Wordpress,Woocommerce,Custom Taxonomy,Variations,向WooCommerce产品添加变体时,它是在添加空白属性。这意味着,当我尝试添加自己的属性时,它会附加到现有数组中 我正在运行的示例代码: $product_variation = wc_get_product( $variation_id ); $product_variation->set_attributes(array('attribute_pa_varinfo' => 'blue')); $product_variation->save(); 然后当我var\u

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

我正在运行的示例代码:

$product_variation = wc_get_product( $variation_id );
$product_variation->set_attributes(array('attribute_pa_varinfo' => 'blue'));
$product_variation->save();
然后当我
var\u dump($product\u variation)时我得到以下信息:

["attributes"]=>
array(2) {
  [0]=>
  string(0) ""
  ["pa_varinfo"]=>
  string(4) "5034"
}
$product_variation = wc_get_product( $variation_id );
$product_variation->set_attributes( array ( 'simon' => 'confused' ) );
$product_variation->save();
var_dump($product_variation->get_attributes());
因此,当我在WooCommerce admin中查看产品时,我的所有变体都在那里,但所有变体的属性都停留在“任意选项”上

奇怪的是,当我从wp admin“更新”产品时,所有的变体都被选中了

以前有没有人遇到过这种情况,或者对我能做什么有什么想法

作为另一个示例,如果我运行以下命令:

["attributes"]=>
array(2) {
  [0]=>
  string(0) ""
  ["pa_varinfo"]=>
  string(4) "5034"
}
$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"
}
第一项来自哪里?我似乎无法清除它。

更新(与您的更新和评论相关)

继续(我们的评论):产品属性存在。此外,此属性的所有术语都在父变量产品中定义和设置(在“属性”设置选项卡中)

我做了一些测试:

  • 我创建了一个新的“Varinfo”属性(
    pa_Varinfo
    ),它有4个值(术语名):
    104 mm
    110 mm
    130 mm
    140 mm
    (因此术语段塞类似于
    104 mm
  • 我已经创建了一个新的变量产品,其中包含一个空变量(没有为该变量定义任何内容),并且alter保存select字段显示:
  • 使用此代码时(与您的代码类似):

    它只对我有效,我在后端为该变体获取所选值:

    注意我正在使用分类名称作为属性,并在数组中使用术语SLUG

    所以我不知道你哪里做错了什么


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

    // 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中创建了附加属性…,您将获得:

    更新(与您的更新和评论相关)

    继续(我们的评论):产品属性存在。该属性的所有术语都在父变量产品中定义和设置(在“属性”设置选项卡中)

    我做了一些测试:

  • 我创建了一个新的“Varinfo”属性(
    pa_Varinfo
    ),它有4个值(术语名):
    104 mm
    110 mm
    130 mm
    140 mm
    (因此术语段塞类似于
    104 mm
  • 我已经创建了一个新的变量产品,其中包含一个空变量(没有为该变量定义任何内容),并且alter保存select字段显示:
  • 使用此代码时(与您的代码类似):

    它只对我有效,我在后端为该变体获取所选值:

    注意我正在使用分类名称作为属性,并在数组中使用术语SLUG

    所以我不知道你哪里做错了什么


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

    // 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中创建了附加属性…,您将获得:


    问题是,由于实际设置了主父产品的属性,我通过了一个未命名的数组,通过添加< >代码> WCQAtEdTyTyTraceNyYyNoLy('ValnFiel')= > >代码>(下面的第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);
    

    问题是,由于实际设置了主父产品的属性,我正在通过一个未命名的数组,通过添加< >代码> WCQAtEdTyTyTraceNyYyNoLy('ValnFiel')= > >代码>(下面的第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);
    

    感谢@LoicTheAztec的帮助,我尝试了这段代码,希望同步功能是我所缺少的。唉,我仍然被同样的问题困扰着,没有一个变体被赋予属性。我得到以下信息:@SimonPollard您的问题不清楚,因为您没有告诉我它在后端…我的答案与前端相关。现在我将稍后更新我的答案(对于后端)抱歉-我已经把问题写得更清楚并添加了图像-感谢你的帮助我的问题是属性数组中的空白项-应该只有1条记录-不知道它是如何/在哪里得到ADDEI只是删除了0000个属性以防万一-仍然没有感谢的帮助@ LoigTeaTeTEC我尝试了那个代码并希望同步功能是我所缺少的。唉,我仍然被同样的问题困扰着,没有一个变体被赋予属性。我得到以下信息:@SimonPollard您的问题不清楚,因为您没有告诉我它在后端…我的答案与前端相关。现在我将稍后更新我的答案(对于后端)抱歉-我已经把问题写得更清楚,添加了图像-感谢你的帮助我的问题是属性数组中的空白条目-应该只有1条记录-不知道它是如何在何处得到ADDII删除0000属性只是在情况下-仍然没有运气我已经更新了我的答案…你可以检查我已经更新我的回答……你可以查一下