How to recalculate order taxes and total programmatically in woocommerce - wordpress

I use woocommerce from Admin only and add orders manually from Admin panel only.
Everything works fine except tax calculation.
After adding products, I must click Recalculate to be able to apply relevant taxes. If I forget, invoice does not include taxes.
I am wondering if there's a way to apply all the steps performed by Recalculate button during order update. I have custom meta boxes and many order meta that I save programmatically on order update.
Let me know if the question isn't clear, this is my first post here.
Thanks for your help.

This might help a bit into some direction:
$this->order = new WC_Order($order_id);
// Order back to pending
$this->order->update_status('pending', '<%Your message%>', TRUE);
// Recalculate the order
$total = $this->order->calculate_totals();
// Save it, although the calculate also does it.
$this->order->save();
I am not really sure about it, but it might get you going (if still needed)

Related

Woocommerce Set Default Variations

In Woocommerce some customers forget to select the variations or they don't know that they must select a variation first, so they keep clicking on the add to cart button but it doesn't work for them and they email me wondering why.
So, I thought of setting the first variation as default for all of the products.
I know I can do this through the admin but I cannot do this for all the products one by one, it's time consuming.
Please is there a code to add/edit to make the first variation the one selected by default?
I once were about to do the same and what i've found after searching a bit is this: https://quadlayers.com/default-product-attributes-woocommerce/
Basically he just hooks into woocommerce_before_single_product_summary and applies some logic that programmatically set the same thing you would set by going into the backend.
Beside this being maybe a good idea i finally gave up because i had to maintain an order into my variation choices and doing this would have break it for sure in the long term (imagine something like a dimension and you have it sorted, like 10,100,1000. You run the code today and it sets 10 as the default, then one day you add 5 and it is no longer correct)
Maybe you should think about giving some visual feedback to your users when they don't select the variation, that might solve the issue and actually create more internet-aware ppls around =)

WooCommerce - add new subtotal and re-order fields in order emails

I have a client which requires the order confirmation/completion emails to include the 'PO subtotal' which is the amount they will actually pay (product subtotal + shipping cost). We would also like to move the 'payment method' to the bottom of the list, and rename some others.
For example, the section of the email currently looks like this:
But the client needs it to look like this:
I don't need to update the website front or back ends, only the emails, although it doesn't matter if it also updates the front end. Is this possible? Thanks.
Would it be ok if it did update the display on the site as well? If so, the woocommerce_get_order_item_totals filter lets you re-arrange and add new rows there.
If not, you could:
Find a way of identifying whether that filter's being called in the context of the email(s) you're targeting. I think you can check each WC_Email's $sending for that.
Customize email-order-details.php to do what you want (copied into your theme, not editing the original).

Reset woocommerce orders to 0

Does anyone know how to reset order systems so order ids start from 0 again?
I have tried to delete all orders from woocommerce but new orders start from last one again?
Thanks
My problem was solved by using the plugin Custom Order Numbers for WooCommerce.
You need to delete all orders and leave only one order.
Now go into WooCommerce settings, you'll find a tab Custom Order Numbers.
Here input your desired custom order in Sequential: Next order number.
After, click on Save Changes.
Now click on Remunerate Order tool.
Now click for confirmation.
That's it!
From within WooCommerce, there is no way you can reset the order numbers. There are some plugins that can do it.

Unique issue with WooCommerce and hiding the add to cart button..?

We have a plugin that allows people to submit offers on products, and then the site owner can accept, counter, or decline offers, and you can negotiate back and forth until a price agreement is reached.
We are using the woocommerce_after_add_to_cart_button hook to add the offer button after the cart button.
We are getting lots of feedback that people want to leave the price of the product empty so that it simply display a price on the site, it won't display the Add to Cart button, but they do want it to display the offer button and allow that functionality.
If you set the price to 0.00, then it says FREE on the site, and still includes the add to cart button, so that's no good.
When left blank, though, the problem we run into is that when a customer clicks to buy a product from an accepted offer, WooCommerce keeps removing the item out of the cart because it says it's not purchase. This is happening because of the is_purchasable() function. With a blank price WC says the product is not purchasable, so it will automatically remove itself out of the cart, which it is indeed doing.
There is a hook available that allows us to force $purchasable = true for the product, but then of course that makes the Add to Cart button show up again, which doesn't make sense for this use-case.
So as it stands now we're at that point. We've got the system overriding $purchasable so that we can complete the checkout on a product with an empty price, but it's leaving the Add to Cart button, which allows the person to add the product at 0.00.
I'm stumped on the best way to hide to the Add to Cart button in this scenario..?? I would usually hide it by using that hook to make $purchasable = false, which takes care of it nicely, but in this case I can't do that because that's what keeps us from being able to purchase the product, of course.
Any information on a way around this would be greatly appreciated!
UPDATE
I found this tutorial for overriding core template files from a plugin: https://www.skyverge.com/blog/override-woocommerce-template-file-within-a-plugin/
So I think what we're going to try is to basically say "if $purchasable = false and $offers_enabled = true, then use these templates for the add to cart button."
Within our template we're doing nothing but commenting out the actual <button> element. This leaves all hooks in place, but hides our button.
Decent solution? Horrible idea?

woocommerce add_to_cart

When I program:
$woocommerce->cart->add_to_cart( $group_product_id, 1);
with $group_product_id being the product post ID (e.g. 300), the shopping cart shows a quantity of 2 not 1.
When I directly enter the URL (e.g.http://www.mystore.com/shop/stuff?add_to_cart=300), the shopping cart shows a quantity of 1 (the correct expected behavior).
Any suggestions would be really helpful.
Thank you.
P.S. Bonus question: is there any way to program adding multiple items to the cart (not grouped or variations) before taking the user to the cart page?
Grouped products cannot be added to cart directly. Group product is actually a group of simple products. So $group_product_id should be the id of any of the simple products which constitute the grouped product.
The first two parameters you must use on $woocommerce->cart->add_to_cart, are, $product->id and/or quantity to add. The post id has nothing to do here.
You can yes add multiple products at once using ajax calls, but is not so simple to describe on a comment, will depend on your configuration, your theme, etc, but yes, it´s posible, in fact i recently did something like that. The point is to use syncronized ajax calls that actualy call the add_to_cart function. You can use jquery for triggering many add to cart buttons at same time.

Resources