Can i show a custom button on WooCommerce Product if checkbox is added in product tabs?

I am trying to add an extra button only on some products.
I’ve made a checkbox on the product type Simple Product – https://prnt.sc/l4wxkt .

If this checkbox is checked then it should show an extra button on the archive page “shop” called “Add to bundle”. But only on the products where this checkbox is checked.

/**
* Add a new button to Product Archive pages (ex. store category pages) which links to the
* product page.
*/
add_action( ‘woocommerce_after_shop_loop_item’, ‘mycode_add_buttons’, 1 );

function mycode_add_buttons() {
global $product;
/* This value returns a string with yes or no */
var_dump(get_post_meta( get_the_ID(),’_sample_product’, true));

if(get_post_meta( get_the_ID(),’_sample_product’, true) == ‘yes’){
add_action( ‘woocommerce_after_shop_loop_item’, ‘mycode_more_info_button’ );
}

}

/**
* The button that should get added.
*/
function mycode_more_info_button() {
global $product;

$format = ‘<a href=”%s” class=”%s”>%s</a>’;
$url = get_permalink( $product->id );
$classes = ‘button product_type_simple add_to_cart_button ajax_add_to_cart’;
$text = ‘Add to bundle’;
echo sprintf( $format, $url, $classes, $text );
}

I know i need some kind of a loop but can’t figure out how to insert it.
Any help would be appreciated!

Read more here:: Can i show a custom button on WooCommerce Product if checkbox is added in product tabs?

Leave a Reply

Your email address will not be published. Required fields are marked *