Translating add to cart woocommerce button [closed]

I am trying to translate add to cart button. I have 2 code snippets from woocommerce documentation here are they:

FOR SINGLE PRODUCT – THIS ONE WORKS

add_filter( 'woocommerce_product_single_add_to_cart_text', 
'woo_custom_cart_button_text' );    // 2.1 +

function woo_custom_cart_button_text() {

return __( 'My Button Text', 'woocommerce' );

}

FOR PRODUCT ARCHIVE – NOT WORKING?

add_filter( 'woocommerce_product_add_to_cart_text', 
'woo_archive_custom_cart_button_text' );    // 2.1 +

function woo_archive_custom_cart_button_text() {

   return __( 'My Button Text', 'woocommerce' );

}

I dont know why this other is not working. Is there some fix. I am using DIVI wordpress theme. And also overwrote my loop archive addtocart.php so I can display quantity input. This is also snippet from woocommerce documentation.

1 Answer
1

What is the content contained within your add-to-cart.php file template override?

It should be similar to this:

global $product;

echo apply_filters( 'woocommerce_loop_add_to_cart_link',
    sprintf( '<a href="https://wordpress.stackexchange.com/questions/218348/%s" rel="nofollow" data-product_id="https://wordpress.stackexchange.com/questions/218348/%s" data-product_sku="https://wordpress.stackexchange.com/questions/218348/%s" data-quantity="https://wordpress.stackexchange.com/questions/218348/%s" class="button %s product_type_%s">%s</a>',
        esc_url( $product->add_to_cart_url() ),
        esc_attr( $product->id ),
        esc_attr( $product->get_sku() ),
        esc_attr( isset( $quantity ) ? $quantity : 1 ),
        $product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
        esc_attr( $product->product_type ),
        esc_html( $product->add_to_cart_text() )
    ),
$product );

In which case, if $product->add_to_cart_text() exists, then using woocommerce_product_add_to_cart_text should be sufficient enough to achieve the result.

Leave a Comment