【问题标题】:Change Add To Cart button text based on parent product categories in Woocommerce根据 Woocommerce 中的父产品类别更改“添加到购物车”按钮文本
【发布时间】:2018-11-12 14:45:47
【问题描述】:

我有一个包含父类别和子类别的 Woocommerce 商店。

例如,父类别是男装,其两个子类别是衬衫和裤子。

我想为 Menswear 父类别中的所有产品更改所有单个产品页面上的“添加到购物车”按钮文本。也就是说,衬衫和裤子子类别中的所有产品。

以下代码实现了这一点,但必须有更好的方法在 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。

我希望 Yellow subcat 1 和 Yellow subcat 2 中的产品具有添加到购物车文本 BUY YELLOW。

我希望 Green subcat 1 和 Green subcat 2 中的产品具有添加到购物车文本 BUY GREEN。

我希望 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;
}

进一步更新: 这个解决方案有一个错误。如果父类别之一为空,则代码不起作用。

看这里:

Bug in Change Add To Cart button text based on parent product categories in Woo

【问题讨论】:

    标签: php wordpress button woocommerce product


    【解决方案1】:

    更新

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

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

    代码:

    // 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 文件中。经过测试并且可以工作。

    【讨论】:

    • 您好,感谢您的意见。我在上面发布的代码也可以。我希望的是一个使用数组中的父类别(男装)而不是子类别衬衫和裤子的解决方案。在我的站点中,我只有四个父类别,但每个类别都有几十个子类别。我一直在寻找一种解决方案,避免在数组中列出几十个子猫,而只使用四个父类别。是否可以根据父类别更改添加到购物车的文本? (也就是说,该更改将发生在属于该父级的子类别中的所有产品上)。
    • 您好,再次感谢您的帮助。但是,您提供的解决方案仍然不适合我的情况。请在上面的问题中查看我进一步添加的 cmets。
    • @khunmax 抱歉,我更新的代码只是处理您在问题末尾评论的内容。
    • 不用说对不起。我非常感谢您的帮助。我认为问题在于我的问题不够明确。无论如何,我已经添加了进一步的 cmets 来澄清。
    【解决方案2】:

    感谢@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;
    }
    

    【讨论】:

    • 谢谢,。我可以建议:由于您所做的此答案中的代码已经是您问题中的编辑,您应该将其删除,您不觉得吗?
    猜你喜欢
    • 2018-10-04
    • 2021-04-24
    • 2020-08-23
    • 1970-01-01
    • 1970-01-01
    • 2019-02-20
    • 2018-06-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多