Home > database >  Missing add to cart button on my custom product type in WordPress
Missing add to cart button on my custom product type in WordPress

Time:01-18

I'm developing a custom plugin that is adding a custom product type "Metal" to products. Everything seems alright, the plugin is almost finished but the problem is that the "Add to cart" button does not appear anywhere on the front website. I tried so many solutions googling my problem but the only thing I could do is to display the add to cart functionality on the product page but not on the shop page. I achieved that adding an action

add_action( 'woocommerce_metal_add_to_cart', fn() => wc_get_template( 'single-product/add-to-cart/simple.php' ));

using the simple product template. Now, the problem is that the button does not appear on the shop page. How can I display the "Add to cart" button on the shop page? I'm really stuck.

CodePudding user response:

You can use the function wc_get_template to include the "Add to cart" template for your custom product type in the overridden shop page template. It could look something like this:

if( $product->is_type( 'metal' ) ) {
    wc_get_template( 'single-product/add-to-cart/simple.php' );
} else {
    wc_get_template( 'loop/add-to-cart.php' );
}
  • Related