How to add an “add to cart” button in single-product.php page in woocommerce?

I am using woocommerce to develop a ecommerce site

In single-product.php I am facing a problem.

Read More

I can’t display the add to cart button under the product in this page.

So far I am using this code:

 <?php 
    //global $post;
    echo do_shortcode('[add_to_cart id="$post->ID"]');
    ?> 

But no luck for me yet!

Related posts

Leave a Reply

3 comments

  1. I would suggest using the WooCommerce default content-single-product.php template.

    By default, the single product’s add to cart template is added via the woocommerce_template_single_add_to_cart() function and is added to the woocommerce_single_product_summary hook (priority 30).

    If you must change the single product content very radically, you can either call woocommerce_template_single_add_to_cart() directly or add it to a different hook. There’s no reason to use a shortcode here.

  2. If I understood you right, you want to move the Add-to-Cart button from the top of the page to the bottom of the page, so that it would go below the main product description.

    Here is how I’ve done this on my website:

    /* Removing the Add-To-Cart button from right below the product summary.
     */
    remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
    
    /* Adding the Add-To-Cart button so that it would go after the main product description.
     */
    add_action( 'woocommerce_after_single_product_summary', 'woocommerce_template_single_add_to_cart', 12 );