【问题标题】:Creating WooCommerce product variation adds an empty attribute value创建 WooCommerce 产品变体会添加一个空属性值
【发布时间】:2018-05-19 08:31:33
【问题描述】:

向 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 管理员中查看产品时,我的所有变体都在那里,但所有变体的属性都停留在“任何选项”。

奇怪的是,当我从 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"
}

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

【问题讨论】:

  • 好的,我已经更新了我的答案……你可以查看

标签: php wordpress woocommerce custom-taxonomy variations


【解决方案1】:

更新 (与您的更新和 cmets 有关)

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

我做了一些测试:

  1. 我创建了一个新的“Varinfo”属性 (pa_varinfo),它有 4 个值(术语名称):
    104 mm110 mm130 mm140 mm(所以术语 slugs就像104-mm…)
  2. 我创建了一个新的变量产品,其中包含一个空变体(未为此变体定义任何内容)并更改保存选择字段显示:

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

$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();
}

它只是为我工作,我在后端获得了这个变体的选定值:

注意,我在属性中使用 分类名称,在数组中使用 术语 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 中创建了附加属性……,你会得到:

【讨论】:

  • 感谢@LoicTheAztec 的帮助我尝试了该代码并希望同步功能是我所缺少的。唉,我仍然遇到同样的问题,即没有一个变体被分配属性。我得到以下信息:i.imgur.com/TnmTW0I.png
  • @SimonPollard 您的问题不清楚和明确,因为您没有说它在后端……我的回答与前端有关。现在我稍后会更新我的答案(对于后端)......
  • 道歉 - 我已经重写了问题以更清楚并添加了图像 - 感谢您的帮助
  • 我的问题是属性数组中的空白项 - 应该只有 1 条记录 - 不知道它是如何/在哪里添加的
  • 我删除了 0000 属性以防万一 - 仍然没有运气
【解决方案2】:

所以问题出在针对主要父产品的属性的实际设置,我通过一个未命名的数组,通过添加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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-07
    • 1970-01-01
    • 2022-08-19
    • 2019-01-23
    • 2021-06-05
    • 1970-01-01
    • 2018-05-11
    • 1970-01-01
    相关资源
    最近更新 更多