WooCommerce隐藏基于高级类别的送货方式

时间:2016-03-13 21:34:24

标签: php wordpress woocommerce

我卖自行车和零件。如果有人在WooCommerce网上商店购买自行车,则只能看到送货方式'local_pickup'。如果有人购买零件,则只能提供“flat_rate”(运费)。如果有人购买了一个bicylce并且只有部分运输方法'local_pickup'应该可用。我应该如何使用新的代码段来解决这一挑战?

我有两个完美合作的片段,但只完成了一部分工作......

 // Hide 'Shipping' (flat_rate) with a bicylce in the shopping cart
add_filter( 'woocommerce_package_rates', 'hide_shipping_based_on_tag' ,    10, 1 );

function check_cart_for_share() {

// specify the category id's you want to hide flat_rate
$category_ID = array(
'7', // Type A bike
'8', // Type B bike
'9', // Type c bike
'10', // Type D bike

);
global $woocommerce;
$cart = $woocommerce->cart->cart_contents;

$found = false;

// loop through the array looking for the categories. Switch to true if the category is found.
foreach ($woocommerce->cart->cart_contents as $key => $values ) {
    $terms = get_the_terms( $values['product_id'], 'product_cat' );
    foreach ($terms as $term) {
        if( in_array( $term->term_id, $category_ID ) ) {

    $found = true;
    break;
}
}
}

return $found;

}

function hide_shipping_based_on_tag( $available_methods ) {

// use the function above to check the cart for the categories.
if ( check_cart_for_share() ) {

// remove the method you want
unset( $available_methods['flat_rate'] ); // Replace "flat_rate" with the shipping option that you want to remove.
  }

// return the available methods without the one you unset.
return $available_methods;

  }

 // Hide local ickup (local_pickup) with parts in shopping cart
 add_filter( 'woocommerce_package_rates', 'hide_shipping_based_on_tag' ,    10, 1 );

 function check_cart_for_share() {

// specify the category id's you want to hide flat_rate
$category_ID = array(
'11', // Parts

);
global $woocommerce;
$cart = $woocommerce->cart->cart_contents;

$found = false;

// loop through the array looking for the categories. Switch to true if the category is found.
foreach ($woocommerce->cart->cart_contents as $key => $values ) {
    $terms = get_the_terms( $values['product_id'], 'product_cat' );
    foreach ($terms as $term) {
        if( in_array( $term->term_id, $category_ID ) ) {

    $found = true;
    break;
}
}
}

 return $found;

 }

 function hide_shipping_based_on_tag( $available_methods ) {

// use the function above to check the cart for the categories.
if ( check_cart_for_share() ) {

// remove the method you want
unset( $available_methods['local_pickup'] ); // Replace "local_pickup" with the shipping option that you want to remove.
  }

// return the available methods without the one you unset.
return $available_methods;

   }

亲切的问候,

Sjors

0 个答案:

没有答案