Display Customer order comments (customer note) in Woocommerce - php

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

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!

Set an external order ID when creating programmatically a WooCommerce order

I've been using some well documented code to programatically create Woocommerce orders. Working great. Reference here: https://quadlayers.com/add-products-woocommerce/
However I'd be keen to know if it's possible to define the order_id (or is that the post_id?) whilst creating my WC orders via a script. I run this code on my shop, which currently imports orders into Woocommerce from a marketplace online where orders already have an Order number and I'd be keen to cut out the cross-referencing via this hack, and pass along the existing Order number to WC.
Here's a rough idea of what I have;
$insert_marketplace_order_id = 1234567890;
$order = wc_create_order();
$order->set_order_id($insert_marketplace_order_id);
$order->set_date_created($date_here);
$order-> etc. etc.
$order->save();
I've obviously searched around, unfortunately the existence of a plugin - which seems to be all Google knows about it - doesn't help.
I would have though 'set_order_id' would do the trick, but perhaps there's an issue with where I'm placing it. Any other ideas?
In WooCommerce, the order Id is the post Id, so you can't insert an external reference Id as you are trying to do. WooCommerce orders are a custom post type and order Id (the post Id) is generated from last post Id used in wp_posts database table…
Now you can set instead an Order number using the filter hook woocommerce_order_number, to set your own order reference number as order number, which is not the WooCommerce Order Id (post Id).
So your code will be:
$insert_marketplace_order_id = 1234567890;
$order = new WC_Order();
$order->set_date_created($date_here);
// $order-> etc. etc.
$order->update_meta_data('_order_number', $insert_marketplace_order_id); // <== Here
$order->save();
Then you will add the following hooked function to be able to get and display the correct order number:
add_filter( 'woocommerce_order_number', 'wc_set_order_number', 10, 2 );
function wc_set_order_number( $order_id, $order ) {
// Get the order number (custom meta data)
$order_number = $order->get_meta('_order_number');
return empty($order_number) ? $order_id : $order_number;
}
Code goes in functions.php file of the active child theme (or active theme).
Now when using the WC_Order method get_order_number() WooCommerce will get/display the correct order number.
Note: As you don't use any arguments with wc_create_order() function is better to use $order = new WC_Order(); instead, which gives an empty WC_Order instance object without using save() method 2 times (so much lighter).
Related: Dynamic custom order numbers based on payment method

Woocommerce - add serial to products in orders

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.

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