Change Woo-commerce shipping price based on cart product

Put below code in functions.php

function aviweb_flat_rates_cost( $rates, $package ) {

foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {

$_product = $values[‘data’];

if($_product->id == 123){

$rates[‘flat_rate:2’]->cost = 0;

}

}

return $rates;

}
add_filter( ‘woocommerce_package_rates’, ‘aviweb_flat_rates_cost’, 10, 2 );

Restrict specific products in Woo-Commerce shop

Put below code in functions.php

add_action( ‘woocommerce_product_query’, ‘aviweb_product_query’ );add_action( ‘woocommerce_product_query’, ‘aviweb_product_query’ );
function aviweb_product_query( $q_array ){

$product_ids_on_sale = array(); $product_ids_on_sale[] = 7076;

$q_array->set( ‘post__not_in’, $product_ids_on_sale );

}

post__in -> products within array

post__not_in -> products not in array

Change order of billing fields on woo-commerce checkout page

We can change fields order based on priority please find below code to change FIELD ORDER

add_filter(“woocommerce_checkout_fields”, “order_fields”);

function order_fields($fields) {

$fields[‘billing’][‘billing_first_name’][‘priority’] = 1;
$fields[‘billing’][‘billing_last_name’][‘priority’] = 2;
$fields[‘billing’][‘billing_company’][‘priority’] = 3;
$fields[‘billing’][‘billing_email’][‘priority’] = 4;
$fields[‘billing’][‘billing_phone’][‘priority’] = 5;
$fields[‘billing’][‘billing_country’][‘priority’] = 6;
$fields[‘billing’][‘billing_address_1’][‘priority’] = 7;
$fields[‘billing’][‘billing_address_2’][‘priority’] = 8;
$fields[‘billing’][‘billing_city’][‘priority’] = 9;
$fields[‘billing’][‘billing_state’][‘priority’] = 10;
$fields[‘billing’][‘billing_postcode’][‘priority’] = 11;

return $fields;
}

How to add & save custom field in My Account Details page in Woocommerce?

add_action( ‘woocommerce_edit_account_form’, ‘my_woocommerce_edit_account_form’ );
add_action( ‘woocommerce_save_account_details’, ‘my_woocommerce_save_account_details’ );

function my_woocommerce_save_account_details( $user_id ) {
update_user_meta( $user_id, ‘dob’, htmlentities( $_POST[ ‘dob’ ] ) );
$user = wp_update_user( array( ‘ID’ => $user_id, ‘user_url’ => esc_url( $_POST[ ‘url’ ] ) ) );
}

function my_woocommerce_edit_account_form() {
$user_id = get_current_user_id();
$user = get_userdata( $user_id );
if ( !$user )
return;
$dob = get_user_meta( $user_id, ‘dob’, true );
$url = $user->user_url;
?>
<fieldset>
<p class=”form-row form-row-thirds”>
<label for=”dob”>Date of Birth:</label>
<input type=”text” id=”datepicker” name=”dob” value=”<?php echo esc_attr( $dob ); ?>” class=”input-text” />
</p>
</fieldset>
<?php
}

Get products from multiple categories in wordpress woocommerce

Please find below code to get products from multiple categories in wordpress woo-commerce

$args = array( ‘post_type’ => ‘product’,
‘tax_query’ => array(
‘relation’ => ‘AND’,
array(
‘taxonomy’ => ‘product_cat’,
‘field’ => ‘slug’,
‘terms’ => $slug1
),
array(
‘taxonomy’ => ‘product_cat’,
‘field’ => ‘slug’,
‘terms’ => $slug2
)
),
‘orderby’ =>’date’,’order’ => ‘ASC’ );

Get variation attribute based on product ID in WooCommerce

/* Get variation attribute based on product ID */
$product = new WC_Product_Variable( $product_id );
$variations = $product->get_available_variations();
$var_data = [];
foreach ($variations as $variation) {
if($variation[‘variation_id’] == $variation_id){
$var_data[] = $variation[‘attributes’];
}
}

/*Get attributes from loop*/
foreach($var_data[0] as $attrName => $var_name) {
echo $var_name;
}

Get variation product quantity in admin product list page

add_filter(‘woocommerce_admin_stock_html’, ‘admin_product_data’,10,2);
function admin_product_data($stock_html,$product) {
global $wpdb;
if( $product->is_type( ‘variable’ ) ) {
$query = “SELECT sum(meta_value)
FROM $wpdb->posts AS p, $wpdb->postmeta AS s
WHERE p.post_parent = %d
AND p.post_type = ‘product_variation’
AND p.post_status = ‘publish’
AND p.id = s.post_id
AND s.meta_key = ‘_stock'”;

$product_qty = $wpdb->get_var($wpdb->prepare($query,$product->id));
$stock_html = ‘<mark class=”instock”>’. __(‘In stock’, ‘woocommerce’ ) . ‘</mark>’.’ (‘.$product_qty.’)’;
}
return $stock_html;
}