How to get orders of a user in Wordpress? - wordpress

Wordpress Version 5.3.2
Woocommerce Version 3.8.1
To acheive Show user his purchased orders along with order_items (products in the order)
What I have tried
Saw How to get WooCommerce order details , Get some order and order items data in Woocommerce emails ,https://github.com/woocommerce/woocommerce/wiki/wc_get_orders-and-WC_Order_Query
Still couldn't solve my issue.
Error I am facing
Fatal error: Uncaught Error: Call to a member function get_id() on
array
Code
//got the user
$user = wp_get_current_user();
//In my case $user->ID returns 1
$args = array(
'customer_id' => $user->ID,
'status' => 'confirmed',
);
//Since i only want the confirmed orders
$order= wc_get_orders( $args );
$order_id = $order->get_id();

I was struggling a bit more after the accepted solution.
Initially i was getting a complete array of orders, After iterating through that array i found what i was looking for.
$user = wp_get_current_user();
$args = array(
//'customer_id' => $user->ID
'customer_id' => 6
);
$orders = wc_get_orders($args);
foreach($orders as $order){
$order_id = $order->get_id();
echo $order_id . "<br/>";
}
This foreach allowed accessing the order_id i required.

As you can see from the docs, wc_get_orders returns an array or stdClass. The error message tells you that you cannot call get_id of an array, so you have an array. Because the customer you are searching with has multiple confirmed orders. Solution:
//...
$orders = wc_get_orders( $args );
if (!is_array($orders)) $orders = [$orders];
//...

Related

Modifying WP_Query to order by total_sales meta_key

I'm trying to update the sorting of search results in wordpress / woocommerce
so, when there is a search as: mydomain.com/?s=cases&post_type=product
The results get sorted by the 'total_sale' meta_key
I have the following hook
function sv_update_default_search_to_sales( $query ){
// check if the user is requesting an admin page
// or current query is not the main query
if ( is_admin() || ! $query->is_main_query() ){
return;
}
// Query Update for Search Results pages with products
if (is_search() && is_woocommerce() ){
if(get_query_var('post_type') != 'product'){
return;
}
$orderBy = get_query_var('orderby');
if(empty($orderBy) || $orderBy == 'relevance'){
$query->set('orderby', 'meta_value_num');
$query->set('order', 'desc');
$query->set('meta_key', 'total_sales');
$query->set('meta_type', 'NUMERIC');
}
}
}
add_action( 'pre_get_posts', 'sv_update_default_search_to_sales', 1 );
When I output $wp_query after the query has been parsed, i can see the 'meta_key' has been set to blank.
[meta_key] =>
[orderby] => meta_value_num
[order] => DESC
[meta_type] => NUMERIC
I've seen on other codes examples where they set meta_key and meta_value for matching; however, i don't have a set value, i need to order the results by 'total_sales' meta key.
Im basically trying to recreate this type of query call:
$query = new WP_Query([
'post_type' => 'product',
'meta_key' => 'total_sales',
'orderby' => 'meta_value_num'
]);
OK, so after further debugging. I realized there were other plugins modifying the query that were set to fire after my action.
I don't know which plugin specifically was modifying it, but after the page is rendered I printed the filter with
global $wp_filter;
echo '<pre>';
print_r($wp_filter['pre_get_posts']);
echo '</pre>';
Figured outthe action number: 90000 was the last action hooked, so I set mines to 90001
add_action( 'pre_get_posts', 'sv_update_default_search_to_sales', 900001 );
and it worked! x)

function to show purchase note in Woocommerce email deprecated

I am trying to show the purchase note beneath the product in the customer_processing_order email that Woocommerce generates.
I have added the following to my functions.php file:
function sww_add_images_woocommerce_emails( $output, $order ) {
// set a flag so we don't recursively call this filter
static $run = 0;
// if we've already run this filter, bail out
if ( $run ) {
return $output;
}
$args = array(
'show_purchase_note' => true,
);
// increment our flag so we don't run again
$run++;
// if first run, give WooComm our updated table
return $order->email_order_items_table( $args );
}
add_filter( 'woocommerce_email_order_items_table', 'sww_add_images_woocommerce_emails', 10, 2 );
This works, however it is printing an error message in the email stating the following:
"Notice: WC_Order::email_order_items_table is deprecated since version
3.0! Use wc_get_email_order_items instead. in /nas/content/staging/ishgamultisite/wp-includes/functions.php on line
3853"
if I change woocommerce_email_order_items_table to wc_get_email_order_items the function doesn't work.
I'm hoping someone can tell me how I should modify the code as I'm not sure?
Bit late but if anybody is still needing to show purchase notes, then there's an easier hook:
/**
* PURCHASE NOTE
* Edits the email order items args to show purchase notes
*/
function ag_add_wc_order_email_purchase_notes( $args ) {
$args['show_purchase_note'] = true;
return $args;
}
add_filter( 'woocommerce_email_order_items_args', 'ag_add_wc_order_email_purchase_notes', 10, 1 );
The filter woocommerce_email_order_items_args contains the arguments for what to display in the order emails.
$array — Optional. (callback) => array( 'order' => $order, 'items' => $order->get_items(), 'show_download_links' => $order->is_download_permitted() && ! $args['sent_to_admin'], 'show_sku' => $args['show_sku'], 'show_purchase_note' => $order->is_paid() && ! $args['sent_to_admin'], 'show_image' => $args['show_image'], 'image_size' => $args['image_size'], 'plain_text' => $args['plain_text'], 'sent_to_admin' => $args['sent_to_admin'], )
Source: http://hookr.io/filters/woocommerce_email_order_items_args/
Tested with WooCommerce 3.6.2 and works fine.
replace return $order->email_order_items_table( $args );
with
return wc_get_email_order_items( $order, $args );

