限制WooCommerce中仅一个国家/地区的送货类别

时间:2017-07-28 08:23:47

标签: php wordpress woocommerce checkout shipping

我正在使用Woocommerce,我想限制运输类'酗酒者',只允许意大利而不是其他国家,你能帮我解决这个问题吗?非常感谢你的帮助!

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;
}

由于

3 个答案:

答案 0 :(得分:1)

您无法自行限制送货课程。国家限制是航运区的佣人,您可以为每个人设置具体的运输方式。运输类允许操纵每个"固定费率"邮寄方式。

最后,您可以为产品(或产品变体)设置运输类别,这样您就可以为所选产品设定自定义运费方式。

相反,您可以使用 woocommerce_package_rates 过滤器钩子,通过其唯一的费率ID 有条件地操纵您的送货区域中设置的每种送货方式:

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;
}
  

您需要更改代码设置您自己的产品类别,产品ID,运费率ID和费率,以使其适合您的需求...

代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。

相关答案:WooCommerce: update custom fields after checkout validation failure

答案 1 :(得分:0)

答案 2 :(得分:0)