WooCommerce Recalculate Order, Taxes - Delete & Add Order_item + discount - php

I would like to briefly give you some information about our problem here: We need to correct all our Woocommerce orders from 2021 and now we are facing a big challenge. Since we have changed the tax class on the products, we can't just have the order recalculated but need to re-add the entire order line item to the order and remove the old line item.
We have now found a workaround that works:
Edit corresponding purchase order
Set purchase order from "completed" to "on hold
Update/Save
Now the order can be edited:
Check which coupon has been redeemed
Remember the product ID of the order
Delete product completely
Add new product via product ID
If voucher was used Add voucher code
Update/Save
This is exactly the process we use to correct our orders as needed. From this we have programmed a bot that goes through this process in wp-admin. Unfortunately, with tens of thousands of orders, this takes months....
Now I have come across Hooks and Functions and believe there is a simpler solution here.
For example I found this thread: How Can I remove a particular product from an completed order in woocommerce?
foreach ($order->get_items() as $item_id => $item) {
if ($item_id == 3) {
wc_delete_order_item($item_id);
}
}
Here it is explained how to remove an order-item from the order.
We would need a Function that:
Opens an order
Saves the order_item_id to itself
Deletes the order_item
adds the order_item again
checks if there was a coupon
if yes: delete old voucher & add new voucher
Does anyone have any ideas or can help us with this?
Thanks a lot!

Related

Quantities not updating after refund on woocommerce

I have followed the instructions to refund products as per this link: https://docs.woocommerce.com/document/woocommerce-refunds/
On the email sent back to the customer, the order value has been updated and takes into consideration the refund, but the order quantity does not. The quantity stays the same.
How can I make sure I am pulling the correct order quantity? When My customers come to collect their goods their receipt still shows they are to receive the original quantity?
I know order quantities are stored in the table "woocommerce_order_item_meta" with a separate "order_item_id" for ordered quantity and refunded quantity, the latter signalled by an additional line: "_refunded_item_id". Do I need some code to sum the "_qty"'s?
I would like my customer emails and order reports to show the correct quantity, so happy to make just make changes to order-items.php and create a function to input into my report plugin (wp All export)
Actually, Woocommerce has no any default functionality to restore stock for cancelled and refunded orders.
There is one of the plugin to solve this solution. Woocommerce Auto Restore Stock is the plugin that will help you to achieve this.
It will automatically restore your WooCommerce inventory/stock for orders that were placed and refunded or canceled.
Hope this will work for you. Thanks.

Getting product weight info in virtuemart inside order details/order history page

I have the following problem in virtuemart:
I have made a function to calculate shipping cost based on a five stage cost given by the shipping company. This works perfectly on the cart page, but on the order details and order history page it does not, as I cannot get the product weight in these pages.
I have looked into order_items.php where the order details references are kept, but there is no reference to the product weight, only to product id, quantity, category, etc.
Could anyone help me in how to get the product weight inside the order details page? I can do the rest in calculating the shipping rates and updating the final sales price, but I am stuck in getting this info.
Any help is welcomed :)
Try this,
I think you missed something about order tables, hope you are using VM2.x
In this version once the order is processed its payment,shipment information gets saved on the #__orders table.
In additionally the payment method Id and Shipment method Id also gets saved. So you can identify the method used on the order.
The idea of calculating ordered products shipping cost later on admin side is not practical bcoz later the shipping cost may very.
So practically once the user placed an order that time you have to save these information on the above table. If you're using VM default Shipping plugins they will do this by default.
Hope its clear.

magento customize quote collectTotals to show the updated totals

