M
M
Mikhail Lebedev2021-03-10 00:32:02
WooCommerce
Mikhail Lebedev, 2021-03-10 00:32:02

Woocommerce display price in "add to cart" button?

How to display the price in the Add to Cart button in Woocommerce?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Valery Litvinov, 2021-03-10
@zaza41rus

The code needs to be added to function.php:

add_filter( 'woocommerce_product_add_to_cart_text', 'custom_add_to_cart_price', 20, 2 ); // Shop and other archives pages
add_filter( 'woocommerce_product_single_add_to_cart_text', 'custom_add_to_cart_price', 20, 2 ); // Single product pages
function custom_add_to_cart_price( $button_text, $product ) {
    if( $product->is_type('variable') ) { // Variable products
        if( ! is_product() ){ // shop and archives
            $product_price = wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_variation_price() ) ) );
            return $button_text . ' - From ' . strip_tags( $product_price );
        } else { // Single product pages
            return $button_text;
        }
    } else {     // All other product types
        $product_price = wc_price( wc_get_price_to_display( $product ) );
        return $button_text . ' - Just ' . strip_tags( $product_price );
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question