Woocommerce – add Category Name & Add to Cart button (& buy now) on shop page

Question

This code adds a Buy Now button to product pages, but doesn’t work in the shop loop (won’t post as a code block – sorry):

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_get_checkout_url();
// run only on simple products
if( $product->is_type( ‘simple’ ) ){
echo ‘Buy Now’;
//echo ‘Buy Now’;
}
}

Perhaps I am trying to do too much! I have simple products rather than variable products for ebooks, audiobooks and paperbacks as I want people to be able to shop just those categories. But, equally, I want people to know they’re purchasing an ebook not an audiobook, for example, when they add it to cart.

Thanks for taking the time to read my question. Sorry for the double post – couldn’t get that third code snippet to post properly, then lost a lot of words, so replied to myself!

0
jane 1 week 2021-07-20T04:23:21-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse