Changing the Add To Cart button text in WooCommerce for items with variations

前端 未结 2 988
抹茶落季
抹茶落季 2020-12-04 03:36

I\'m running WooCommerce version 2.5.5. The following lines of code don\'t seem to change the Add To Cart button text on my product page for an item with variations:

<
相关标签:
2条回答
  • 2020-12-04 03:58

    Update: for WooCommerce 3+

    You are using an obsolete hook for prior versions 2.1 of WooCommerce (see at the bottom the reference).

    First you can target the desired product type in those (new) hooks with conditions:

    global $product;
    
    if ( $product->is_type( 'simple' ) ) // for simple product
        // Your text for simple product
    
    if ($product->is_type( 'variable' ) ) // for variable product
        // Your text for variable product
    
    if ($product->is_type( 'grouped' ) ) // for grouped product
        // Your text for grouped product
    
    if ($product->is_type( 'external' ) ) // for external product
        // Your text for external product
    

    Now you have 2 available hooks for Woocommerce:

    • One for single product pages (targeting the product types with conditions):
    add_filter( 'woocommerce_product_single_add_to_cart_text', 'my_custom_cart_button_text', 10, 2 );
    
    • And the other for product archives pages (targeting the product types with conditions):
    add_filter( 'woocommerce_product_add_to_cart_text', 'my_custom_cart_button_text', 10, 2 );
    

    And you will use one or both of them with your customized function targeting through the variables product type condition, this way:

    function my_custom_cart_button_text( $button_text, $product ) {
    
        if ( $product->is_type( 'variable' ) )
            $button_text = __('Buy Now', 'woocommerce');
    
        return $button_text
    }
    

    You can also have a custom button text by product type: See here.


    Reference: Change add to cart button text

    0 讨论(0)
  • 2020-12-04 04:03

    The correct filter for the single product page is woocommerce_product_single_add_to_cart_text.

    function my_custom_cart_button_text( $text, $product ) {
        if( $product->is_type( 'variable' ) ){
            $text = __('Buy Now', 'woocommerce');
        }
        return $text;
    }
    add_filter( 'woocommerce_product_single_add_to_cart_text', 'my_custom_cart_button_text', 10, 2 );
    
    0 讨论(0)
提交回复
热议问题