woocommerce user sql query that does not order in 2 weeks - php

I am having trouble getting the data of users who did not order in the last 2 weeks in woocommerce.
this code is pulling only one person data, I need a sql query to poll all users
function has_bought($user_id) {
// Get all customer orders
$customer_orders = get_posts( array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'meta_value' => $user_id,
'post_type' => 'shop_order', // WC orders post type
'post_status' => 'wc-completed' // Only orders with status "completed"
) );
// return "true" when customer has already one order
return count( $customer_orders ) > 0 ? true : false;
}

You can use date_query. try the below code.
function has_bought($user_id) {
// Get all customer orders
$customer_orders = get_posts( array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'meta_value' => $user_id,
'post_type' => 'shop_order', // WC orders post type
'post_status' => 'wc-completed', // Only orders with status "completed"
'date_query' => array(
'after' => date('Y-m-d', strtotime('-14 days'))
)
) );
// return "true" when customer has already one order
return count( $customer_orders ) > 0 ? true : false;
}

Related

WooCommerce checking for first-time orders

Is there a way to check at woocommerce_payment_complete if the order that processed was a first time order and not a renewal? I don't see anything related via the $order object.
I am using a function at the woocommerce_payment_complete hook to check if the order is a first time order or renewal and then sending come data via curl method, but not sure how to go about the basic act of checking this?
`function returningCustomer($billingEmail){
// Get all customer orders
if(get_current_user_id() != 0){
$customer_orders = get_posts( array(
'numberposts' => 2, // more than 1
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id(),
'post_type' => 'shop_order', // WC orders post type
'post_status' => 'wc-completed', // Only orders with completed
'fields' => 'ids', // Return Ids completed
) );
// return "true" when customer has already at least one order
// (false if not)
return count($customer_orders) > 1 ? true : false;
}
else {
$customer_orders_email = get_posts( array(
'numberposts' => 2, // more than 1
'meta_key' => '_billing_email',
'meta_value' => $billingEmail,
'post_type' => 'shop_order', // WC orders post type
'post_status' => 'wc-completed', // Only orders with completed
'fields' => 'ids', // Return Ids completed
) );
// return "true" when customer has already at least one order
// (false if not)
return count($customer_orders_email) > 1 ? true : false;
}
}`
Believe this should work

Woocommerce display status filtered orders made by customer

Hope you are doing great.
As subject says I´m trying to display a function in a specific page in Wordpress through shortcode but returns blank.
Any idea what´s wrong?
function statics_customer_dash($has_orders)
{
$customer_orders = get_posts(array(
'numberposts' => - 1,
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id() ,
'post_type' => wc_get_order_types() ,
'post_status' => array_keys(wc_get_order_statuses()) ,
'post_status' => array(
'wc-completed'
) ,
));
$total_orders = count($customer_orders);
if ($has_orders)
{
echo 'n° orders: ' . $total_orders;
}
};
// Register shortcode
add_shortcode('stats_each_customer', 'statics_customer_dash');

How to get quantity of a WooCommerce subscription?

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

Woocommerce order by seller name or author name

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

Get postmeta of last order, increase it by 1 and add to new order

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;

Categories