• Resolved babakras

    (@babakras)


    I am trying to show product price on add to cart button and as the customer change the quantity of the product the price inside the add to cart button changes.
    For example the product is $1 and the quantity is set to 1 then add to cart button displays “BUY THIS ITEM FOR $1” and if the customer change the quantity of the product from 1 to 5 the button displays “BUY THIS ITEM FOR $5

    Does anyone know how I can do this?

    Thank you all ??

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Support Gabriel – a11n

    (@gabrielfuentes)

    Hi there ??

    Upon checking on the internet, I could find a code snippet that could do the job for you. I’ve modified it a little bit, in order to meet your needs.

    Here is the code:

    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 ) {
        // Variable products
        if( $product->is_type('variable') ) {
            // shop and archives
            if( ! is_product() ){
                $product_price = wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_variation_price() ) ) );
                return $button_text . ' - From ' . strip_tags( $product_price );
            } 
            // Single product pages
            else {
                return $button_text;
            }
        } 
        // All other product types
        else {
            $product_price = wc_price( wc_get_price_to_display( $product ) );
            return 'BUY THIS ITEM FOR ' . strip_tags( $product_price );
        }
    }

    I normally recommend using the Code Snippet plugin in order to add custom PHP code into your site without having to touch the functions.php file. Here is a link on how to use the Code Snippet plugin:

    https://www.wpbeginner.com/plugins/how-to-easily-add-custom-code-in-wordpress-without-breaking-your-site/

    Also, here is the link where I found the code:

    https://stackoverflow.com/questions/51522141/display-price-on-add-to-cart-button-from-the-functions-php-file-in-woocommerce

    I hope that helps!

    Thread Starter babakras

    (@babakras)

    Hello @gabrielfuentes

    Thank you for sharing. I have used the code snippet in child theme PHP.function and it does add the price to the “add to cart button” however the only problem is that when I change the QUANTITY of the product price wont update and it stays the same.

    Do you know how this can be achieved so when the customer change the quantity of the product the price on the “add to cart button” will update automatically according to the quantity.

    Thank you again for your help & support ??

    Plugin Support Gabriel – a11n

    (@gabrielfuentes)

    Hi @babakras!

    Happy to help ??

    Well, at the moment, there’s no default option for that in WooCommerce, and setting that as the default would require quite a bit of additional coding. Please kindly note that this requires some custom coding – which goes beyond the scope of support we are able to provide in this forum.

    If you do require more help with the actual coding, we’d recommend you hire a developer who can take a look at this, quote you for their services, and help you add this feature to your site. We’d recommend getting in touch with a web developer or one of the customization experts listed at https://woocommerce.com/customizations/

    Cheers!

    Plugin Support abwaita a11n

    (@abwaita)

    Hi @babakras,

    We’ve not heard back from you in a while, so I’m marking this thread as resolved. Hopefully, the code snippet was helpful and you were able to proceed as advised by my colleague regarding the customizations you wanted!

    If you have further questions, please feel free to open a new topic.

    Thanks.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Display product price on add to card button’ is closed to new replies.