WooCommerce: Adding tax to old (completed) orders - php

I have a little problem. A client of ours setup a WooCommerce shop himself and forgot to add taxes. Since he was in over his head he decided to get help setting it all up.
Now unfortunately this shop is already live and has about 50 completed orders in it. I setup the taxes etc. correctly for him but this will work only for new orders, not the old ones.
Does anybody know a simple way of adding the taxes to the old orders (all prices are including tax), maybe a loop or something I can run over the orders to get the taxes calculated?
I tried something really simple, just constructing the order and calling calculate_taxes() but this will only add a tax of 0,00€ to the order, so I guess I'm missing something important here:
$order = new WC_Order(1234);
$order->calculate_taxes();
Thank you for your help

What I did was:
1. update the product to pending payment.
2. recalculate the tax, change status to complete again, and set order action to send invoice to customer
that's it.

Alright, I dove really deep into the the mechanics behind this, so I think I can answer this myself, but be warned, it's more complicated then it seems.
There are some important things about how WooCommerce handles orders before though:
Shipping costs have to entered without taxes
Prices are stored without taxes in an order, even when you added them including taxes to the product
So when you add taxes after there are completed orders, that leaves you with the following problems:
The prices need to be changed in the old orders
The shippings costs need to be chached in the old orders
In more technical words, you would have to loop through all orders, loop through the items in the order, update prices, add the tax, update the shipping costs and only then can you run "calculate_taxes()" as I did in my really simple example.
I can't provide you with a finished function that does all this (yet) since I first have to clarify if the client is willing to pay for this. I'll add the function if he does. If anybody stumbles upon this and writes this function fell free to post is as an answer, I'll accept it as correct.

Related

In Woo I need real stock levels to decrease when order is created and don't decrease until order cancel

I need a product stock to decrease immediately on order creation and it should not increase unless the order is cancelled (it should also not decrease a second time upon payment successful or any other switch).
My problem is I want to use my storefront to invoice and collect local service work. I am using a pay later invoice gateway that drops orders into processing & woo does not decrease stock in this status so the quantities I just sold are still listed as available in my product/catalog pages. However, I found that when a customer goes to add these intangible stock quantities they receive a message like unable to add to cart insufficient stock even though the catalog pages show its available.
I cannot figure out what function this would serve since the quantity is already spoken for in a payment pending order why is it showing in product/catalog pages. I showered every post I could find across 3 search engines trying to find a similar situation with an answer but haven't had much luck.
I found this:
function reduce_stock_processing($order_id) {
wc_reduce_stock_levels($order_id);
}
In a post with similar issues and tried to implement but it had no effect that I could see.
I then found this:
add_action( 'woocommerce_valid_order_statuses_for_payment', function( $statuses, $order ) {
$statuses[] = 'on-hold';
return $statuses;
}, 10, 2 );
But it allowed me to drop orders into an on-hold status where woo does pull stock and it allowed for payment to be made from that status. I thought this is great finally, but when I started another round of testing I found that if I created an order and purchased all the remaining stock of a certain product it would A: pull the stock from inventory correctly leaving product stock at zero, then B: when I sent an email invoice to customer with payable link.
It tells them that the order cannot be paid for since item is out of stock. Frustrating, because it handles orders with abundant stock well; after order completed I'm left with appropriate stock levels.
It seems that woo needs to have extra stock on hand to allow customers to pay.... or at least that is how it seems to me.
Any ideas on how I can achieve my goal? Thanks in advance.
Update-
So I changed the first code to this:
function reduce_stock_pending($order_id) {
wc_reduce_stock_levels($order_id);
}
add_action('woocommerce_order_status_pending', 'reduce_stock_pending');
and at first I thought it didn't work because the stock levels didn't change upon creating an order and having it drop straight to pending payment status, but after toggling the status to on-hold it reduced stock and when I toggle it back to pending status the stock remains reduced and with payment successful it does not further reduce stock so that seems at least workable considering I'm the moderator I'll know what to do.... but is there a better solution that would allow me to create an order and drop it straight into pending and have it reduce stock right away without having to toggle status back and forth.
-More Info
Order notes:
everything located above image only pertains to payment
So it seems to be rapidly increasing & then reducing stock on status change to pending. Again any help would be appreciated.

Php cart: how to store product information in cart

