Add an "Add to Cart" Button to the Woo Products Module

Written by Dan Mossop

Divi includes a WooCommerce Products module (formally called the Shop module), which can display a grid of products from your WooCommerce store. Here's how to add an "Add to Cart" button to the products in the products module, and some tips for configuring these buttons.

Enabling "Add to Cart" Buttons in the WooCommerce Products Module

While there isn't currently a built-in option to enable "Add to Cart" buttons in the WooCommerce Products module, there are various tutorials on the web (such as this one) all offering variations of the following PHP code snippet:

add_action('woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_add_to_cart', 10);
Simply add this PHP code to your site and an ajax enabled "Add to Cart" button will be added below each product.

Disable the "Add to Cart" buttons after they are clicked

If the product can only be added once, you may want to disable the button once it has been used. You can do so by adding the following jQuery to your site:

<script>
jQuery(function() { 
	jQuery(document).on(
		'click', 
		'.et_pb_shop .ajax_add_to_cart', 
		function() {
			jQuery(this).css('pointer-events', 'none');
		}
	);
});
</script>

Change the "Add to Cart" button text when clicked

To change the text displayed on the add to cart button, e.g. to "Added to Cart", add the following jQuery to your site:

<script>
jQuery(function() { 
	jQuery(document).on(
		'click', 
		'.et_pb_shop .ajax_add_to_cart', 
		function() {
			jQuery(this).text('Added to Cart');
		}
	);
});
</script>

Hide the "Add to Cart" button on out-of-stock products

Normally, when a product is out of stock, the "Add to Cart" button will change to a "Read More" button that just links to the product page. If you'd like to hide the button entirely when a product is out of stock, you can add the following CSS to your site:

<style>
.et_pb_shop .outofstock .button {
	display:none !important; 
}
</style>

Run PHP Code Directly in your Divi Layouts

Unlock endless customization, automation, and dynamic functionality by seamlessly adding PHP code to your Divi pages and posts with the Divi PHP Code Module. Style, preview, and debug your PHP creations directly in the visual builder with robust error handling and enhanced security.

About Dan Mossop

Dan is a Scottish-born web developer, now living in Brisbane with his wife and son. He has been sharing tips and helping users with Divi since 2014. He created Divi Booster, the first Divi plugin, and continues to develop it along with 20+ other Divi plugins. Dan has a PhD in Computer Science, a background in web security and likes a lot of stuff, 

4 Comments

  1. Hi! I added the "add to cart" button in the products module, but when "add to cart" is clicked, 2 woo notices are displayed. This is only when the "add to cart" button for the woo products module is clicked. Am I missing something? Thank you!

    Reply
    • Hey Raissa, I'm not seeing a double notice on my test site, I'm afraid. Is there any chance you're able to share a link to a page showing the issue so that I can take a look at it for you? Thanks!

      Reply
  2. Hi,

    I add to stop it, while it was showing "unavalaible" for products with wariables…

    Reply
    • Hey Antonin, thanks for the comment. I'm not sure your comment fully came through, sorry… I can't see what you added?

      But if you want to hide the "Add to Cart" button on any "Variable – unavailable" products, then I guess you'd be using some CSS similar to this, right?

      .et_pb_shop .product-type-variable.outofstock .button {
      	display:none !important; 
      }
      

      (Note that when the product is out of stock the button will normally say "Read More" rather than "Add to Cart").

      Anyway, thanks again for the comment and sorry if I haven't understood it correctly. Hopefully you've got things working the way you want, but if you do need any help with it, perhaps you can explain in a bit more detail what you need?

      Reply

Submit a Comment

Comments are manually moderated and approved at the time they are answered. A preview is shown while pending but may disappear if your are cookies cleared - don't worry though, the comment is still in the queue.

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

We may earn a commission when you visit links on our website.

Latest From Divi Booster

Enable Automatic Looping for YouTube Videos in the Divi Video Module

Enabling automatic looping for YouTube videos in your Divi video module ensures that content plays seamlessly and continuously without interruption. This feature can enhance user engagement, highlight key information, or create dynamic visual effects on your website....

Hide Video Controls in Divi Video Module

Disabling YouTube video controls in the Divi Video Module helps create a seamless, distraction-free viewing experience for your site visitors. This approach is useful when you want full control over how your video content appears and is interacted with on the page, or...

Mute YouTube Videos by Default in the Divi Video Module

In the Divi Video Module, starting your YouTube videos muted by default can create a more user-friendly browsing experience on your webpage. It is particularly beneficial for reducing distractions, enhancing visitor engagement, and providing a seamless content preview...

Autoplay Videos in Divi Video Module

Enabling videos in your Divi video module to start playing automatically can enhance both the site's design and user engagement. To ensure full compatibility with browser requirements, such as Chrome's autoplay policy, it's often necessary to start your videos muted...

How to Add the Post Status in Divi's Dynamic Content

Divi's Dynamic Content feature is great for enhancing your post/page templates with post-specific information. While Divi includes an option to show information such as the Post Created Date using Dynamic Content, there is no equivalent option for the Post Status....

Random Divi Posts