Category: PHP

Disable Payment Gateway for a Specific Country in WooCommerce

If you want to disable payment gateway for a specific country in WooCommerce then you have to decide the payment gateway and country code. Here I’m disabling the cod for India. You have to replace “cod” instead of your payment gateway. Have to replace “IN” instead of your decided country code. Place the following code in your functions.php file.

function disable_payment_gateway_for_a_country( $available_gateways ) {
  global $woocommerce;
  if ( isset( $available_gateways['cod'] ) && $woocommerce->customer->get_country() == 'IN' ) 
  {
    unset( $available_gateways['cod'] );
  } 
  return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'disable_payment_gateway_for_a_country' );

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!

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

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

$billing_email='CUSTOMER_EMAIL_ID_HERE';
$customer_orders = get_posts( array(
                    'meta_key'    => '_billing_email',
                    'meta_value'  => $billing_email,
                    'post_type'   => 'shop_order',
                    'post_status' => array_keys( wc_get_order_statuses() ),
                    'numberposts' => -1
                ));

Have any doubt, then comment here!

Get All WooCommerce Orders for a Customer

There are several ways to get all WooCommerce Orders for a Customer.
Here you can get all Woocommerce orders for a customer by customer email id.
Use the following code to fecth WooCommerce orders for a customer.
The following code will fecth all orders for a current user.

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

Have any doubt, then comment here!

How to change default product visibility in woocommerce

If you want to change default product visibility in woocommerce then add that following code in your functions.php file.
Here instead of “hidden” change your default visibility values.

function filter_woocommerce_product_visibility_default( $visible ) { 
    return "hidden"; 
	//return your default value here
}; 
add_filter( 'woocommerce_product_visibility_default', 'filter_woocommerce_product_visibility_default', 10, 1 );

Have any doubt, then comment here!