Change “Add to Cart” Button to Add Free Sample Product Instead

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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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;
}
</code>
<code>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; } </code>
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!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Change “Add to Cart” Button to Add Free Sample Product Instead

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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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;
}
</code>
<code>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; } </code>
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!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật