根据Woocommerce中的父产品类别更改“添加到购物车”按钮文本

坤玛斯

我有一家带有父类别和子类别的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;
}

进一步更新:此解决方案有一个错误。如果父类别之一为空,则代码无效。

看这里:

基于Woo中父产品类别的更改添加到购物车按钮文本中的错误

LoicTheAztec

更新

首先,您会在下面找到一个实用程序函数,用于从父产品类别中获取子数组。然后,您将可以在钩子函数中使用它。

您可以使用不同的文本按钮处理多个父产品类别。

编码:

// 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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

WooCommerce根据产品类别替换购物车/结帐中的“按需订购”

WooCommerce:当产品已经在购物车中时,更改添加到购物车文本

WooCommerce无法从产品类别访问购物车

删除Woocommerce中针对特定产品类别的添加购物车按钮

删除WooCommerce 3中特定产品类别的“添加到购物车”按钮

根据Woocommerce中产品类别添加到购物车的最大商品数量

在Woocommerce中检查购物车项目中的产品类别

在WooCommerce中为特定产品类别自定义“添加到购物车”按钮

更改Woocommerce 3中特定产品类别的“添加到购物车”文本

Woocommerce中产品在购物车中时,更改添加到购物车按钮样式

WooCommerce中特定产品类别的最小购物车项目数量

Ajax上的JS警报已添加到购物车,以获取Woocommerce中特定产品类别的数量

根据Woocommerce产品类别更改“添加到购物车”按钮的颜色

更改Woocommerce中特定产品的添加到购物车按钮文本

更改Woocommerce中购买产品的“添加到购物车”按钮

如果用户未登录Woocommerce,请避免将其添加到特定产品类别的购物车中

Woocommerce中基于产品类别和项目计数的购物车总数

隐藏购物车中“ WooCommerce”产品类别的“删除商品”

根据WooCommerce产品类别禁用特定的购物车项目数量字段

根据产品类别自定义购物车总计和结帐总计文本

在WooCommerce购物车项目上为特定产品类别添加正文类

如何根据产品类别拆分woocommerce购物车页面

根据WooCommerce产品类型更改添加到购物车按钮和文本

根据WooCommerce单个产品中的重量替换添加到购物车按钮

如果购物车包含特定的Woocommerce产品类别,请阻止添加到购物车

WooCommerce重定向到添加到购物车上的产品类别

根据产品类型,WooCommerce的“添加到购物车”按钮旁边的“自定义”按钮

更改 Woocommerce 中特定产品类别的购物车项目价格

自定义特定 WooCommerce 产品类别上的“添加到购物车”按钮