Checkbox Field That Add A Subscription Product And Change Prices Of Other Products In Checkout And Cart Page

by ADMIN 109 views

Introduction

In the world of e-commerce, offering subscription-based products can be a game-changer for businesses. WooCommerce, being one of the most popular e-commerce platforms, provides a wide range of features to help merchants create and manage their online stores. However, sometimes, custom requirements can be a challenge. In this article, we will explore how to add a checkbox field in the WooCommerce checkout section that adds an additional subscription product and adjusts the prices of other products in the cart and checkout pages.

Adding a Checkbox Field in WooCommerce Checkout

To add a checkbox field in the WooCommerce checkout section, we can use the woocommerce_checkout_fields filter. This filter allows us to modify the checkout fields and add new ones as needed. We will use this filter to add a checkbox field that adds an additional subscription product.

function add_subscription_checkbox_field($fields) {
    $fields['subscription'] = array(
        'label' => __('Add Subscription Product', 'text_domain'),
        'description' => __('Add an additional subscription product to the order.', 'text_domain'),
        'type' => 'checkbox',
        'class' => array('form-row-wide'),
        'required' => false,
    );
    return $fields;
}
add_filter('woocommerce_checkout_fields', 'add_subscription_checkbox_field');

This code adds a new field to the checkout form with the label "Add Subscription Product" and a checkbox input. The required parameter is set to false, which means the field is not required to be filled in.

Adding an Additional Subscription Product

When the checkbox is checked, we need to add an additional subscription product to the order. We can use the woocommerce_add_order_item action to achieve this. This action is triggered when a new order item is added to the order.

function add_subscription_product($item_id, $order_id) {
    if (isset($_POST['subscription'])) {
        $subscription_product_id = 123; // Replace with the ID of the subscription product
        $subscription_product_name = 'Subscription Product'; // Replace with the name of the subscription product
        $subscription_product_price = 10.99; // Replace with the price of the subscription product
        $subscription_product_quantity = 1;
        $subscription_product_meta = array(
            'subscription_product_id' => $subscription_product_id,
            'subscription_product_name' => $subscription_product_name,
            'subscription_product_price' => $subscription_product_price,
            'subscription_product_quantity' => $subscription_product_quantity,
        );
        wc_add_order_item($order_id, array(
            'name' => $subscription_product_name,
            'product_id' => $subscription_product_id,
            'quantity' => $subscription_product_quantity,
            'meta_data' => array(
                array(
                    'key' => '_subscription_product_id',
                    'value' => $subscription_product_id,
                ),
                array(
                    'key' => '_subscription_product_name',
                    'value' => $subscription_product_name,
                ),
                array(
                    'key' => '_subscription_product_price',
                    'value' => $subscription_price,
                ),
                array(
                    'key' => '_subscription_product_quantity',
                    'value' => $subscription_product_quantity,
                ),
            ),
        ));
    }
}
add_action('woocommerce_add_order_item', 'add_subscription_product');

This code checks if the checkbox is checked and adds an additional subscription product to the order. The product ID, name, price, and quantity are hardcoded in this example, but you should replace them with the actual values of your subscription product.

Adjusting Prices of Other Products in Cart and Checkout Pages

When the checkbox is checked, we also need to adjust the prices of other products in the cart and checkout pages. We can use the woocommerce_cart_item_subtotal filter to achieve this. This filter is triggered when the subtotal of a cart item is calculated.

function adjust_cart_item_subtotal($cart_item_subtotal, $cart_item, $cart_item_key) {
    if (isset($_POST['subscription'])) {
        $subscription_product_id = 123; // Replace with the ID of the subscription product
        if ($cart_item['product_id'] != $subscription_product_id) {
            $cart_item_subtotal *= 1.1; // Increase the price by 10%
        }
    }
    return $cart_item_subtotal;
}
add_filter('woocommerce_cart_item_subtotal', 'adjust_cart_item_subtotal', 10, 3);

