57
Recently I was on a coaching call with a client and the “Free Sample” challenge came up. Client has 400+ products on the website and had no intention of adding a free variation to each product manually.
So, I promised to myself I was going to study a different approach. And today you get it for free – nice! Needless to say, a comment and a social media share are much appreciated 🙂
Requirements for “Free Sample” – WooCommerce
Before digging into PHP, you will need to create a brand new simple hidden product (otherwise the code won’t work. Or call it whatever you like, but make sure to change the PHP snippet below accordingly).
Here are the requirements:
- Title: whatever you like e.g. “Free Sample“
- Price: “0“
- Catalog Visibility: “Hidden“
- Inventory: “Sold Individually“
Make sure to remember the product ID of this new product, because you will need to use it in the PHP snippet 🙂
PHP Snippet: “Free Sample” Add to Cart Button @ WooCommerce Single Product Page
/**
* @snippet Add Free Sample to Cart @ Single Product
* @how-to Get tutoraspire.com FREE
* @author Tutor Aspire
* @testedwith WooCommerce 5
* @donate $9 https://tutoraspire.com
*/
// -------------------------
// 1. Display Free Sample Add to Cart
// Note: change "123" with Free Sample ID
add_action( 'woocommerce_single_product_summary', 'tutoraspire_add_free_sample_add_cart', 35 );
function tutoraspire_add_free_sample_add_cart() {
?>