I am trying to build an application which texts me my woo commerce order, order items and Quantity,
I am 90% there,
function custom_woocommerce_complete_order_sms( $order_id ) {
global $woocommerce;
if ( !$order_id )
return;
$order = new WC_Order( $order_id );
$product_list = '';
$order_item = $order->get_items();
foreach( $order_item as $product ) {
$prodct_name[] = $product['name'];
}
$product_list = implode( ',\n', $prodct_name );
require "twilio-php-master/Services/Twilio.php";
$AccountSid = "xxxxxxxxx";
$AuthToken = "xxxxxxxxx";
$client = new Services_Twilio($AccountSid, $AuthToken);
$people = array(
"xxxxxxxxxx" => "Me",
);
foreach ($people as $number => $name) {
$sms = $client->account->messages->sendMessage(
"+44xxxxxxxxxx",
// the number we are sending to - Any phone number
$number,
// the sms body
"Hey $name, there is a new Order, the order is, $product_list"
);
}
}
My problem is I do not know how to get the item Quantity , for example my text looks like list, item 1, item 2, item 3, I want it to say item 1 x1, item 2 x2, item3 x3
I did try and dig into the email php file in abstract woo commerce folder to see how they do as they send Quantities in emails but got a little lost
also in the class WC_Abstract_Order the only other thing I could find is get_item_total which returns to the total of all items
From research you can also grab the qty from the order item
$product['qty'];
Therefore , it was simple to loop over and add the quantity to the item name (below)
$product_details = array();
$order_items = $order->get_items();
foreach( $order_items as $product ) {
$product_details[] = $product['name']."x".$product['qty'];
}
$product_list = implode( ',', $product_details );
Related
While im pretty hopeless at php, i feel like im getting close on this one, i just cant quite lock it in
So we have 2 plugins in the mix here WCFM & AST
In WCFM, a vendor adds a tracking number and carrier this is store in the order meta data with a meta keys of
wcfm_tracking_code
AND
wcfm_tracking_url
i found this hook elesewhere that allows me to fire a set of things to happen, once the tracking has been submitted into the system
“wcfm_after_order_mark_shipped” (Parameters -> $order_id, $order_item_id, $tracking_code, $tracking_url)
so now i want to take the above 2 order meta datas and push them into another plugin called AST
They code they provide to push data into thier system is:
<?php if ( class_exists( 'WC_Advanced_Shipment_Tracking_Actions' ) ) {
$order_id = '123'; //Replace with your order id
$tracking_provider = 'USPS'; //Replace with your shipping provider
$tracking_number = '123123'; //Replace with your tracking number
$date_shipped = '2020-06-22'; ////Replace with your shipped date
$status_shipped = 1; // 0=no,1=shipped,2=partial shipped(if partial shipped order status is enabled)
$sku = 't-shirt,blue-jeans'; //the line item (product) SKU
$qty = '1,1'; //the line item (product) quantity
if ( function_exists( 'ast_insert_tracking_number' ) ) {
ast_insert_tracking_number( $order_id, $tracking_number, $tracking_provider, $date_shipped, $status_shipped, $sku, $qty );
}
}
SO using both codes provided, can anyone show me how i can take the metadata input from WCFM and push it into AST?
Ive hired someone on upwork, and they cant even figure it out, but it seems so straightforward.
This is the code we have,it pushes the data in but it comes in looking like this
Instead of this like it should
add_action( 'wcfm_after_order_mark_shipped', 'testwr', 99, 6 );
function testwr( $order_id, $order_item_id, $tracking_code, $tracking_url, $product_id, $wcfm_tracking_data ) {
$order = wc_get_order( $order_id );
$data['order_id'] = $order_id;
$data['tracking_provider'] = $tracking_url;
$data['wcfm_tracking_code'] = $wcfm_tracking_data['wcfm_tracking_code'];
$data['date_shipped'] = $order->order_date;
$data['status_shipped'] = 1;
extract($data);
foreach ($order->get_items() as $item) {
$product = wc_get_product($item->get_product_id());
$item_quantity[] = $item->get_quantity();
$item_sku[] = $product->get_sku();
}
if( $item_sku ) {
$data['sku'] = implode( ",",$item_sku );
}
$tracking_url = $tracking_url . "/" . $tracking_code;
$url = "<a href='".$tracking_url."'>test</a>";
$data['qty'] = implode( ",",$item_quantity );
$test = ast_insert_tracking_number( $order_id, $tracking_url, $tracking_url, $date_shipped, $status_shipped, $sku, $qty);
var_dump($test);
print_r($tracking_code);
print_r($data);
print_r($wcfm_tracking_data);
var_dump($order_item_id);
//ast_insert_tracking_number
die();
}
Kudos to anyone willing to give this a try and some time
Regards
I would like that when a user orders items, a special unique key is generated for each item. These keys / This key should then be visible in ALL order mails (admin and customer). Here is what I tried to do after some research around the web :
add_action('woocommerce_order_status_processing', 'process_membership_order');
//Trying to generate my keys for each item
function process_membership_order($order_id) {
$order = new WC_Order($order_id);
$items = $order->get_items();
foreach ($items as $item_id => $product) {
$key_id = "TESTING123"; //testing purpose of course, will call a function later
wc_add_order_item_meta($item_id, 'custom_key', $key_id);
}
}
add_action( 'woocommerce_email_order_meta', 'add_email_order_meta', 10, 3 );
//Trying to edit the email order infos
function add_email_order_meta($order_obj, $sent_to_admin, $plain_text){
$key = get_post_meta( $order_obj->get_order_number(), 'custom_key', true );
if ( $plain_text === false ) {
echo "<h2>Custom Information</h2><p>$key</p>";
} else {
echo "CUSTOM INFORMATION : $key";
}
}
It doesn't seem to work, my mails have the new title but it's always empty instead of showing my TESTING123 as many times as there is an item. I'm more a tinkerer than a dev, I've used an entire day on this, a little push on the right direction would be really appreciated, I think I'm close but something's missing ! Anyway, any advice would be great, thank you.
I think you need to get order items from the order object and then you can get your custom meta by using wc_get_order_item_meta. check below code.
add_action('woocommerce_order_status_processing', 'process_membership_order');
//Trying to generate my keys for each item
function process_membership_order($order_id) {
$order = new WC_Order($order_id);
$items = $order->get_items();
foreach ($items as $item_id => $product) {
$key_id = "TESTING123"; //testing purpose of course, will call a function later
wc_add_order_item_meta($item_id, 'custom_key', $key_id);
}
}
add_action( 'woocommerce_email_order_meta', 'add_email_order_meta', 10, 3 );
//Trying to edit the email order infos
function add_email_order_meta($order_obj, $sent_to_admin, $plain_text){
$order = wc_get_order( $order_obj->get_order_number() );
$items = $order->get_items();
foreach ( $order->get_items() as $item_id => $item ) {
$key = wc_get_order_item_meta( $item_id, 'custom_key', true );
if ( $plain_text === false ) {
echo "<h2>Custom Information</h2><p>$key</p>";
} else {
echo "CUSTOM INFORMATION : $key";
}
}
}
I need to insert in a custom plugin the code to get the name of the discount codes I enter in the settings, the discount obtained with the code and the total.
Based on Get coupon data from WooCommerce orders answer code, I have inserted the following code:
$order = wc_get_order( $order_id );
// GET THE ORDER COUPON ITEMS
$order_items = $order->get_items('coupon');
// print_r($order_items); // For testing
// LOOP THROUGH ORDER COUPON ITEMS
foreach( $order_items as $item_id => $item ){
// Retrieving the coupon ID reference
$coupon_post_obj = get_page_by_title( $item->get_name(), OBJECT, 'shop_coupon' );
$coupon_id = $coupon_post_obj->ID;
// Get an instance of WC_Coupon object (necessary to use WC_Coupon methods)
$coupon = new WC_Coupon($coupon_id);
## Filtering with your coupon custom types
if( $coupon->is_type( 'fixed' ) || $coupon->is_type( 'percent' ) || $coupon->is_type( 'fixed_product' ) ){
// Get the Coupon discount amounts in the order
$order_discount_amount = wc_get_order_item_meta( $item_id, 'discount_amount', true );
$order_discount_tax_amount = wc_get_order_item_meta( $item_id, 'discount_amount_tax', true );
## Or get the coupon amount object
$coupons_amount = $coupons->get_amount();
}
}
$confirmation = str_ireplace("{order_items}", $order_items, $confirmation);
But the only information it brings back to me, when I do an echo is the word "array".
What am I doing wrong? Any help?
Try the following instead, that will add a coma separated string of applied coupon codes with their respective discount amount:
$order = wc_get_order( $order_id ); // If needed
$output = array(); // Initializing
// loop through order items "coupon"
foreach( $order->get_items('coupon') as $item_id => $item ){
// Get the coupon array data in an unprotected array
$data = $item->get_data();
// Format desired coupon data for output
$output[] = $data['code'] . ': ' . strip_tags( wc_price( $data['discount'] + $data['discount_tax'] ) );
}
$confirmation = str_ireplace("{order_items}", implode(', ', $output), $confirmation);
I have added shipping cost for the orders that are synced from Amazon. For some reason I had to set custom shipping flat price in woo-orders created for Amazon-order. It is done as follow:
$OrderOBJ = wc_get_order(2343);
$item = new WC_Order_Item_Shipping();
$new_ship_price = 10;
$shippingItem = $OrderOBJ->get_items('shipping');
$item->set_method_title( "Amazon shipping rate" );
$item->set_method_id( "amazon_flat_rate:17" );
$item->set_total( $new_ship_price );
$OrderOBJ->update_item( $item );
$OrderOBJ->calculate_totals();
$OrderOBJ->save()
The problem is, I have to update orders in each time the status is changed in Amazon, there is no problem doing that, problem is I have to update the shipping cost also if it is updated. But I have not found anyway to do so. Can anyone tell me how to update the shipping items of orders set in this way? Or is it the fact that, once shipping item is set then we cannot update or delete it?
To add or update shipping items use the following:
$order_id = 2343;
$order = wc_get_order($order_id);
$cost = 10;
$items = (array) $order->get_items('shipping');
$country = $order->get_shipping_country();
// Set the array for tax calculations
$calculate_tax_for = array(
'country' => $country_code,
'state' => '', // Can be set (optional)
'postcode' => '', // Can be set (optional)
'city' => '', // Can be set (optional)
);
if ( sizeof( $items ) == 0 ) {
$item = new WC_Order_Item_Shipping();
$items = array($item);
$new_item = true;
}
// Loop through shipping items
foreach ( $items as $item ) {
$item->set_method_title( __("Amazon shipping rate") );
$item->set_method_id( "amazon_flat_rate:17" ); // set an existing Shipping method rate ID
$item->set_total( $cost ); // (optional)
$item->calculate_taxes( $calculate_tax_for ); // Calculate taxes
if( isset($new_item) && $new_item ) {
$order->add_item( $item );
} else {
$item->save()
}
}
$order->calculate_totals();
It should better work…
To remove shipping items use te following:
$order_id = 2343;
$order = wc_get_order($order_id);
$items = (array) $order->get_items('shipping');
if ( sizeof( $items ) > 0 ) {
// Loop through shipping items
foreach ( $items as $item_id => $item ) {
$order->remove_item( $item_id );
}
$order->calculate_totals();
}
Related: Add a shipping to an order programmatically in Woocommerce 3
The WC_Order_Item_Shipping object can be added to an order using either of 2 methods.
WC_ORDER->add_shipping( WC_Order_Item_Shipping ) This is deprecated in WooCommerce V3.
WC_ORDER->add_item( WC_Order_Item_Shipping )
If you need to persist this change on the database then use WC_ORDER->save();
References: woocommerce.github.io.../#add_shipping woocommerce.github.io.../#add_item
Just get order and delete item by id
$ordr_id = 4414;
$item_id = 986;
$order = wc_get_order($ordr_id);
$order->remove_item($item_id);
$order->calculate_totals();
I have orders in the format [domain]/checkout/order-received/[order_number]/key=[wc-order-key] - how do I get [wc-order-key]?
So far I've done:
add_action('woocommerce_payment_complete', 'custom_process_order', 10, 1);
function custom_process_order($order_id)
{
$order = new WC_Order( $order_id );
$myuser_id = (int)$order->user_id;
$user_info = get_userdata($myuser_id);
$items = $order->get_items();
foreach ($items as $item)
{
$product_name = $item['name'];
$product_id = $item['product_id'];
$product_variation_id = $item['variation_id'];
$product_description = get_post_meta($item['product_id'])->post_content
}
return $order_id;
}
If i understand correctly, you need get order_key by order_id, is it correct?
If so, you can just use WC_Order property:
$test_order = new WC_Order($order_id);
$test_order_key = $test_order->order_key;
Edited
As mentioned indextwo, since Woo 3.0 there new syntax:
$test_order = wc_get_product($order_id);
$test_order_key = $test_order->get_order_key();
2018 updated answer
As WooCommerce 3 changed how property calls were made, the appropriate way to get the same information is:
$order = wc_get_order($order_id);
// Added a check to make sure it's a real order
if ($order && !is_wp_error($order)) {
$order_key = $order->get_order_key();
}
Note that you can easily do the same in reverse: get an order ID from an order key:
$order_id = wc_get_order_id_by_order_key($order_key);