2016-10-04 10 views
0

私はwoooforceでWooCommerce配送プラグインのカスタム関数を作成しようとしています。 2つのプラグイン、アップとuspsがあります。配送のためのWooCommerceカスタム関数

私が達成しようとしているのは、顧客がカテゴリXまたはそのサブカテゴリから製品を追加した場合、アップグレートを除くすべてのレートが設定されていない場合です。

これらの製品がカートに存在しない場合、すべてのオプションが利用可能なままです。

開発者は私にこのコードを指摘しましたが、PHPとWordPress/WooCommerceのPHPにはまだまだ新しくなっていますので、進め方としてちょっと固執しています。

ここで私が与えたコードです。

add_filter('woocommerce_package_rates', 'hide_shipping_method_when_shipping_class_product_is_not_in_cart', 10, 2); 

function hide_shipping_method_when_shipping_class_product_is_not_in_cart($available_shipping_methods, $package) 
{ 

    // Shipping class IDs that need the method removed 

    $shipping_class_ids = array(
     27, 
    ); 
    $shipping_services_to_hide = array(
     'wf_fedex_woocommerce_shipping:FEDEX_GROUND', 
       'wf_fedex_woocommerce_shipping:FEDEX_2_DAY_AM' 
    ); 
    $shipping_class_exists = false; 

    foreach(WC()->cart->cart_contents as $key => $values) { 
     if (in_array($values['data']->get_shipping_class_id() , $shipping_class_ids)) { 
      $shipping_class_exists = true; 
      break; 
     } 
    } 

    if (!$shipping_class_exists) { 
     foreach($shipping_services_to_hide as & $value) { 
      unset($available_shipping_methods[$value]); 
     } 
    } 

    return $available_shipping_methods; 
} 

答えて

0

私はこれを自分で調べることができました。将来、この機能が必要な人のためのコードです。 この機能はWooForce Shippingプラグインで特に機能します。

add_filter('woocommerce_package_rates', 'hide_shipping_method_when_shipping_class_product_is_not_in_cart', 10, 2); 
    function hide_shipping_method_when_shipping_class_product_is_not_in_cart($available_shipping_methods, $package){ 
    // set our flag to be false until we find a product in that category 
    $cat_check = false; 

    // check each cart item for our category 
    foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) { 

$product = $cart_item['data']; 

// replace 'membership' with your category's slug 
if (has_term('ammo', 'product_cat', $product->id)) { 
    $cat_check = true; 
    // break because we only need one "true" to matter here 
    break; 
} 
    } 

    // if a product in the cart is in our category, do something 
    if ($cat_check) { 
// we have the category, do what we want 
    // Shipping class IDs that need the method removed 
    $shipping_class_ids = array(
     1, 
     2, 
     3, 
     18, 
     23, 
     33, 
     47, 
     49 
    ); 
    $shipping_services_to_hide = array(
     'wf_shipping_usps:D_PRIORITY_MAIL', 
       'wf_shipping_usps:D_EXPRESS_MAIL' 
    ); 
    $shipping_class_exists = false; 
    foreach(WC()->cart->cart_contents as $key => $values) { 
     if (in_array($values['data']->get_shipping_class_id() , $shipping_class_ids)) { 
      $shipping_class_exists = true; 
      break; 
     } 
    } 
    if (!$shipping_class_exists) { 
     foreach($shipping_services_to_hide as & $value) { 
      unset($available_shipping_methods[$value]); 
     } 
    } 
    return $available_shipping_methods; 
} 
else{ 
    return $available_shipping_methods; 
} 
    } 
関連する問題