根据类别woocommerce更改同一产品的默认变化值 [英] Change default variation value for the same product according to the category woocommerce
问题描述
我正在努力根据他的类别显示SAME产品的默认变化值. 例如,我出售的卡带有选项blue&红色的. 当用户来自类别ONE时,我希望默认值为蓝色. 如果他属于"TWO"类别,则该值将为红色.
I'm working on a way to display default variations value for the SAME product according to the category he is. For example, I sell a card with option blue & red. When the user comes by the category ONE, I want the default value is blue. If he comes by the category TWO, the value will be red.
我找到了一个钩子woocommerce_product_default_attributes
,但是我不知道如何使用它.
I found a hook woocommerce_product_default_attributes
, but I don't know how to use it .
注意::即使您的产品属于两类,woocommerce似乎也只能识别一种产品.
Note : It seems that woocommerce recognize only one category per product even if your product is in two category.
示例 (编辑):
我有一个产品 P
.
产品 P
分为两类:Cat 1
& Cat 2
.
此外,产品 P
具有两个变量: Blue
& Red
I have a product P
.
Product P
is in two categories : Cat 1
& Cat 2
.
Also, product P
has two variables : Blue
& Red
当用户来自 Cat 1
时,我希望默认值为 Blue
.
如果他来自 Cat 2
,则值为 Red
.
When the user comes by Cat 1
, I want the default value is Blue
.
If he comes by Cat 2
, the value will be Red
.
@LoicTheAztech的答案代码 (如下)有效,但:
当我转到
Cat 1
或Cat 2
时,可以看到对于Woocommerce,该产品仅位于Cat 1
中,即使我们可以同时访问这两个类别.
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问题.
So before everything, I need to solve the woocommerce issue.
推荐答案
新的不同的 此处更新了答案 ,其中带有替换过滤器挂钩
New different updated answer HERE with the replacement filter hook
在WooCommerce 3+中,过滤器挂钩
woocommerce_product_default_attributes
位于get_variation_default_attributes()
不推荐使用的方法中,因此它并不是正确的挂钩实现您想要的.
In WooCommerce 3+ the filter hook
woocommerce_product_default_attributes
is located inget_variation_default_attributes()
deprecated method, so it's not really the right hook to achieve what you want.
get_variation_default_attributes()
方法替换为 get_default_attributes()
.
get_variation_default_attributes()
method is replaced by get_default_attributes()
.
例如,您可以在 woocommerce_before_add_to_cart_form
操作挂钩中实现条件功能.
You can achieve your conditional function in woocommerce_before_add_to_cart_form
action hook, for example.
注释:
- 产品属性分类始终以'pa_'+属性段标记 开头
- 您需要在变量"标签设置中为此变量产品设置此属性的默认值.
- Product Attribute taxonomy always begin by 'pa_' + the attribute slug
- You need to set in for variable products the default value for this attribute in the variation tab settings.
代码:
add_action( 'woocommerce_before_add_to_cart_form', function(){
global $product;
// We EXIT if it's not a variable product
if( ! $product->is_type('variable') ) return;
## DEFINE HERE the desired product attribute taxonomy
$pa_attribute = 'pa_color';
$default_attribute_for_variation = $product->get_variation_default_attribute( $pa_attribute );
// We EXIT if Product Attribute Color is not set as variation usage
if( empty( $default_attribute_for_variation ) ) return;
// Get the array of default attributes
$default_attributes = $product->get_default_attributes();
// For product category 'ONE => Attribute "blue" slug value
if( has_term( 'clothing', 'product_cat', $product->get_id() ) )
$default_attributes[$pa_attribute] = 'blue';
// For product category 'TWO' => Attribute "blue" slug value
elseif( has_term( 'TWO', 'product_cat', $product->get_id() ) )
$default_attributes[$pa_attribute] = 'red';
else return; // If no product categories match we exit
// If a product category match we set the default attribute
$product->set_default_attributes( $default_attributes );
}, 80, 0 );
代码会出现在您活动的子主题(或主题)的function.php文件中,也可能会出现在任何插件文件中.
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
此代码已经过测试并且可以正常工作.
This code is tested and works.
这篇关于根据类别woocommerce更改同一产品的默认变化值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!