wordpress woocommerce:为产品属性增加价值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19518878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:24:02  来源:igfitidea点击:

woocommerce: add value to a product attribute

wordpresswoocommerce

提问by Rao

How do I add value to a woocommerce attribute through code? I have created an attribute called 'Dispatch time' (taxonomy: pa_dispatch) and now I want to add value to a particular product's Dispatch attribute.

如何通过代码为 woocommerce 属性添加值?我创建了一个名为“调度时间”(分类:pa_dispatch)的属性,现在我想为特定产品的调度属性添加值。

How to do this programmatically?

如何以编程方式执行此操作?

enter image description here

在此处输入图片说明

回答by Rao

I found the answer, you need to use wp_set_object_termsto set the terms of object of a taxonomy,

我找到了答案,你需要使用wp_set_object_terms来设置分类对象的术语,

wp_set_object_terms( $object_id, $terms, $taxonomy, $append);

Where, $append can be trueor false, if true, the tag will be appended to existing tag, if false, the tag is replaced.

其中, $append 可以是trueor false,如果为真,标签将被附加到现有标签,如果为假,标签将被替换。

In my example,

在我的例子中,

wp_set_object_terms( $object_id, '2 Business Days', 'pa_dispatch' , false);

Here, the pa_dispatchis the woo-commerce taxonomy.

在这里,这pa_dispatch是 woo-commerce 分类法。

回答by Sameer Joshi

You cannot add value to an attribute. You need to make the product variable, create a variation, and assign it with the attribute. Now in this variation you can assign a value.

您不能为属性添加值。您需要使产品变量,创建变体,并为其分配属性。现在在这个变体中,您可以分配一个值。

Attributes Configuration

属性配置

Variations Configuration

变体配置

Read mode:

阅读模式:

  1. http://docs.woothemes.com/document/product-variations/
  2. http://www.youtube.com/watch?v=7PX8MWBOAeo
  1. http://docs.woothemes.com/document/product-variations/
  2. http://www.youtube.com/watch?v=7PX8MWBOAeo

EDIT:

编辑:

After more clarification on the question, here is an updated solution.

在对这个问题进行更多澄清之后,这里是一个更新的解决方案。

Add the below function to your functions.php. Call it on the appropriate hook and pass the product ID as well as the attribute values.

将以下函数添加到您的functions.php。在适当的挂钩上调用它并传递产品 ID 和属性值。

function se19519561_set_attributes($post_id, $attributes) {

    //Type attribute
    $product_attributes['type'] = array(
        //Make sure the 'name' is same as you have the attribute
        'name' => htmlspecialchars(stripslashes('Dispatch Time')),
        'value' => $attributes,
        'position' => 1,
        'is_visible' => 1,
        'is_variation' => 1,
        'is_taxonomy' => 0
    );

//Add as post meta
update_post_meta($post_id, '_product_attributes', $product_attributes);

}

Hope this helps!

希望这可以帮助!