Category: PHP

How to get number of item in WooCommerce cart?

If you want to get number of items in a WooCommerce cart then you can place the following code in your file.

global $woocommerce;
$count=$woocommerce->cart->cart_contents_count;

Here $count having total number of items in your WooCommerce cart.
Have any doubt, then comment here!

Get Shipping address from Order Id in WooCommerce

You can fetch shipping address by using WC_Order object and get_shipping_address() from order id.
Use the following code to fetch shipping address from order ID.

$order = new WC_Order( $order_id );
$shipping_address=$order->get_shipping_address();

Have any doubt, then comment here!

Get Billing address from Order Id in WooCommerce

You can fetch billing address by using WC_Order object and get_billing_address() from order id.
Use the following code to fetch billing address from order ID.

$order = new WC_Order( $order_id );
$billing_address=$order->get_billing_address();

Have any doubt, then comment here!

Get all orders of a single customer in WooCommerce by user ID

You can fetch all WooCommerce orders of a single customer by different parameter.
Here you can find all WooCommerce orders of a customer by user id.
Use the following to fetch all orders of a single customer in WooCommerce by user ID.
You have to replace customer id instead of ‘CUSTOMER_USER_ID_HERE’.

$user_id='CUSTOMER_USER_ID_HERE';
$customer_orders = get_posts( array(
                    'meta_key'    => '_customer_user',
                    'meta_value'  => $user_id,
                    'post_type'   => 'shop_order',
                    'post_status' => array_keys( wc_get_order_statuses() ),
                    'numberposts' => -1
                ));

Have any doubt, then comment here!