计算1个产品而非整个购物车的运费

时间:2018-05-23 09:53:25

标签: php wordpress woocommerce

我已经在单个产品的页面中安装了一个用于计算运费的插件。 插件在购物车中添加产品计算运费(整车+该产品),然后将其删除。

我想计算该产品的运费(应该是复制当前购物车,空购物车,在购物车中添加此产品计算运费,清洁购物车从副本中回滚购物车?) 任何人都有建议如何修改上面的代码,以便只计算当前的产品?

/* function for calculate shiiping */

    public function get_shipping_text($shipping_method, $country)
    {
        global $woocommerce, $post;
        $returnResponse = array();
        WC_Shortcode_Cart::calculate_shipping();


        if (isset($_POST["product_id"]) && $this->check_product_incart($_POST["product_id"]) === false) {
            $qty = (isset($_POST['current_qty']) && $_POST['current_qty'] > 0) ? $_POST['current_qty'] : 1;
            if (isset($_POST['variation_id']) && $_POST['variation_id'] != "" && $_POST['variation_id'] > 0) {
                $cart_item_key = WC()->cart->add_to_cart(sanitize_text_field($_POST["product_id"]), $qty, sanitize_text_field($_POST['variation_id']));
            } else {
                $cart_item_key = WC()->cart->add_to_cart(sanitize_text_field($_POST["product_id"]), $qty);
            }
            $packages = WC()->cart->get_shipping_packages();
            $packages = WC()->shipping->calculate_shipping($packages);
            $packages = WC()->shipping->get_packages();
            WC()->cart->remove_cart_item($cart_item_key);
        } else {
            $packages = WC()->cart->get_shipping_packages();
            $packages = WC()->shipping->calculate_shipping($packages);
            $packages = WC()->shipping->get_packages();
        }

        wc_clear_notices();

        if (isset($packages[0]["rates"][$shipping_method])) {
            $selectedShiiping = $packages[0]["rates"][$shipping_method];
            $returnResponse = array("label" => $selectedShiiping->label, "cost" => wc_price(($selectedShiiping->cost)*1.24));
        } else {
            $AllMethod = WC()->shipping->load_shipping_methods();
            $selectedMethod = $AllMethod[$shipping_method];
            $flag = 0;
            if ($selectedMethod->availability == "including"):
                foreach ($selectedMethod->countries as $methodcountry) {
                    if ($country == $methodcountry) {
                        $flag = 1;
                    }
                }
                if ($flag == 0):
                    $message = $selectedMethod->method_title . " is not available in selected country.";
                    $returnResponse = array("code" => "error", "message" => $message);
                endif;
            endif;
        }
        return $returnResponse;
    }

0 个答案:

没有答案