1

I need to place Add to cart button at every product on the page of certain category as shown below page's screenshot

How can I achive this? Link to subject page oma-fintess.com.ua

4 Answers 4

7

You can use the WooCommerce hook woocommerce_after_add_to_cart_button. This hook will add content after the "Add To Cart" button.

If the customer clicks on this button, the product should get added to the cart, and the customer should be send to the checkout page.

Add the below code in your child theme functions.php

/* Create Buy Now Button dynamically after Add To Cart button */
    function add_content_after_addtocart() {
    
        // get the current post/product ID
        $current_product_id = get_the_ID();
    
        // get the product based on the ID
        $product = wc_get_product( $current_product_id );
    
        // get the "Checkout Page" URL
        $checkout_url = WC()->cart->get_checkout_url();
    
        // run only on simple products
        if( $product->is_type( 'simple' ) ){
            echo '<a href="'.$checkout_url.'?add-to-cart='.$current_product_id.'" class="buy-now button">Buy Now</a>';
            //echo '<a href="'.$checkout_url.'" class="buy-now button">Buy Now</a>';
        }
    }
    add_action( 'woocommerce_after_add_to_cart_button', 'add_content_after_addtocart' );
1
<a href="<?php the_permalink(); ?>" class="more">More info</a><?php
if($available){?><a href="<?php
                $add_to_cart = do_shortcode('[add_to_cart_url id="'.$post->ID.'"]');
                echo $add_to_cart;
?>" class="more">Buy now</a>
                    <?php 
                }

This code solve my problem as expected.

WooCommerce documentation reference

2
  • Could you explain where you put this code ?
    – bokan
    Commented Jun 25, 2020 at 20:35
  • You have to place that code in product's page template as far as I remember.
    – Twissell
    Commented Jun 27, 2020 at 12:27
1

Here is simple solution for it.

$product_object = wc_get_product( $product_id );
echo '<a href="' . esc_url( $product_object->add_to_cart_url() ) . '" class="buy-now button">' . esc_html__( 'Buy Now', 'text-domain' ) . '</a>';
2
0

Please make sure that “Enable AJAX add to cart buttons on archives” is turned on in WooCommerce > Settings > Products > Display.

Also please Check following code place in to your theme function file.

add_action( ‘woocommerce_after_shop_loop_item’, ‘woocommerce_template_loop_add_to_cart’, 10 );
4
  • @pratik-pratel No, it doesn't work for me at all in my case. May be you missing some part of your code when paste it? I guess I should use <?php echo do_shortcode('[add_to_cart id="'.$post->ID.'"]'); ?> but how can I apply button style to this shortcode?
    – Twissell
    Commented Aug 30, 2018 at 10:28
  • You must try this [add_to_cart id="27" style="border-right:1px solid #ccc"] Commented Aug 30, 2018 at 10:37
  • If you're overriding any existing styles you'll have to write .woocommerce .product .add_to_cart_button.button{ background-color: #333333; color: #FFFFFF; } Commented Aug 30, 2018 at 10:41
  • @pratik-pratel I may guess that you had copy and little bit modify your advice from jonathanbossenger.com/…, wasn't you ?
    – Twissell
    Commented Aug 30, 2018 at 13:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.