Woocommerce Subscriptions: Exporting Subscription Order "Iteration" Number to Shipstation - php

That's a terrible title, sorry. It was the best I could come up with, though. I'm a bit stumped trying to accomplish this.
The relevant pieces of software are:
Woocommerce
Woocommerce Subscriptions Extension
Woocommerce Shipstation Integration Extension
My end goal is: each time a subscription is charged, shipstation should display the subscription's current "iteration" for that order, i.e. how many times the item has been charged as a part of the subscription.
So when a customer initially subscribes, shipstation should display a "1" associated with the item. If the subscription is renewing for the sixth month, and billed monthly, shipstation should display a "6" associated with the item.
Woocommerce Subscriptions does track this number, as shown on the 'Subscriptions' admin page. It's the number highlighted on the right side of this screenshot.
This number is generated by counting the number of related orders, so it's not technically counting renewals, but it winds up working out the same for this purpose.
This first problem is that this is on a 'subscription id' level, and to bring it to shipstation we'll have to move it to an 'order meta' level or an 'order item meta' level.
Since an order could theoretically have multiple subscriptions in it, I'm thinking that it wouldn't work out to put this on the 'order meta' level. So I'm thinking that it will have to be assigned to the meta information of the actual item within the order.
Shipstation looks like this, so it may not be feasible to display something on the order level, when you get down to the 'items ordered' view it does list some information like SKU and variations. I don't want to turn this number into an actual variation, but it seems like there should logically be some way to get the information to show up in the same row.
So I guess I have to:
1) On subscription purchase OR renewal, count the number of related orders to the associated subscription and store this somewhere in the 'order item meta'.
2) Then feed that information to shipstation each time an appropriate order is exported.
I've Googled endlessly, and found a few chunks of information that might be relevant in helping me to build the first step, but I'm really not sure on my course. I can't make this data an actual item variation, so should I be doing it as a custom item attribute?
Any help or pointers in the right direction would be greatly appreciated. I was a bit surprised that I couldn't find more people previously looking for similar functionality.
Thanks in advance!

I figured this out. Took me way longer than it should have for something so simple, but it felt like a lot of documentation was missing and a lot of other use cases I could reference involved deprecated functions, etc.
I ended up putting this on the 'order meta' level rather than the 'order item meta' level after all since I realized that purchasing multiple subscription items simultaneously still bundles them into a single subscription with singular recurring orders.
For anyone that might want similar functionality in the future, this is working correctly for me now.
// woocommerce_subscription_payment_complete fires on both initial orders and renewals
add_action('woocommerce_subscription_payment_complete', 'add_iteration', 10, 2);
function add_iteration($subscription) {
// $subscription_id is extraneous and can be removed
$subscription_id = $subscription->id;
// gather relevant data
$payment_count = $subscription->get_completed_payment_count();
$related_orders = $subscription->get_related_orders();
$arrayKeys = array_keys($related_orders);
$parent_order_id = end($related_orders);
$current_order_id = $related_orders[$arrayKeys[0]];
// add data to order meta
update_post_meta($current_order_id, '_iterations', $payment_count);
}
// send it to shipstation
add_filter( 'woocommerce_shipstation_export_custom_field_2', 'shipstation_custom_field_2' );
function shipstation_custom_field_2() {
return '_iterations';
}

Related

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

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!

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.

Categories