Get WooCommerce order information like billing, shipping & date.

$order_id = 1234;
$order_data = new WC_Order($order_id);

//Order Date
$order_date = $order_data->order_date;    

//Billing information 
$billing = $order_data->get_address('billing'); 

//Shipping information 
$shipping = $order_data->get_address('shipping');
Advertisements

How to check selected product in previous customer orders or not

$seletedProduct = 25;
$exist = ‘no’;
if(is_user_logged_in()){      // Check for customer login
$orders = get_posts( array(
‘numberposts’ => -1,
‘meta_key’ => ‘_customer_user’,
‘meta_value’ => get_current_user_id(),
‘post_type’ => ‘shop_order’,
‘post_status’ => array(‘wc-completed’, ‘wc-processing’)
) );
foreach ($orders as $order) {
$WC_Order = new WC_Order($order->ID);
$items = $WC_Order->get_items();
foreach ( $items as $item ) {
if($item[‘product_id’] == $seletedProduct){
$exist = ‘yes’;
}
}
}
}
if($exist == ‘no’){
echo ‘Product not ordered’;
}else{
echo ‘Product already ordered’;
}

Remove shipping method from WooCommerce cart programmatically

add_filter( ‘woocommerce_package_rates’, ‘aviweb_hide_free_shipping_class’, 10, 2 );

function aviweb_hide_free_shipping_class( $rates, $package ) {
echo $in_cart = false;
foreach( WC()->cart->cart_contents as $key => $values ) {
$product_id = $values[‘product_id’];
if ( $product_id == 2 ) {
$in_cart = true;
break;
}
}
if( $in_cart ) {
unset( $rates[‘flat_rate:7’] );
}
return $rates;
}