php - Restrict Shipping classes for only one country in WooCommerce -
i'm using woocommerce , i'd restrict shipping class 'alcoholics' , allow just italy , no other countries, can me code? thank help!
function( $is_available ) { $cart_items = wc()->cart->get_cart(); foreach ( $cart_items $cart_item ) { $product = $cart_item['data']; $class = $product->get_shipping_class(); if ( $class === 'alcoholics' ) { return false; } } return $is_available; } add_filter( 'woocommerce_states', 'al_woocommerce_states' ); function al_woocommerce_states( $states ) { $states['it'] = array( 'ag' => __( 'agrigento', 'woocommerce' ), 'al' => __( 'alessandria', 'woocommerce' ), 'an' => __( 'ancona', 'woocommerce' ), 'ao' => __( 'aosta', 'woocommerce' ), 'ar' => __( 'arezzo', 'woocommerce' ), 'ap' => __( 'ascoli piceno', 'woocommerce' ), 'at' => __( 'asti', 'woocommerce' ), 'av' => __( 'avellino', 'woocommerce' ), 'ba' => __( 'bari', 'woocommerce' ), 'bt' => __( 'barletta-andria-trani', 'woocommerce' ), 'bl' => __( 'belluno', 'woocommerce' ), 'bn' => __( 'benevento', 'woocommerce' ), 'bg' => __( 'bergamo', 'woocommerce' ), 'bi' => __( 'biella', 'woocommerce' ), 'bo' => __( 'bologna', 'woocommerce' ), ); return $states; }
thanks
you can't restrict shipping classes themselves. country restriction maid shipping zones in witch can set specific shipping methods each of them. shipping classes allow manipulate rates in each "flat rate" shipping method.
finally can set shipping class product (or product variation), allow have custom shipping method rate chosen products.
instead can conditionally manipulate each shipping method set in shipping zones unique rate id using woocommerce_package_rates
filter hook:
add_filter( 'woocommerce_package_rates', 'manipulating_shipping_flat_rates', 10, 2 ); function custom_delivery_flat_rate_cost_calculation( $rates, $package ) { // initializing variables $has_products = false; $has_cat = false; // iterating through cart items foreach(wc()->cart->get_cart() $cart_item ): $product_id = $cart_item['product_id']; // product id $variation_id = $cart_item['variation_id']; // variation id // detecting targeted product category if( has_term( 'clothing', 'product_cat', $product_id ) ) $has_cat = true; // detecting specific product ids if ( in_array( $cart_item['product_id'], array(20, 22, 28, 47, 58)) ) $has_products = true; endforeach; foreach($rates $rate_key => $rate_values){ $method_id = $rate_values->method_id; $rate_id = $rate_values->id; // targetting flat rate shipping method if ( 'flat_rate' === $rate_values->method_id ) { ## 1) unsetting rate ids // product category if ( $has_cat ) { unset( $rates['flat_rate:5'] ); } // product ids if ( $has_products ) { unset( $rates['flat_rate:14'] ); } ## 2) manipulating specif rates ids prices // product category , specific rate id if ( $has_cat && 'flat_rate:8' == $rate_id ) { $rates[$rate_id]->cost = $rates[$rate_id]->cost * 0.1; $rates[$rate_id]->taxes[1] = $rates[$rate_id]->taxes[1] * 0.1; } // product ids , specific rate id if ( $has_products && 'flat_rate:11' == $rate_id ) { $rates[$rate_id]->cost = $rates[$rate_id]->cost * 0.1; $rates[$rate_id]->taxes[1] = $rates[$rate_id]->taxes[1] * 0.1; } } } return $rates; }
you need change code setting own product categories, product ids, shipping rates ids , rates costs, make working needs …
code goes in function.php file of active child theme (or theme) or in plugin file.
related answer: woocommerce: update custom fields after checkout validation failure
Comments
Post a Comment