In today’s online stores, customers are often offered the input field options to include gift-wrapping services for their purchases. This personal touch adds an extra layer of care to gifting items. Customers can easily select this option from the product page to be gift wrapped.
But here’s the exciting part for store owners: This WooCommerce customization implements the dynamic pricing system that lets you customize the cost based on the quantity of gifting options selected. Let’s see how you can set different prices for gift wrapping based on the number of items selected.
Solution: Implement WooCommerce Dynamic Product Calculated Price Based on Product Input Fields
The code adds the “Gift Wrapping Quantity” input field before the “Add to Cart” button on each product page, allowing customers to specify the quantity of gift wrapping they desire. Based on the quantity selected, the code will dynamically add a fee of $2 for each unit of gift wrapping.
add_action('woocommerce_before_add_to_cart_button', 'ts_add_gift_wrapping_input_field'); function ts_add_gift_wrapping_input_field() { ?> <div class="gift-wrapping-option"> <label for="gift_wrapping_quantity">Gift Wrapping Quantity:</label> <input type="number" name="gift_wrapping_quantity" id="gift_wrapping_quantity" value="0" min="0" step="1"> </div> <?php } // Save Gift Wrapping Quantity to Cart Item Data add_filter('woocommerce_add_cart_item_data', 'ts_save_gift_wrapping_quantity_to_cart', 10, 2); function ts_save_gift_wrapping_quantity_to_cart($cart_item_data, $product_id) { if (isset($_POST['gift_wrapping_quantity'])) { $cart_item_data['gift_wrapping_quantity'] = (int) $_POST['gift_wrapping_quantity']; // Ensure each cart item is unique $cart_item_data['unique_key'] = md5(microtime() . rand()); } return $cart_item_data; } // Display Gift Wrapping Quantity in Cart and Checkout add_filter('woocommerce_get_item_data', 'display_gift_wrapping_quantity_cart', 10, 2); function display_gift_wrapping_quantity_cart($item_data, $cart_item) { if (isset($cart_item['gift_wrapping_quantity'])) { $item_data[] = array( 'name' => __('Gift Wrapping Quantity', 'woocommerce'), 'value' => $cart_item['gift_wrapping_quantity'], ); } return $item_data; } // Update Cart Item Price Based on Gift Wrapping Quantity add_action('woocommerce_before_calculate_totals', 'ts_update_cart_item_price_based_on_gift_wrapping'); function ts_update_cart_item_price_based_on_gift_wrapping($cart) { if (is_admin() && !defined('DOING_AJAX')) return; foreach ($cart->get_cart() as $cart_item) { if (isset($cart_item['gift_wrapping_quantity'])) { $gift_wrapping_quantity = $cart_item['gift_wrapping_quantity']; $product = $cart_item['data']; $original_price = floatval($product->get_regular_price()); $additional_price = $gift_wrapping_quantity * 2; // Add $10 for each gift-wrapped item $new_price = $original_price + $additional_price; $product->set_price($new_price); } } }
Output
When a customer visits the product page and enters the quantity of items to be gift wrapped, there happens a dynamic claculation of the product costs. Consider a scenario where a customer purchases a set of coffee mugs priced at $20, consisting of four mugs. If the customer opts to have only two of the mugs gift wrapped, the total cost of the product in the cart would adjust to $24.
when the product is added to the cart, the product price will be changed based on the quantity of the selected input field.
In summary, we have seen how to dynamically change the product prices based on custom input fields selected by the customer. Similarly, you can also offer discounts based on input field selections, encouraging customers to make a selection of these custom fields to avail the discounts.