I want to make implement Cart funcionality in Laravel. I chose this plugin. But here and in many others cart packages I see that cart row stores not a product id but full information (product name, options, price). But what if the product title or product price was changed? Then the user still sees the old title (price) and it will cause some inconvienences to him. What I do not understand, why most cart packages store full information in cart row.
So what is the right way to store cart data: full info or by keys (product_id, option_id, ...)?
There is a preferred way to store cart information, and its the latter that you mentioned.
The reason for this is to not only increase the security of your shopping cart, but to also ensure that, as you rightly pointed out, customers are getting the right price, even after a change.
Typically what you want to do is store the product ID, any customisation options as well as a cart ID. Depending on the amount of products and the traffic you're receiving it may be unnecessary to work out the basket price each time a new page is loaded, and so typically you only work out a new price when a new product is added, taken away or modified; in addition to when the customer gets to the checkout area.
This serves several purposes,
the first is that is ensure that when the cart is directly changed by
a customer action, you can show an updated price.
The second is that you cut down the amount of calculations you have to do by a large amount by only changing the prices in the basket when something acts upon it.
You can ensure that customers cannot cheat the system by modifying the price, then going on to pay a different amount because you stored the price in the session.
I can't give you a more concrete answer because you've been fairly light on the specifics, but I hope this indicates the direction you should be heading in.

Incorrect shipping calculation in Magento

I have a Magento site which uses the webshopapps matrixrates plugin for shipping rates.
The shipping cost doesn't seem to be calculating correctly and appears to apply a discount to the product cost.
In the screenshot below, the basket contains one product that has a price of €30, the shipping option 'European Airmail' (€5.50) has been selected.
Therefore the total cost should be €35.50
However the cost comes out at €30.55.
It's almost as if the Delivery cost is subtracted from the total (ignoring euro cents)
Interestingly there are two instances of this site, the UK instance appears to work correctly but I can't find anything different in the config.
I've tried comparing a CSV export of shipping rates but can't see anything obviously different.
What do I need to do to get the cost to come out as the correct amount (€35.50)?
Any suggestions welcome!
EDIT
If I dump out the quote object, the price for the product shows up as 25
But if I just load the product and dump it's data, I get the actual price which is €30:
I've got the tax amount with:
<?php
$taxAmount = $quote->getShippingAddress()->getData('tax_amount');
?>
And that comes back as 0.000
So I'm at a bit of a loss as to what is going on here.
Got to the bottom of this, it was a catalogue price rule for free shipping upto 4.95..
I had removed all shopping cart price rules already but wasnt aware that anyone had created a catalogue price rule so didnt consider it. Thanks for the suggestions guys.

Adding discounts to shipping in magento

I was wondering if anyone here has ever added an automatic discount to a shipping method in magento before. FedEx is the only company I ship with, but I have 3 different methods of FedEx. I want to add a discount to 2 of the 3 methods, and display these discounts at the method selection screen automatically at checkout(to make it seem like they are getting a deal with the higher priority shipping I suppose?). I found which file displays the methods, but I don't think this is the file I need:
template/checkout/onepage/shipping_method.phtml
Does anyone know which file these are displayed in so that I can slash out the real price, and add the discounted price bellow it? I suppose I could change the shipping price by just subtracting from the variable price, correct?
I suggest this thread for adding custom discount price but its not related to any of shipping method price calculation, its might helpful, as its helped me well..
http://www.excellencemagentoblog.com/magento-add-fee-discount-order-total

Magento Free shipping and Coupon discount

I have a free shipping price rule which is configured like this:
All customer groups
No coupon
uses per customer: 0
Conditions: Cart total >= 100, Shipping country == NL or BE or DE
Free shipment: for shipment with matching items.
When I enter a coupon with a fixed discount amount however, the free shipment disappears.
Both price rules have Stop Further Rule Processing set to NO
This behaviour of Magento is driving me insane
Apparently Magento does not care about other shopping cart price rules if there is a valid coupon code entered in the Front-end.
Fix for this issue is in the comments of the following website:
http://magentoexpert.com/issue-with-cart-price-rules-coupon-code-discard-promotion-without-coupon-code/
As suggested in one of the comments it's best that you try and convert the current rule into a "Shopping Cart Price Rule".
In a Shopping Cart Price rule you can also set conditions that depend on the subtotal of the cart, the shipping region, shipping country, ...
In the action tab you can easily Free shipping option to "For shipment with matching items"
This should solve it ;)
Go through all your other rules which might be triggered as well (make sure they have "further rule processing" on "no").
Set the priority to "1"
Try again if the rule is applied and let us know.
If we are talking for Magento CE 1.9 and above, the code is corrected as long as the database is concerned but you might encounter the same problem! If so, then there is another cause.
One also usual bug is * user not being able to update the cart quantities* or users not able to login. This has to do with formKey and the answer that was given here.
Apparently, the same solution fixes the problem of vanished shipment or payment method after applying a coupon, that we are discussing here.
Hope this helps users of newer versions...
so be it - manemoi

Categories