Woocommerce - add serial to products in orders - php

I am looking for a way to add a serial number to each sold product, in the order display in WooCommerce. What I need is a manual solution, where the S/N of the sent product is added to the order before the package is sent. I have tried to show it on the following picture:
Originally, I thought about implementing it as a product meta (without really knowing how) but I realized that product metas can only be altered when the order is set on hold and not while processing.
Any ideas aboout how to proceed? Thanks already!

Not really sure what your trying to achieve but if you need a serial number for each product (just to display it in your cart page) you can add that data as an attribute to each listing then recall it in the checkout area and have it display using hooks. How do you want to process the serial number? is it purely for display on checkout? assuming it is...
add_filter( 'woocommerce_cart_item_name', recall_serial_number, 10, 2 );
function recall_serial_number() {
$serial = $item_data->get_attributes( 'serial-number' );
echo $serial;
}
Give this code a whirl if all you want to do is display the serial on the cart page without processing it any further. Make sure the name of the attribute is "Serial Number" not tested but should work.

Related

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

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';
}

Display Customer order comments (customer note) in Woocommerce

I have a little problem when I try to display woocommerce customer order comments (not the notes, but the comments that a customer can add during the checkout process).
(I'm going to add just the relative lines for this problem, as other woocommerce data is correctly displayed so it shouldn't be a setup problem).
What I've tried so far is this:
$notes = $order->get_customer_order_notes(); //This line returns an Array[]
Inside that array, this is the field that I think I need, as it contains my order comment:
$notes
0={stdClass} 38
post_excerpt = "test"
and so what I did is trying to display this value like this:
echo "Order Notes: " . $notes->post_excerpt
but unfortunately the result is empty.
What am I doing wrong?
Many thanks
Ok, after some time spent on trying, I finally find out that the right way to display the customer checkout comment is selecting $order->customer_message; and set this value as variable.
Update 2017 - 2018 | For Woocommerce 3+
Since Woocommerce 3 you can't access anymore properties From the WC_Order object. You need to use the WC_Order method get_customer_note() instead:
$customer_note = $order->get_customer_note();
Related:
Display order customer note in Woocommerce email notifications
Add order customer note to YITH Woocommerce PDF Invoice

How to set custom price for a product in Magento for only the first of that product added to the cart?

I trying to allow users to get the first product sample they add to their cart for free, but any samples they add after (including the one they already added) should be normal price. Right now I using setOriginalCustomPrice in a module, but the customer can change the quantity, and the price stays at zero instead of going up. Anyone know how to go about this?
You can achieve this by a simple calculation where you are using setOriginalCustomPrice.Currently I think you are setting price 0.Instead of doing this,use following logic to set custom price.
$customPrice=$qtyincart*$productPrice-$productPrice.
So if user add 1 qty then $customPrice will be 0 else will equal to sum of qty-1.
I hope this solve your purpose.

Woocommerce manipulate products internaly

I need the following and i´m turning around the thing but can´t get a clean way on doing it:
Some products needs to belong to x category, if a customer buy one of those products, all the rest of the products behind that category should get outofstock or unpublished.
So i need a way to create a function that could run once a product is sold and then find all those products with same category and change their metadata, in this case i will solve the rest of the thing just changing the outofstock meta, but knowing that i guess i will also be able to edit any other post meta value.
I know how to update a meta value, i can´t imagine how to hook/filter the rest. Any ideas? Thanks
Have you taken a look at the WooCommerce hooks?
I would check out the woocommerce_order_status_.$new_status->slug hook for the status completed. So something like:
function the_function($order_id) {
// get the order object
$order = new WC_Order( $order_id );
}
add_action( 'woocommerce_order_status_completed', 'the_function' );

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.

Categories