I am working on a WooCommerce store where I offer free samples of the products. Here’s what I’m trying to achieve:
Button behavior: I have renamed the default “Add to Cart” button to “Free Sample.”
Goal: When a customer clicks the “Free Sample” button, I want it to add a different product (the corresponding free sample product) to the cart instead of the regular product.
Free sample product: The free sample is a separate product with a price set to £0.
Setup:
I have already renamed the “Add to Cart” button to “Free Sample” on both the shop and product pages, and I like how it looks and is positioned.
Each regular product has a corresponding free sample product, which is set up as a separate product in WooCommerce and priced at £0.
What I’ve Tried:
functions.php Code
I added the following code to my functions.php file to handle the button functionality and product ID swapping:
php
Copy code
// Modify the Add to Cart button text to 'Free Sample'
add_filter('woocommerce_product_single_add_to_cart_text', 'change_add_to_cart_button_text');
add_filter('woocommerce_product_add_to_cart_text', 'change_add_to_cart_button_text');
function change_add_to_cart_button_text() {
return __('Free Sample', 'woocommerce'); // Button says 'Free Sample'
}
// Ensure the correct free sample product is added to cart when the Free Sample button is clicked
add_filter('woocommerce_add_cart_item_data', 'add_free_sample_to_cart', 10, 3);
function add_free_sample_to_cart($cart_item_data, $product_id, $variation_id) {
if (isset($_POST['is_free_sample']) && $_POST['is_free_sample'] === 'true') {
// Get the corresponding free sample product ID from the array
$free_sample_id = get_free_sample_product_id($product_id);
if ($free_sample_id) {
$cart_item_data['product_id'] = $free_sample_id; // Replace the product ID with the free sample product ID
}
}
return $cart_item_data;
}
// Mapping of original products to free sample product IDs
function get_free_sample_product_id($product_id) {
$free_samples = array(
350 => 2658, // Original Product => Free Sample Product
353 => 2657,
362 => 2656,
368 => 2653,
371 => 2652,
// More mappings...
);
return isset($free_samples[$product_id]) ? $free_samples[$product_id] : false;
}
The Problem:
Even after implementing the above solution:
Clicking the “Free Sample” button still adds the original product to the cart (with the regular price), not the free sample product priced at £0.
There are no console errors, and the button looks and functions normally in terms of its position and styling.
I have tried several approaches, including overriding add-to-cart behavior, but nothing seems to work as intended. I want the button to seamlessly add the free sample product based on the mapping instead of the main product.
What I’m Looking For:
I’d appreciate guidance on:
How to correctly swap the product being added to the cart with its free sample counterpart.
Why the current approach might not be working and possible fixes.
Any help would be much appreciated!
Additional Context:
I’m using the WoodMart theme, but the main WooCommerce functionality should still apply. The button already has the correct label “Free Sample,” and I just need to ensure that the corresponding free sample product is added to the cart.
Thanks in advance for any help!