Woocommerce How do I add kilojoules from products to each other in the cart - woocommerce

I have a website where each product (food items), lists all the Caloric quantity in KJ. (added as a custom field on the single product page via Woocommerce Nutrition Info plugin)
As the products get added to the cart, I need a total KJ section near the totals, so my customer can see if the mix they are making is appropriate.
Eg. add 1 from A, and 0 to 3 of B, C, D E F. Letters being food items to make their own mix,
Knowing that WooCommerce adds prices, it would make sense that it could somehow do this too in the cart but I'm not smart enough to begin to know how to do it.
I have only limited PHP and Javascript knowledge, so I am totally out of my league and doing my head in.
Does anyone know how to I would add (I'm guessing) custom fields quantities to each other and display a running total to the cart?
I tried writing some code and big surprise it doesn't work, (trying not to be hard on myself but failing abysmally because I'm not a php coder YET!) and I don't know how to make it add up exponentially as products are added to the cart. I have spent weeks googling and Google doesn't understand my question either, hence me reaching out.
add_action( 'nutrition-table-kj', 'kj-value-add-to_cart_function' );
kj-amount-add_to_cart_function () {
$product_id = all;
if ( WC()->cart->get_cart_nutrition-table-kj-value () {
WC()->cart->add_to_cart( $nutrition-table-values );
}
}

Related

Woocommerce subscriptions : how to switch between simple subscriptions?

I'm using woocommerce membership with woocommerce subscriptions to allow users to buy one of the three levels of access of my website. The "A level" is free and gives a mininum of access, "B level" costs 10€ and gives more access to the site, whereas "C level" which costs 20€ allows user to access to the whole content.
How to allow users switching between the different level knowing that each level is a simple subscription product ?
I know this is possible with variable subscriptions but in my case, each level has his own description as we can often see on many websites like this example below, so I thought simple subscription products was the best option :
EDIT - MY SOLUTION
Finally I found a way to overcome this. I have created a variable subscription with only one attribute called "type" with values "free","middle","premium" corresponding to A, B, C levels. The user will arrive on the page with the three offers as show in the image above. Each link add directly to the cart the product variation without going through the product page using the following url :
/checkout/?add-to-cart={variation_id}
.
The user is automatically redirected to the checkout page then using the following filter in functions.php :
add_filter ('woocommerce_add_to_cart_redirect', function( $url, $adding_to_cart ) {
return wc_get_checkout_url();
}, 10, 2 );

WooCommerce ignoring decimal price

I have a WooCommerce 7.0.0 shop for which I'm programmatically creating products.
The price of these products is calculated according to various user input values.
I've done it before on other sites without problem, but I'm currently facing an issue : when the calculated price is a float, on the review order table, the price is not displayed correctly, and I'm getting a notice : A non well formed numeric value encountered in class-wc-cart.php on line 2151
Eg. if the price is 5.24€, I get 5.00€ on the product detail, subtotal, and total of the cart.
However, the price is displayed correctly as 5.24€ in the back-office.
Also, it is stored correctly in database.
Here's 2 methods I've tried to save the price before getting to checkout :
// Method 1
$product->set_price($total);
$product->set_regular_price($total);
$product->save();
// Method 2
add_post_meta($pID, '_regular_price', $total);
add_post_meta($pID, '_price', $total);
I tried various casting and rounding methods before saving (flotval, number_format, sprintf('%.2F') ) to no avail.
I also tried both , and . separators on the WooCommerce settings.
I'm running out of ideas, I must have missed something obvious but as it is, I can't put my finger on it.
Maybe something has changed on the newer versions of WooCommerce ? The last version I've used where I created products in the same way without issue, was 6.3.1.
Any help is welcome :)
Thanks,

Differences between WooCommerce price filters

First of all, thank you for your support all those years.
I am building a role-based pricing plugin for a client and I am having an existential crisis over filters WooCommerce uses for prices. I made this post in hope someone can explain this so someone in the future can find this post.
I managed to understand how filters for simple products work but I don't have an idea how equivalent filters for variable products work
Simple products
As from what I understood, you can override price, regular price, and sale price for simple products returning some value inside filters woocommerce_product_get_price, woocommerce_product_get_regular_price, and woocommerce_product_get_sale_price. Those filters will save provided data into $product object and you can easily access them later in woocommerce_get_price_html filter by using $product->get_regular_price
Variable products
Looks like variable products work differently. For example, they dont save overriden price to $product or $variation object and you cannot fetch overriden prices by calling $variation->get_regular_price.
As I understood, following filters change price of the product on add-to-cart variation select, and in the cart and do not save them in $variation object.
woocommerce_product_variation_get_price
woocommerce_product_variation_get_regular_price
woocommerce_product_variation_get_sale_price
I didnt manage to fully understand what following filters do. But when I turn them on loop price changes and product price changes but prices are removed from add-to-cart select and cart prices are not changed.
woocommerce_variation_prices_price
woocommerce_variation_prices_regular_price
woocommerce_variation_prices_sale_price

fixed price for specific categories

I would like to know some questions. We have a school where in a short time we will begin to enroll people online, especially with the Covid-19. We only charge 1 tuition even if the same student enrolls in several options. So I would like the final price to be the same regardless of the quantity of items. Well, I have found a code that solves me in part since the coupons do not work for me, discounts do not apply this for me, as a last option I could be worth it temporarily:
add_filter( 'woocommerce_calculated_total', 'change_calculated_total', 10, 2 );
function change_calculated_total( $total, $cart ) {
return $total = 40;
}
So if there are 3/4 (maximum number of items admitted to the basket) they will only pay 40€
This code is from here: Change Cart total using Hooks in Woocommerce 3.2+
This option is not the most correct, since it does not show a discount, it simply "tricks the system". But I would like this rule to affect the articles of a specific categories, only the enrollment, but the rest of the items in my store in other categories are charged at normal price. Could you please help me those 2 questions? Thanks in advance

WooCommerce - Change customer order after it has been placed and paid

Using three plugins:
WooCommerce
Subscriptio
WooEvents
I have created a product which allows for a customer to pay for an event over five instalments.
A customer has placed an order and made their first of the five payments via PayPal and has then contacted me to advise they have booked the wrong event.
The correct event is exactly the same except the dates are a month sooner.
I have added the following snippet to be able to edit 'processing' orders:
add_filter( 'wc_order_is_editable', 'wc_make_processing_orders_editable', 10, 2 );
function wc_make_processing_orders_editable( $is_editable, $order ) {
if ( $order->get_status() == 'processing' ) {
$is_editable = true;
}
return $is_editable;
}
From the order screen within the back-end of WordPress I can see that the the order can be edit by way of removing the product from the order or editing the meta data and cost.
The correct product that should have been ordered has a different product/variation ID.
My question is simply:
Should I remove the incorrectly ordered product from the order and add the correct product (both have the same properties with the exception of the courses dates); or
Should I just change the meta data and increase the stock levels for the incorrectly product back to X and reduce the stock level for the correct product?
Your second option is much better. Change the meta from database if its accessible and then manage the stock accordingly.
I have not yet done this, but according to this exchange ...
How can I add a product to an existing and paid Woocommerce order?
... you can set the order status to "On Hold" and make changes to the order itself. I presume you would then return the status to "Processing".
I would expect that if the application permits those changes, then it would be doing the background meta changes to inventory levels, etc., and spare you the problem of doing so (and the potential errors that may occur when messing with the data tables outside of the application).
Like I said, I haven't done this. But it might be worthwhile doing a little test to make sure it works as it seems to be described in the answer to the other question.
J

Resources