WC_CREATE not setting billing or shipping address

favorite
I am trying to create a new order and my code as follows
{
$products = $_POST['products'];
$shipping_address = $_POST['shipping_address'];
$billing_address = $_POST['billing_address'];
global $woocommerce;
$args = array(
'customer_id' => get_current_user_id(),
);
$order = wc_create_order($args);
foreach ($products as $product) {
$order->add_product(get_product($product['id']), $product['quantity']);
}
$order->set_address($shipping_address, 'shipping');
$order->set_address($billing_address, 'billing');
$order->calculate_totals();
wp_send_json(array("success" => true, "order" => $order));
}
Questions
1.Shipping address and billing address are not updating. (Refer image). I am passing the fields as
https://docs.woocommerce.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/
shown in billing and shipping.
2.I need to have custom field, like (preferred pickup date), how to add that field as meta_data to that order
3.How to get the newly created order_id i.e, successful or error
How to attach coupon to that totals
1, You need to use $order->save(); to save updates you make to the order.
2, Use $order->update_meta_data( 'meta_field', $data ); and don't forget to $order->save();
3, You get the id from $order via $order->get_id();
4, Did not try coupon, but I am sure you will work that out.

How to add short description in WooCommerce checkout page

Thanks for reading, Just had a issue regarding WooCommerce, I want to add a short description checkout page of below billing field.
How to add short description in WooCommerce checkout page of below billing field?
I tried add function, custom code but failed with error.
add_filter( 'woocommerce_get_item_data', 'wc_checkout_description_so_27900033', 10, 2 );
function wc_checkout_description_so_27900033( $other_data, $cart_item )
{
$post_data = get_post( $cart_item['product_id'] );
$other_data[] = array( 'name' => 'description', 'value' => $post_data->post_excerpt );
return $other_data;
}
I was used this code but it is showing inner product info table.
There's no real reason to call get_post(). The $product object is stored in the $cart_item array and the $post object is stored inside the $product. This gets the product's excerpt (aka the short description) to show up in the cart and in the checkout. Now, it isn't likely the make the description show up on the order received page, or in the my account area, or in emails, etc since the only place that the woocommerce_get_item_data filter appears is in the cart class.
One thing to take note of, WooCommerce 2.7 is a major rewrite of WooCommerce and $_product->post->post_excerpt will result in PHP notices about directly accessing product properties. So I've suggested both the 2.6 and 2.7 compatible approaches.
add_filter( 'woocommerce_get_item_data', 'wc_checkout_description_so_27900033', 10, 2 );
function wc_checkout_description_so_27900033( $other_data, $cart_item )
{
$_product = $cart_item['data'];
// Use this for WC2.7
//$other_data[] = array( 'name' => 'description', 'value' => $_product->get_short_description() );
// Use this for WC2.6
$other_data[] = array( 'name' => 'description', 'value' => $_product->post->post_excerpt );
return $other_data;
}

NinjaForm - How To Search & Retrieve By DateTime?

I'm using NinjaForm plugin on wordpress. Here how to search and retrieve data:
<?php
$args = array(
'form_id' => $form_id,
'user_id' => $user_id,
'fields' => array(
'34' => 'checked',
'54' => 'Hello World',
),
);
// This will return an array of sub objects.
$subs = Ninja_Forms()->subs()->get( $args );
// This is a basic example of how to interact with the returned objects.
// See other documentation for all the methods and properties of the submission object.
foreach ( $subs as $sub ) {
$form_id = $sub->form_id;
$user_id = $sub->user_id;
// Returns an array of [field_id] => [user_value] pairs
$all_fields = $sub->get_all_fields();
// Echoes out the submitted value for a field
echo $sub->get_field( 34 );
}
What I want to do is searching by DateTime fields. How do I do that?
I have tried change args like this but result same.
$args = array(
'form_id' => 5,
'date_modified'=> '2015-07-25 3:19:09'
);
or like this
$args = array(
'form_id' => 5,
'date_modified'=> '< 2015-07-25 3:19:09'
);
Did I do wrong?
Find Ninja DB Table:
Go into your database using phpmyadmin or something and find the table Ninja Forms is using. Hopefully they're using their own table. If not, you can search each wp table for some of the arg data that you know returns a form from Ninja_Forms(). Or go into the Ninja plugin code and try and find where they interact with the db to find which table they write into.
Write your own mysql search code:
Instead of using Ninja's class to search, use wordpress's built in mysql search and throw in the table you found in step 1.
GLOBAL $wpdb;
$wpdb->get_results($wpdb->prepare("SELECT * FROM `ninja_table` WHERE `date_modified` = %s", $strDate));
I haven't tested, but this would be my course of action.
Use begin_date and end_data parameters to get the submissions
$args = array(
'form_id' => $form_id,
'begin_date' => '2015-07-20 0:00:00',
'end_date' => '2015-07-25 3:19:09'
);
$subs = Ninja_Forms()->subs()->get( $args );

Resources