I found the code to add existing product:
global $woocommerce;
$woocommerce->cart->add_to_cart(16);
But I need to add product, with price that determined by 2 inputs:
- Age
- Quality
I calculate the product's price by the formula age*quality.
I know it's possible to add product with variations, but it's so many possible variations.
Is it possible to set the product price dynamically based on custom fields calculation values?
Thanks
For the cart items (when product is added-to cart), is possible to set a custom dynamic calculated price based on product custom fields values.
1) First we add in the single product pages your 2 custom fields (Here a normal
text input
field and aselect
field) for"Age"
and"Quality"
:2) When product is add-to-cart, we save these custom post data values with the added cart item:
3) Finally in this last step, you get all necessary data from cart to make your own calculation, and to update the price for each cart item dynamically.
Here is the code that will make this dynamic calculation based on your custom field values:
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Code is tested and works perfectly.
Related answer: WooCommerce - Adding a custom price to each product in cart