php-根据woocommerce类别更改同一产品的默认变化值
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php-根据woocommerce类别更改同一产品的默认变化值,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3071字,纯文字阅读大概需要5分钟。
内容图文
我正在努力根据他的类别显示SAME产品的默认变化值.
例如,我出售的卡带有选项blue&红色.
当用户来自类别ONE时,我希望默认值为蓝色.
如果他来自两个类别,则该值将为红色.
我找到了一个woocommerce_product_default_attributes钩子,但是我不知道如何使用它.
注意:似乎woocommerce每个产品只能识别一个类别,即使您的产品属于两个类别.
示例(编辑):
我有一个产品P.
产品P分为两类:类别1和类别1.猫2.
而且,产品P具有两个变量:蓝色&红色
当用户来自Cat 1时,我希望默认值为Blue.
如果他来自Cat 2,则该值为Red.
@LoicTheAztech的答案代码(如下)有效,但是:
When I go to
Cat 1
orCat 2
, I can see that for Woocommerce, the product is only inCat 1
, even if we can access by both category.
因此,在一切之前,我需要解决woocommerce问题.
解决方法:
从Woocommerce 3开始,您可以使用woocommerce_product_get_default_attributes筛选器挂钩…因此,您的2个产品类别的代码将如下所示:
add_filter( 'woocommerce_product_get_default_attributes', 'filtering_product_get_default_attributes', 10, 2 );
function filtering_product_get_default_attributes( $default_attributes, $product ){
// We EXIT if it's not a variable product
if( ! $product->is_type('variable') ) return $default_attributes;
## --- YOUR SETTINGS (below) --- ##
// The desired product attribute taxonomy (always start with "pa_")
$taxonomy = 'pa_color';
$category1 = 'one' // The 1st product category (can be an ID, a slug or a name)
$value1 = 'blue' // The corresponding desired attribute slug value for $category1
$category2 = 'two' // The 2nd product category (can be an ID, a slug or a name)
$value2 = 'red' // The corresponding desired attribute slug value for $category2
## --- The code --- ##
// Get the default attribute used for variations for the defined taxonomy
$default_attribute = $product->get_variation_default_attribute( $taxonomy );
// We EXIT if define Product Attribute is not set for variation usage
if( empty( $default_attribute ) ) return $default_attributes;
// For product category slug 'one' => attribute slug value "blue"
if( has_term( 'one', 'product_cat', $product->get_id() ) )
$default_attributes[$taxonomy] = 'blue';
// For product category slug 'two' => attribute slug value "red"
elseif( has_term( 'two', 'product_cat', $product->get_id() ) )
$default_attributes[$taxonomy] = 'red';
else return $default_attributes; // If no product categories match we exit
return $default_attributes; // Always return the values in a filter hook
}
代码进入您的活动子主题(或活动主题)的function.php文件中.尚未测试,但应该可以.
说明:
Since Woocommerce 3 and new introduced CRUDS setter methods, when a method is using
get_prop()
WC_Data
method, a dynamic hook based on the object type and the method name can be used (essentially for frontend: “view” context)…
参见this line $value = apply_filters($this-> get_hook_prefix().$prop,$value,$this);在
和this line返回’woocommerce_’. $this-> object_type. ‘_得到_’;在
因此,以这种方式动态制作了过滤器挂钩(其中$this-> object_type等于’product’):
'woocommerce_' . $this->object_type . '_get_' . 'default_attributes'
有2个参数:
> $attributes(替换$value的属性值)
> $product(替换$this的类实例对象)…
在Woocommerce Github closed and solved issue上看到
内容总结
以上是互联网集市为您收集整理的php-根据woocommerce类别更改同一产品的默认变化值全部内容,希望文章能够帮你解决php-根据woocommerce类别更改同一产品的默认变化值所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。