In my WooCommerce store, I have 15 products that should have over 2K variations each (WooCommerce allowing to create 50 variation max), so I used a product extra options plugin and created some inputs to retrieve the customers inputs.
- Those product inputs fields are:
- Width,
- Height,
- Thickness,
- Material Density ("MDF" = 760, "Chipboard" = 680).
- Weight calculation: ( W x H x T x MD ) / 1 000 000 000,
- Rate calculation: Weight x Height,
The price will be based on Rate ranges, so for example:
- CASE 1: 2600 <= Rate <= 5349 then price = 1000 ,
- CASE 2: 5350 <= Rate <= 8999 then price = 2000 ,
- CASE 3: 9000 <= Rate <= 17250 then price = 3000.
Here is the product input fields HTML:
<table class="thwepo-extra-options thwepo_simple" cellspacing="0">
<tbody>
<tr class="">
<td class="label leftside">
<label class="label-tag">Door height (mm)</label>
<abbr class="required" title="Required">*</abbr>
</td>
<td class="value leftside">
<input type="text" id="height884" name="height" placeholder="Height" value="" class="thwepof-input-field validate-required">
</td>
</tr>
<tr class="">
<td class="label leftside">
<label class="label-tag">Door width (mm)</label>
<abbr class="required" title="Required">*</abbr>
</td>
<td class="value leftside">
<input type="text" id="width784" name="width" placeholder="Width" value="" class="thwepof-input-field validate-required" maxlength="1800">
</td>
</tr>
<tr class="">
<td class="label leftside">
<label class="label-tag">Thickness (mm)</label>
<abbr class="required" title="Required">*</abbr>
</td>
<td class="value leftside">
<input type="text" id="thickness334" name="thickness" placeholder="Thickness" value="" class="thwepof-input-field validate-required">
</td>
</tr>
<tr class="">
<td class="label leftside">
<label class="label-tag">Material Type</label>
<abbr class="required" title="Required">*</abbr>
</td>
<td class="value leftside">
<select id="type_de_bois772" name="type_de_bois" placeholder="Agglo" value="Agglo" class="thwepof-input-field validate-required">
<option value="Agglo">Chipboard</option>
<option value="MDF">Medium (MDF)</option>
</select>
</td>
</tr>
</tbody>
</table>
And here is My PHP code:
add_action('woocommerce_before_calculate_totals', 'update_product_price_based_on_weight');
function update_product_price_based_on_weight($cart) {
foreach ($cart->get_cart() as $cart_item_key => $cart_item) {
// Get product object
$product = $cart_item['data'];
// Check if product is the one we want to modify
if ($product->get_name() === 'SERVO-DRIVE for AVENTOS HF') {
// Calculate weight based on inputs
$height = $_POST['height']; // Assuming the form submits this data
$width = $_POST['width'];
$thickness = $_POST['thickness'];
$material_type = $_POST['type_de_bois'];
// Calculate density based on material type
$density = ($material_type === 'MDF') ? 760 : 680;
// Calculate weight
$weight = ($height * $width * $thickness * $density) / 1000000000;
$force = $weight * $height;
// Adjust price based on weight
if ($force >= 2600 && $force <= 5349) {
$product->set_price(1000); // Adjust price for CASE 1
} elseif ($force >= 5350 && $force <= 8999) {
$product->set_price(2000); // Adjust price for CASE 2
} elseif ($force >= 9000 && $force <= 17250) {
$product->set_price(3000); // Adjust price for CASE 3
}
}
}
But I can't get it working, setting the correct calculated price. What I am doing wrong?
In the following code, based on your last comments, I make the price calculation dynamically, directly on the product page using JavaScript (based on your calculation algorithm). You may have to fine tune your calculation algorithm, as the current calculation doesn't seem accurate.
Note: This doesn't work with Cart and Checkout blocks, It works only with classic WooCommerce cart and checkout (based on templates).
1). Admin settings (fields)
First, in Admin edit product pages, we add a checkbox to enable the dynamic price calculation for a product, and a text field for the rate ranges pricing:
/, like2000/3000).You will get:
You will need to set in the regular price, the starting (lowest) price amount for this product.
2). Archive pages and product default displayed price
Then on shop and archive pages, for this product, we change add to cart button with a linked button to the product page. Also we change the price display adding "Starting from" prefix to the regular_price:
You will get:
3). Single product input fields (dynamic price calculation)
When all input fields are populated, the price is calculated and displayed (replacing "starting from" default starting price).
4). Saving and displaying customer inputted data in cart and checkout
you will get:
5). Set the calculated price in Cart, Mini-cart and Checkout pages
6). Save customer inputted data and display it in the order and emails
Code goes in functions.php file of your child theme (or in a plugin). Tested and works.