Woocommerce试图将USPS排除在易燃产品之外

时间:2014-02-03 17:40:47

标签: php wordpress woocommerce usps

来自ASP.NET方面的WP新手。使用WP 3.8.1与USPS和来自woocommerce的FEDEX扩展我试图排除除了FEDEX地面费率之外的所有费用,因为它们出现在结帐页面上,因为这些产品易燃并且无法通过空运发送。我编写了一个函数并将其放在我的主题functions.php文件中。我不工作。也许我在add_filter中使用了错误的标签。

想出这个。所以我为那些需要它的人编辑了原始代码。

产品ID是硬编码的,需要进一步开发。可用的方法字符串很难,直到我认为我需要做一个print_r来暴露它们。取消注释该行以公开它们。它们特别适用于woocommerce USPS和FEDEX扩展,可能没有记录。

    // Hide USPS shipping methods when flammable items in cart 
    add_filter( 'woocommerce_available_shipping_methods', 'hide_usps_when_flammable_in_cart' , 1 );

    /** Hide usps shipping when flammable items in cart
        @param array $available_methods 
    */ 
    function hide_usps_when_flammable_in_cart( $available_methods ) {
         // printing the methods so I know the available methods to exclude
         // echo "<pre>" . print_r( $available_methods, true ) . "</pre>";
        global $woocommerce;
         // Get all products in cart
        $products = $woocommerce->cart->get_cart();
        //store product id's in array
        $volitile = array(170,191,192,194,196,199,201,204,206,208,209,210,212,214,217,262);

        // loop through the items looking for one in the ineligible array
        foreach ( $products as $key => $item ) {
            if( in_array( $item['product_id'], $volitile ) ) {
                unset($available_methods['usps:flat_rate_box_priority']);
                unset($available_methods['usps:flat_rate_box_express']);
                unset($available_methods['usps:D_PRIORITY_MAIL']);
                unset($available_methods['usps:D_STANDARD_POST']);
                unset($available_methods['fedex:FEDEX_EXPRESS_SAVER']);
                unset($available_methods['fedex:FEDEX_2_DAY_AM']);
                unset($available_methods['fedex:FIRST_OVERNIGHT']);
                unset($available_methods['fedex:PRIORITY_OVERNIGHT']);
                unset($available_methods['fedex:FEDEX_2_DAY']);
                unset($available_methods['fedex:STANDARD_OVERNIGHT']);
                break;
            }
        }

        return $available_methods;
    } 

0 个答案:

没有答案