Woocommerce 经常一起购买(添加到购物车)按钮重命名

问题描述 投票:0回答:2

我正在尝试重命名 woocommerce 单一产品中的一些特定产品并经常一起购买,下面的代码适用于单一产品按钮,但如何为相同的特定产品重命名经常一起购买的按钮?

add_filter( 'woocommerce_product_single_add_to_cart_text', 'custom_single_loop_add_to_cart_button', 20, 1 );
function custom_single_loop_add_to_cart_button( $button_text ) {
    global $product;

    // Define your specific product IDs in this array
    $specific_ids = array(22246, 22241, 22227, 22039, 22009, 22004, 21999, 21991);
    
    if (in_array($product->get_id(), $specific_ids)) {
        $button_text = __("PRE ORDER", "woocommerce");
    } else {
        // Check if the product is purchasable
        if ($product->is_purchasable()) {
            $button_text = __("Add to Cart", "woocommerce");
        }
    }

    return $button_text;
}

Wanna rename this button, not for all but for the specified products

我已经尝试过这段代码,但没有成功

add_action( 'woocommerce_after_single_product_summary', 'custom_frequently_bought_together_button', 25 );
function custom_frequently_bought_together_button() {
    global $product;

    // Define your specific product IDs in this array
    $specific_ids = array(22246, 22241, 22227, 22039, 22009, 22004, 21999, 21991);

    // Check if the product is one of the specified ones
    if (in_array($product->get_id(), $specific_ids)) {
        // Modify the "Frequently Bought Together" button text
        add_filter( 'woocommerce_fbt_add_to_cart_text', 'custom_fbt_add_to_cart_text' );
    }
}

function custom_fbt_add_to_cart_text( $button_text ) {
    // Change the button text to "PRE ORDER" for "Frequently Bought Together" products
    return __("PRE ORDER", "woocommerce");
}
php wordpress woocommerce hook-woocommerce
2个回答
0
投票

尝试使用以下简化代码:

add_filter( 'woocommerce_product_single_add_to_cart_text', 'filter_product_single_add_to_cart_text', 20, 2 );
function filter_product_single_add_to_cart_text( $add_to_cart_text, $product ) {
    // Define your targeted product IDs in the array below
    $targeted_ids = array(22246, 22241, 22227, 22039, 22009, 22004, 21999, 21991);
    
    if (in_array($product->get_id(), $targeted_ids)) {
        $add_to_cart_text = __("PRE ORDER", "woocommerce");
    }
    return $add_to_cart_text;
}

应该可以。


0
投票
  1. 检查插件 Woocommerce 经常一起购买是否有过滤器 woocommerce_fbt_add_to_cart_text

  2. 如果过滤器存在,则尝试添加优先级和接受的参数,例如:

    add_filter( 'woocommerce_fbt_add_to_cart_text', 'custom_fbt_add_to_cart_text', 99, 1 );

检查 WordPress add_filter 语法:单击

add_filter( 字符串 $hook_name, 可调用 $callback, int $priority = 10, int $accepted_args = 1 );

© www.soinside.com 2019 - 2024. All rights reserved.