I have the following code and I need to display order data based on seller id or seller name:
$filters = array(
'post_status' => 'published',
'post_type' => 'shop_order',
'posts_per_page' => 200,
'paged' => 1,
'orderby' => 'modified',
'order' => 'ASC',
'author' => $seller_id,
'post_parent' => $order_id
);
$loop = new WP_Query($filters);
while ($loop->have_posts()) {
$loop->the_post();
$order = new WC_Order($loop->post->ID);
foreach ($order->get_items() as $key => $item)
{
$red = $item['product_id'];
$_sku = get_post_meta( $red, '_sku', true );
}
}
The above loop is not showing any sku or price on the invoice section.
How to display order details on author ID (or seller name)?
Thanks
Your main problem is the post_status here: For WooCommerce Orders "published" post_status doesn't exist. The available status for WooCommerce Orders are for example:
'wc-cancelled'
'wc-completed'
'wc-custom-status'
'wc-on-hold'
'wc-pending'
'wc-processing'
'wc-refunded'
You can have different order statuses in an array…
Also 'post_parent' is can't be the order ID as it always have a 0 value
To finish since WooCommerce 3+ Order items are now a WC_Order_Item_Product object (and you need to use available methods to access the properties values).
Last thing, to get the user ID from the user name, you can use WordPress function get_user_by().
So your code will be:
$args = array(
'post_status' => array( 'wc-completed' ), // <=== HERE
'post_type' => 'shop_order',
'posts_per_page' => 200,
'paged' => 1,
'orderby' => 'modified',
'author' => $seller_id
);
$loop = new WP_Query($args);
while ($loop->have_posts()) {
$loop->the_post();
$order_obj = wc_get_order($loop->post->ID);
foreach ($order_obj->get_items() as $item_id => $item_obj)
{
$item_data = $item_obj->get_data(); // Accessing WC_Order_Item_Product object protected data
$product_id = $item_data['product_id']; // Product ID
$product_sku = get_post_meta( $product_id, '_sku', true ); // SKU
// Just for Testing output
echo "Product ID is $product_id - Sku is $product_sku<br>";
}
}
This code is tested and works for WooCommerce 3+
Helpful Answer: How to get WooCommerce order details
Related
I'm writing some code to modify the related products section as follows:
If a product has cross sell products, show those first, and fill up to 4 total products with others from the same category*
Or
If a product has no cross sell products, show 4 products from the same category*
Here's my function to filter the related products so far:
add_filter( 'woocommerce_related_products', 'fivem_add_linked_to_related_products', 9999, 3 );
function fivem_add_linked_to_related_products( $related_posts, $product_id, $args ) {
$product = wc_get_product( $product_id );
$cross_sell_ids = $product->get_cross_sell_ids();
$product_categories = $product->get_category_ids();
// Get cross sell products
$cross_sell_products = get_posts( array(
'post_type' => 'product',
'post_status' => 'publish',
'fields' => 'ids',
'post__in' => $cross_sell_ids,
'posts_per_page' => 4,
'exclude' => array( $product_id ),
));
// Calculate how many filler products are needed
$category_product_count = 4 - count( $cross_sell_products );
// Exclude main product and cross sell products
$excluded_products = array_push( $cross_sell_ids, $product_id );
// Get filler products from same category
$category_products = get_posts( array(
'post_type' => 'product',
'post_status' => 'publish',
'orderby' => 'rand',
'fields' => 'ids',
'post__not_in' => $excluded_products,
'posts_per_page' => $category_product_count,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $product_categories,
'operator' => 'IN',
)
)
));
// Merge cross sell products with filler products
$related_products = array_merge( $cross_sell_products, $category_products );
// Return related products
return $related_products;
}
Currently, the above code mostly works.
If cross-sells are set, it only displays those cross-sell products- ie. does not fill out to 4 total
If no cross-sells are set, it displays products from the same category as expected.
There are two problems I'm trying to solve:
Code above doesn't fill with category products. If I remove the post__not_in and tax_query arguments, it fills out, but obviously not with products from the same category.
I want to show cross-sell products first, then the category-related products. There appears to be another randomization somewhere that mixes the order up, and I can't work out where that comes from.
Any ideas how I can fix this? Thanks in advance.
Code contains
If a product has cross sell products, show those first, and fill up to 4 total products with others from the same category
If a product has no cross sell products, show 4 products from the same category
function filter_woocommerce_related_products( $related_posts, $product_id, $args ) {
// Taxonomy
$taxonomy = 'product_cat';
// Show products
$show_products = 4;
// Get product
$product = wc_get_product( $product_id );
// Get cross sell IDs
$cross_sell_ids = $product->get_cross_sell_ids();
// Calculate how many filler products are needed
$category_product_needed_count = $show_products - count( $cross_sell_ids );
// If category product needed
if ( $category_product_needed_count >= 1 ) {
// Retrieves product term ids for a taxonomy.
$product_cats_ids = wc_get_product_term_ids( $product_id, $taxonomy );
// Get product id(s) from a certain category, by category-id
$product_ids_from_cats_ids = get_posts( array(
'post_type' => 'product',
'numberposts' => $category_product_needed_count,
'post_status' => 'publish',
'fields' => 'ids',
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'id',
'terms' => $product_cats_ids,
'operator' => 'IN',
)
),
));
// Merge array
$related_posts = array_merge( $cross_sell_ids, $product_ids_from_cats_ids );
} else {
// Slice array until show products
$related_posts = array_slice( $cross_sell_ids, 0, $show_products );
}
// Return
return $related_posts;
}
add_filter( 'woocommerce_related_products', 'filter_woocommerce_related_products', 10, 3 );
// Order by
function filter_woocommerce_output_related_products_args( $args ) {
$args['orderby'] = 'id';
$args['order'] = 'ASC';
return $args;
}
add_filter( 'woocommerce_output_related_products_args', 'filter_woocommerce_output_related_products_args', 10, 1 );
I am currently developing a WordPress project and I am using WooCommerce with WooCommerce Subscriptions plugin to offer subscriptions to my users. I need help on how to get the quantity of a subscription in PHP.
I am using this code to get subscription but I can not retrieve quantity:
$subscriptions = wcs_get_subscriptions( array(
'customer_id' => get_current_user_id(),
'subscription_status' => 'wc-active',
'order_by' => 'DESC',
'subscriptions_per_page' => - 1
) );
When a user purchases a subscription, the user can select the quantity of subscriptions. So I need to get the value of this field:
Your code is correct and wcs_get_subscriptions() is the right and best way to get customer active subscriptions.
But you have missed a something after your code to get the customer subscription item quantity (code commented):
// Get current customer active subscriptions
$subscriptions = wcs_get_subscriptions( array(
'customer_id' => get_current_user_id(),
'subscription_status' => 'wc-active',
'order_by' => 'DESC',
'subscriptions_per_page' => - 1
) );
if ( count( $subscriptions ) > 0 ) {
// Loop through customer subscriptions
foreach ( $subscriptions as $subscription ) {
// Get the initial WC_Order object instance from the subscription
$order = wc_get_order( $subscription->get_parent_id() );
// Loop through order items
foreach ( $order->get_items() as $item ) {
$product = $item->get_product(); // Get the product object instance
// Target only subscriptions products type
if( in_array( $product->get_type(), ['subscription', 'subscription_variation'] ) ) {
$quantity = $item->get_quantity(); // Get the quantity
echo '<p>Quantity: ' . $quantity . '</p>';
}
}
}
}
Tested and works.
Here is my working code try this
$current_user_id = get_current_user_id();
$customer_subscriptions = get_posts( array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id(), // Or $user_id
'post_type' => 'shop_subscription', // WC orders post type
'post_status' => 'wc-active' // Only orders with status "completed"
) );
And if you want get all post_status subscription then use this
$customer_subscriptions_for_other_cases = get_posts( array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id(), // Or $user_id
'post_type' => 'shop_subscription', // WC orders post type
'post_status' => array('wc-on-hold','wc-pending-cancel','wc-active') // Only orders with status "completed"
) );
Thanks
I'm working with extra postmeta - invoice number for orders which use bacs as a payment method. I have added a invoice number to orders in Woocommerce by using this:
global $wpdb;
$orders = get_posts( array(
'post_type' => 'shop_order',
'posts_per_page' => '-1',
'meta_key' => '_payment_method',
'meta_value' => 'bacs',
'orderby' => 'post_date',
'order' => 'ASC'
));
$counter = 1001;
foreach($orders as $order) {
update_post_meta($order->ID, '_billing_invoice_number', $counter++);
}
So the last invoice number I got on my test site is 1006. Now I want to increase this number by 1 when the new order is added to the store. Here is my code which I'm trying to use
add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' );
function my_custom_checkout_field_update_order_meta( $order_id ) {
$order = new WC_Order($order_id);
global $wpdb;
$last_order = get_posts( array(
'post_type' => 'shop_order',
'posts_per_page' => '1',
'meta_key' => '_payment_method',
'meta_value' => 'bacs',
'orderby' => 'post_date',
'order' => 'DESC'
));
$last = $last_order[0]->ID;
$test = get_post_meta($last, '_billing_invoice_number');
$final = $test[0] + 1;
if($_POST['payment_method'] == 'bacs') {
update_post_meta( $order_id, '_billing_invoice_number', (string)$final);
}
}
The value saved in order is 1. I tried to save only $test[0] but I got empty postmeta value. I checked also if I'm getting the ID of the last order and it is stored successfully when I save $last as postmeta value.
What can be a problem here? I tried it code on the separate file (test.php) and the value I get from $test[0] is
/var/www/wordpress/test-site/wp-content/themes/myTheme/test.php:19:string '1006' (length=4)
Actually the issue is that you are not converting the string to number format.
You need to convert the $test[0] to number value.
Use $final = intval($test[0]) + 1;
I'm trying to list the total amount of product sold at the top of each category page.
A portion of this total will be donated and my client wants to advertise the amount. I have been looking and trying things for hours now and have not had any luck. I found a plugin that displays total sales for the whole store, but I want just one category.
I tried adding another filter to this plugin, but no luck. Any thoughts?
function getSaleAmountbyCategoryID($product_cat_id)
{
//first getting all the Product ID by Category ID
$pro_args = [
'post_type' => 'product',
'pages_per_post' => -1,
'post_status' => 'publish',
'fields' => 'ids',
'tax_query' => [
[
'taxonomy' => 'product_cat',
'field' => 'term_id', //This is optional, as it defaults to 'term_id'
'terms' => $product_cat_id, //(int)
]
]
];
$product_ids_query = new WP_Query($pro_args);
$include_product_id = $product_ids_query->posts;
//getting all the success orders
$args = [
'post_type' => 'shop_order',
'posts_per_page' => '-1',
'post_status' => ['wc-processing', 'wc-completed', 'wc-onhold']
];
$my_query = new WP_Query($args);
$total = 0;
$orders = $my_query->posts;
foreach ($orders as $ctr => $value)
{
$order_id = $value->ID;
//getting order object
$order = wc_get_order($order_id);
$items = $order->get_items();
foreach ($items as $item_data)
{
$product_id = $item_data['item_meta']['_product_id'][0];
if (in_array($product_id, $include_product_id))
{
//getting product object
//$_product = wc_get_product($product_id);
//$qty = $item_data['item_meta']['_qty'][0];
$pro_price = $item_data['item_meta']['_line_total'][0];
$total += $pro_price;
}
}
}
return $total;
}
Add the ^^above code to your active theme functions.php
USAGE
In your product category page template call it like
echo getSaleAmountbyCategoryID(21); //replace it by your product caregory ID
Please Note: I won't say this is a best solution as it will be slow if you have 1000s of order. If I find out the better solution I'll update my answer.
Hope this helps!
is it possible to add a category to a woocommerce post?
I am creating my products as follows:
// creates woocommerce product
$product = array(
'post_title' => $name,
'post_content' => '',
'post_status' => 'publish',
'post_author' => $current_user->ID,
'post_type' =>'product'
);
// Insert the post into the database
$product_ID = wp_insert_post($product);
I have a category called "Tree" where I have to add the above product to.
I have tried the following but without succes. Is there some special way
to add a category?
wp_set_object_terms($productID, array('Tree'), 'product_cat');
After some trial and error I solved it the following way:
// Creates woocommerce product
$product = array(
'post_title' => $name,
'post_content' => '',
'post_status' => 'publish',
'post_author' => $current_user->ID,
'post_type' =>'product'
);
// Insert the post into the database
$product_ID = wp_insert_post($product);
// Gets term object from Tree in the database.
$term = get_term_by('name', 'Tree', 'product_cat');
wp_set_object_terms($product_ID, $term->term_id, 'product_cat');
reference for more information:
How to programatically set the category for a new Woocommerce Product Creation?
http://codex.wordpress.org/Function_Reference/get_term_by
https://wordpress.stackexchange.com/questions/16394/how-to-get-a-taxonomy-term-name-by-the-slug
If you need multiple categories, just pass an array:
$categories = [ 'Some Category', 'Some other Category' ];
// Overwrite any existing term
wp_set_object_terms( $product_id, $categories, 'product_cat' );
// Or, set last argument to true to append to existing terms
wp_set_object_terms( $product_id, $categories, 'product_cat', true );