Shipping costs and restrictions based on location and product category in WooCommerce

霸气de小男生 提交于 2020-01-24 21:54:27

问题


I'm using Woocommerce and I'd like to restrict the shipping class 'alcoholics' and allow it just for just Italy and no other countries, can you help me with this code? Thank you very much for your help!

function( $is_available ) {

$cart_items = WC()->cart->get_cart();

foreach ( $cart_items as $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


回答1:


You can't restrict Shipping Classes themselves. The country restriction is maid by the Shipping Zones in witch you can set specific shipping methods for each of them. Shipping classes allow to manipulate the rates in each "flat rate" shipping method.

Finally You can set a shipping class for a product (or a product variation), that will allow you to have a custom shipping method rate for the chosen products.

Instead you can conditionally manipulate each Shipping method set in your Shipping zones by its 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 all cart items
    foreach(WC()->cart->get_cart() as $cart_item ):

        $product_id = $cart_item['product_id']; // product ID
        $variation_id = $cart_item['variation_id']; // variation ID

        // Detecting a 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 as $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

            // for a product category
            if ( $has_cat ) {
                unset( $rates['flat_rate:5'] );
            }
            // for a product Ids
            if ( $has_products ) {
                unset( $rates['flat_rate:14'] );
            }

            ## 2) Manipulating specif Rates IDs prices  

            // for a product category and a 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;
            }
            // for a product Ids and a 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 will need to change the code setting your own product categories, product IDs, Shipping rates IDs and rates costs, to make it working for your needs …

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.


Related answer: WooCommerce: update custom fields after checkout validation failure




回答2:


for states: https://businessbloomer.com/woocommerce-limit-shipping-one-state/

and for states of some countries you can use :

https://docs.woocommerce.com/document/states-not-in-core/




回答3:


You could get this done using the following plugin: https://wordpress.org/plugins/advanced-shipping-validation-for-woocommerce



来源:https://stackoverflow.com/questions/45368456/shipping-costs-and-restrictions-based-on-location-and-product-category-in-woocom

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!