This code checks if the checkbox is checked and increases the price of cart items by 10% if they are not the subscription product.

Conclusion

Q: What is the purpose of the dynamic checkbox field in WooCommerce checkout?

A: The dynamic checkbox field in WooCommerce checkout is used to add an additional subscription product to the order and adjust the prices of other products in the cart and checkout pages.

Q: How do I add the dynamic checkbox field to my WooCommerce checkout?

A: To add the dynamic checkbox field to your WooCommerce checkout, you need to add the following code to your theme's functions.php file:

function add_subscription_checkbox_field($fields) {
    $fields['subscription'] = array(
        'label' => __('Add Subscription Product', 'text_domain'),
        'description' => __('Add an additional subscription product to the order.', 'text_domain'),
        'type' => 'checkbox',
        'class' => array('form-row-wide'),
        'required' => false,
    );
    return $fields;
}
add_filter('woocommerce_checkout_fields', 'add_subscription_checkbox_field');

Q: How do I add an additional subscription product to the order when the checkbox is checked?

A: To add an additional subscription product to the order when the checkbox is checked, you need to add the following code to your theme's functions.php file:

function add_subscription_product($item_id, $order_id) {
    if (isset($_POST['subscription'])) {
        $subscription_product_id = 123; // Replace with the ID of the subscription product
        $subscription_product_name = 'Subscription Product'; // Replace with the name of the subscription product
        $subscription_product_price = 10.99; // Replace with the price of the subscription product
        $subscription_product_quantity = 1;
        $subscription_product_meta = array(
            'subscription_product_id' => $subscription_product_id,
            'subscription_product_name' => $subscription_product_name,
            'subscription_product_price' => $subscription_product_price,
            'subscription_product_quantity' => $subscription_product_quantity,
        );
        wc_add_order_item($order_id, array(
            'name' => $subscription_product_name,
            'product_id' => $subscription_product_id,
            'quantity' => $subscription_product_quantity,
            'meta_data' => array(
                array(
                    'key' => '_subscription_product_id',
                    'value' => $subscription_product_id,
                ),
                array(
                    'key' => '_subscription_product_name',
                    'value' => $subscription_product_name,
                ),
                array(
                    'key' => '_subscription_product_price',
                    'value' => $subscription_product_price,
                ),
                array(
                    'key' => '_subscription_product_quantity',
                    'value' => $subscription_product_quantity,
                ),
            ),
        ));
    }
}
add_action('woocommerce_add_order_item', 'add_subscription_product');

Q: How do I adjust the prices of other products in the cart and checkout pages when the checkbox is checked?

A: To adjust the prices of other products in the cart and checkout pages when the checkbox is checked, you need to add the following code to your theme's functions file:

function adjust_cart_item_subtotal($cart_item_subtotal, $cart_item, $cart_item_key) {
    if (isset($_POST['subscription'])) {
        $subscription_product_id = 123; // Replace with the ID of the subscription product
        if ($cart_item['product_id'] != $subscription_product_id) {
            $cart_item_subtotal *= 1.1; // Increase the price by 10%
        }
    }
    return $cart_item_subtotal;
}
add_filter('woocommerce_cart_item_subtotal', 'adjust_cart_item_subtotal', 10, 3);

Q: Can I customize the dynamic checkbox field to fit my needs?

A: Yes, you can customize the dynamic checkbox field to fit your needs. You can modify the code to change the label, description, and behavior of the checkbox field.

Q: Is the dynamic checkbox field compatible with all WooCommerce themes?

A: The dynamic checkbox field is compatible with most WooCommerce themes, but it may not work with all themes. You may need to modify the code to fit the specific theme you are using.

Q: Can I use the dynamic checkbox field with other WooCommerce extensions?

A: Yes, you can use the dynamic checkbox field with other WooCommerce extensions. However, you may need to modify the code to ensure compatibility with the other extensions.