2013-10-22 161 views
12

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

如何做到这一点编程?

enter image description here

回答

14

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

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

其中,$追加可truefalse,如果属实,该标签将被追加到现有标签,如果为false,则替换标签。

在我的例子,

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

这里,pa_dispatch是宇商贸分类。

+1

谢谢。在你的functions.php中,你把这个放在哪里?什么是$ object_id? – nicmare

+0

@nicmare把它放在你想要改变值的地方,但是如果你直接把这个语句放在functions.php中,那么每次加载任何文件时都会运行这个函数!另外,$ object_id只是产品的id,即。,该特定产品的帖子ID。 – Rao

+0

当然,我会用save_post挂钩来解雇它。我会试一试。谢谢 – nicmare

2

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

Attributes Configuration

Variations Configuration

读取模式:

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

编辑:

更澄清的问题后,这里是一个更新的解决方案。

添加下面的函数,你的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); 

} 

希望这有助于!

+0

不,这不是我想要实现的,我可以通过GUI添加值,但我想在php中以编程方式执行此操作。 – Rao

+0

即,我想通过php函数将'3个工作日'的值添加到特定产品的'Dispatch time'属性中。 – Rao

+1

您是否想要在访问产品页面时即时添加属性,或者是否希望一次将其添加到所有产品? –

相关问题