我有一家带有父类别和子类别的Woocommerce商店。
例如,父类别为Menswear,其两个子类别为Shirts和Pants。
我想更改“男装”父类别中所有产品的所有单个产品页面上的“添加到购物车”按钮文本。也就是说,“衬衫”和“裤子”子类别中的所有产品。
以下代码实现了此目的,但是必须有一种更好的方法,即在if语句中使用父类别,而不是两个子类别。
不幸的是,我不知道该怎么做,任何人都可以帮忙吗?
add_filter( 'woocommerce_product_single_add_to_cart_text', 'product_cat_single_add_to_cart_button_text', 20, 1 );
function product_cat_single_add_to_cart_button_text( $text ) {
if( has_term( array('shirts','pants'), 'product_cat') )
$text = __( 'Buy Menswear', 'woocommerce' );
return $text;
}
只是澄清一下:
我希望找到的解决方案是使用数组中的父类别(男装),而不使用子类别的衬衫和裤子。在我的网站中,我只有四个父类别,但是每个类别都有几十个子类别。我一直在寻找一种避免在阵列中列出数十个子猫而仅使用四个父类的解决方案。是否可以根据父类别更改添加到购物车的文字?(也就是说,该更改将发生在属于该父级的子类别中的所有产品)。
进一步澄清:
假设我有四个父类别:红色,黄色,绿色和蓝色。
这些父类别中的每一个都有多个子类别,如下所示:
RED
Red subcat 1
Red subcat 2
YELLOW
Yellow subcat 1
Yellow subcat 2
GREEN
Green subcat 1
Green subcat 2
BLUE
Blue subcat 1
Blue subcat 2
我希望Red subcat 1和Red subcat 2中的产品具有添加到购物车中的文字BUY RED。
我希望“黄色”子类别1和“黄色”子类别2中的产品具有添加到购物车的文字“购买黄色”。
我希望“绿色”子类别1和“绿色”子类别2中的产品具有添加到购物车的文字“购买绿色”。
我希望Blue subcat 1和Blue subcat 2中的产品具有添加到购物车的文字BUY BLUE。
在我的实际网站中,每个父类别都有50多个子类别,因此将它们作为数组列出是不现实的。
谢谢,为什么我正在寻找一种解决方案,以根据父类别将添加到购物车的文字更改。
更新
基于@LoicTheAztec的工作代码(感谢),这是我的最终工作代码:
// Utility function to get the childs array from all parent categories
function get_the_childs( $product_category ){
$taxonomy = 'product_cat';
$parent = get_term_by( 'slug', sanitize_title( $product_category ), $taxonomy );
return get_term_children( $parent->term_id, $taxonomy );
}
// Changing the add to cart button text for product based on parent category
add_filter( 'woocommerce_product_single_add_to_cart_text', 'product_cat_single_add_to_cart_button_text', 20, 1 );
function product_cat_single_add_to_cart_button_text( $text ) {
global $product;
if( has_term( get_the_childs('reds'), 'product_cat', $product->get_id() ) )
$text = __( 'Buy Red', 'woocommerce' );
elseif( has_term( get_the_childs('yellow'), 'product_cat', $product->get_id() ) )
$text = __( 'Buy Yellow', 'woocommerce' );
elseif( has_term( get_the_childs('green'), 'product_cat', $product->get_id() ) )
$text = __( 'Buy Green', 'woocommerce' );
elseif( has_term( get_the_childs('blue'), 'product_cat', $product->get_id() ) )
$text = __( 'Buy Blue', 'woocommerce' );
return $text;
}
进一步更新:此解决方案有一个错误。如果父类别之一为空,则代码无效。
看这里:
更新
首先,您会在下面找到一个实用程序函数,用于从父产品类别中获取子数组。然后,您将可以在钩子函数中使用它。
您可以使用不同的文本按钮处理多个父产品类别。
编码:
// Utility function to get the childs array from a parent product category
function get_the_childs( $product_category ){
$taxonomy = 'product_cat';
$parent = get_term_by( 'slug', sanitize_title( $product_category ), $taxonomy );
return get_term_children( $parent->term_id, $taxonomy );
}
// Changing the add to cart button text
add_filter( 'woocommerce_product_single_add_to_cart_text', 'product_cat_single_add_to_cart_button_text', 20, 1 );
function product_cat_single_add_to_cart_button_text( $text ) {
global $product;
if( has_term( get_the_childs('Menswear'), 'product_cat', $product->get_id() ) )
$text = __( 'Buy Menswear', 'woocommerce' );
elseif( has_term( get_the_childs('Womenswear'), 'product_cat', $product->get_id() ) )
$text = __( 'Buy Womenswear', 'woocommerce' );
elseif( has_term( get_the_childs('Childswear'), 'product_cat', $product->get_id() ) )
$text = __( 'Buy Childswear', 'woocommerce' );
return $text;
}
代码进入您的活动子主题(或活动主题)的function.php文件中。经过测试和工作。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句