【问题标题】:WooCommerce custom function for shipping用于运输的 WooCommerce 自定义功能
【发布时间】:2017-02-12 02:21:30
【问题描述】:

我正在尝试为 wooforce 的 WooCommerce 运输插件创建自定义功能。有两个插件,ups和usps。

我想要实现的是,如果客户从类别 X 或其任何子类别中添加产品,则除 ups ground 之外的所有费率都未设置。

如果购物车中没有这些产品,所有选项仍然可用。

开发人员向我指出了这段代码,但我对 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;
}

【问题讨论】:

    标签: wordpress plugins woocommerce


    【解决方案1】:

    我能够自己解决这个问题。这是将来可能需要此功能的任何人的代码。 此功能特别适用于 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;
    }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-21
      • 2022-11-02
      • 2018-03-16
      • 2021-03-12
      • 1970-01-01
      • 2014-03-11
      • 2018-05-18
      • 2021-06-07
      相关资源
      最近更新 更多