WooCommerce checking for first-time orders - php

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

Related

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');

woocommerce user sql query that does not order in 2 weeks

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;
}

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

How can I see when a coupon is created in WooCommerce?

I am writing a custom plugin and I need to automatically send information about every newly created coupon. Until now, I can only choose a specific coupon by its name(e.g. 1234):
$coupon = new WC_Coupon("1234");
But I cannot seem to find how to get a coupon right after its created, without knowing its name, or at least how to get all of the available coupons. Can someone help?
May be this might help. Tested and it's working. This will return all coupons since coupons are saved as post_type shop_coupon.
$args = array(
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'asc',
'post_type' => 'shop_coupon',
'post_status' => 'publish',
);
$coupons = get_posts( $args );
Try to get using custom post type or directly mysql query.
1) Using mysql query
// Run a query on the postmeta table to get the id of every coupon that has the email in the customer_email restrictions
$couponlist = $wpdb->get_results("SELECT
`wp_postmeta`.`post_id`
FROM `wp_postmeta`
WHERE `wp_postmeta`.`meta_key` LIKE 'customer_email'
AND `wp_postmeta`.`meta_value` LIKE '%".$email."%'");
$couponarrayfinal = array( ); //Create an array of the ids so we can use wp_query to more quickly grab the data
// Add the ids to the array in a foreach loop
foreach( $couponlist as $key => $row) {
$value = $row->post_id;
$couponarrayfinal[] = $value ;
}
2) Using get_posts method
$arg = array(
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'asc',
'post_type' => 'shop_coupon',
'post_status' => 'publish');
$coupons_list = get_posts( $arg );
Try with this you will get the all the data
// WP_Query arguments
$args = array(
'post_type' => array('shop_coupon'),
'post_status' => array('publish'),
'posts_per_page' => '-1',
'order' => 'DESC',
'orderby' => 'id',
);
// The Query
$query = new WP_Query($args);
// The Loop
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
// do something
$coupon = new WC_Coupon(get_the_ID());
$coupnCode = $coupon->code;
$coupnAmount = $coupon->amount;
$minAmount = wc_format_decimal($coupon->minimum_amount, 2);
$maximumAmount = wc_format_decimal($coupon->maximum_amount, 2);
$expire = $coupon->expiry_date;
}
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();

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