Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

How to Add BOGO (Buy One Get One) for Products with Custom Fields Added to Cart?

In this post, we’ll look into the step-by-step process of implementing a BOGO (Buy One Get One) offer specifically tied to Product Input Fields For WooCommerce Plugin.

Let’s have an overview of what the plugin does. The plugin allows you to define custom input fields such as text boxes, checkboxes, or dropdowns for your products with much more flexible functionality according to your needs. Customers can input additional information, choose variations, or specify preferences directly on the product page.

To implement the functionality, firstly, you need to download the plugin file . Once the plugin is activated, you can configure the settings of this plugin from WooCommerce >Settings >Product Input Fields.

How to Add BOGO (Buy One Get One) for Products with Custom Fields Added to Cart? - Tyche Softwares
How to Add BOGO (Buy One Get One) for Products with Custom Fields Added to Cart? - Tyche Softwares

As a next step, you can add the given code in the code snippets plugin, save, and activate the snippet. The code snippet helps to check if per-product input fields are configured and enabled for the selected products. If the customer’s chosen product has a custom field option enabled for per-product input, it adds a complimentary product to the cart.

// Hook to check per-product input field and add free product to cart
add_action('woocommerce_add_to_cart', 'ts_check_and_add_free_product', 10, 6);

function ts_check_and_add_free_product($cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data) {
    if (is_admin() && !defined('DOING_AJAX')) {
        return;
    }

    // Check if the per-product input field is enabled for the current product
    if (is_per_product_input_enabled($product_id)) {
        // Add the free product (ID 470) to the cart if not already added
        add_free_product_to_cart_if_not_exists();
    }
}

// Function to check if per-product input field is enabled
function is_per_product_input_enabled($product_id) {
    // Get total number of fields set for per product.
    $total_fields = get_post_meta($product_id, '_' . ALG_WC_PIF_ID . '_local_total_number', true);

    if ($total_fields > 0) {
        for ($i = 1; $i <= $total_fields; $i++) {
            // Check if the field is enabled.
            $is_enabled = get_post_meta($product_id, '_' . ALG_WC_PIF_ID . '_enabled_local_' . $i, true);

            if ('yes' === $is_enabled) {
                return true; // If any field is enabled, return true.
            }
        }
    }

    return false; // If no enabled fields are found, return false.
}

// Function to add the free product to the cart if it doesn't already exist
function add_free_product_to_cart_if_not_exists() {
    $free_product_id = 470;

    // Check if the free product (ID 470) is not already in the cart
    if (!WC()->cart->find_product_in_cart($free_product_id)) {
        // Add the free product to the cart
        WC()->cart->add_to_cart($free_product_id, 1);
    }
}

Output

When customers visit the individual product page of the website, products that have the per-product option enabled on the backend will have an input field. As an admin, based on your shop preference, you can choose any type of input field such as checkbox, file, country, date picker, etc as provided in the dropdown.

How to Add BOGO (Buy One Get One) for Products with Custom Fields Added to Cart? - Tyche Softwares

For example, if you run a shop selling personalized T-shirts and only for some T-shirts you can provide it with a special checkbox option on the product page that says “Add gift wrap:”.

In the below image, I have enabled the input field ‘checkbox’ for the particular product ‘T-shirt’. For example, if you are selling tickets to events or concerts, then you may activate the date picker by your choice. Select any one of the field types from the drop-down options and the selected option is shown on the individual product page.

How to Add BOGO (Buy One Get One) for Products with Custom Fields Added to Cart? - Tyche Softwares

The customer when visiting the product page of this particular product will be able to see the option as shown below.
Note: You also have the option of enabling this option for all products as well.

How to Add BOGO (Buy One Get One) for Products with Custom Fields Added to Cart? - Tyche Softwares

When the customer selects the checkbox and proceeds to add the product to the cart, subsequently the free product is also automatically added to the cart.

How to Add BOGO (Buy One Get One) for Products with Custom Fields Added to Cart? - Tyche Softwares

When products with the absence of input fields are added to the cart, the free product is not added.

How to Add BOGO (Buy One Get One) for Products with Custom Fields Added to Cart? - Tyche Softwares
How to Add BOGO (Buy One Get One) for Products with Custom Fields Added to Cart? - Tyche Softwares

Similarly, you can add tons of features from the Product Input Field Pro for WooCommerce plugin that provides the options to add color fields, range fields, country fields, and even change the product prices dynamically according to the chosen custom input field.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x