categories – Display “add to cart” button on every listing in product category page?


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' );

Jesse Nickles 4 years 2018-08-30T03:45:48-05:00 0 Answers 0 views 0

Leave an answer