hey i'm implementing a custom discount system since magento discount system does not feet my requirements so i'm trying to apply a discount on an Mage_Sales_Model_Quote_Item I've read some and I've found the following function setOriginalCustomPrice the thing is that it applies on the item, and if the user changes the quantity he will get the discount on the item with the new quantity, so i'm trying to use a different method addOption on the item and only on the cart page show the calculations based on the quantity in the option value
$item->addOption(array('code'=>'promo','value' => serialize(['amount'=>10,'qty'=>1])));
and in the cart page
$promo = $item->getOptionByCode('promo');
echo '<div class="orig-price">'.$item->getOriginalPrice().'</div>';
echo '<div class="new-price">'.$item->getOriginalPrice() - ($promo['amount'] * $promo['qty']).'</div>';
the problem is that it does'nt actually apply the new price on the product,
so i want to customize Mage_Sales_Model_Quote->collectTotals() to show my discounts
and send it to the admin back-end when order is completed
how can i achieve that?
thanks in advance
I think there is a fundamental flaw in your approach. I'm not sure what you don't like in standard discounts, and what you can't achieve with catalog or shopping cart rules, but what you're trying to do definitely breaks these features (along with my heart).
However, if you're sure about what you're trying to do, then don't customize Mage_Sales_Model_Quote->collectTotals().
This function just... well, it collects all totals: subtotal, shipping, discount, etc. And it looks like you're changing only price output, but Magento itself doesn't know anything about it.
So if you want to let Magento know that you're changing the item price, you have to either add your own total, or change one of the existing totals. After that Magento will do everything else. Since after your changes Magento outputs already calculated price instead of original one, it may be strange for customer to see the original price in the cart and the additional discount total. So it looks like you will have to change subtotal total.
To do that you have to rewrite Mage_Sales_Model_Quote_Address_Total_Subtotal class in your extension and insert your calculation in _initItem() method. Around line 111 in the original file you will see the code:
$item->setPrice($finalPrice)
->setBaseOriginalPrice($finalPrice);
And this is where Magento sets price for the item, so you can insert your calculations and change $finalPrice before that. If you have virtual products, you will have to change collect() method too.

how to insert cart items as individual orders in opencart

im building a small store using opencart. i currently allow only 1 product to be added to the cart, so when a user adds different products it replaces with the new product. but allows to change the quantity of that product.
what i need to do is once a person has successfully purchased an item with a quantity of eg; 3
the product needs to be saved as 3 different orders.
is this possible to do in OC? if yes can someone please guide me how to do it?
The solution is quite simple - when creating and confirming order instead of creating one order for a product with quantity XYZ, do a loop and store XYZ orders for just one product quantity.
Basically You'll be changing mostly catalog/model/checkout/order.php::addOrder() - here the loop will take place basically around the whole function body, e.g.:
for ($i = 0; $i < $data['products'][0]['quantity']; $i++) {
// the rest of the method, but removing
// foreach ($data['products'] as $product) - this is not needed anymore
}
Also return an array of newly created order IDs so that you can confirm them afterwards...
Then You'll need to modify all the used payment controllers as these are confirming order - You'll need to change them to confirm all of the orders (which IDs You have returned when adding them - they may be stored in a session).
Though it may take some time (depending on how many payment options You use) it is not that difficult.

Restricting customers to max of 2 purchases for a certain product in Magento

I currently have an issue trying to restrict customers to purchasing 2 of a product max.
So far, I can set the max number of the product in the cart to 2 using catalog/manage products, select the product, then inventory and adjusting the max Qty allowed in cart to 2.
However, this will not stop the user coming back another day and purchasing another 2 items.
What I would like to do is create a rule so that when the user logs in and tries to purchase the product, after already purchasing it, the code will throw an error saying that they have reached their max quantity and prevent the cart from processing the order.
I believe that I would have to edit one of the php files to run a check against the users id and the product id, but I am unsure which file will need to be edited.
Magento ver. 1.3.2.4
Links that helped me, hope it helps:
step 1
step 2
First of all create one observer checkout_cart_add_product_before
In this observer event check cart quantity of product, you can get qoute item from quote object and from quote retrieve product quantity and match with newly added product if condition satisfied then redirect customer to product details page with mesage
To support future compatibility and prevent your changes from being overwritten you should create a custom extension that hooks into the core.
You might try the hook: checkout_cart_add_product_complete or checkout_cart_add_product_before and test the number of items of each product. Based on that result you might automatically remove the item and parse an error.

Categories