Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 计算1个产品的发货量,而不是整个购物车_Php_Wordpress_Woocommerce - Fatal编程技术网

Php 计算1个产品的发货量,而不是整个购物车

Php 计算1个产品的发货量,而不是整个购物车,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我在单个产品的页面上安装了一个用于计算运费的插件。 插件将产品添加到购物车中,计算运费(整个购物车+该产品),然后将其删除 我只想计算该产品的运输成本(应该类似于复制当前购物车、空购物车、将该产品添加到购物车计算运输、清洁购物车从副本回滚购物车?) 有人建议如何修改上面的代码,以便只计算当前产品吗 /* function for calculate shiiping */ public function get_shipping_text($shipping_method, $coun

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

我只想计算该产品的运输成本(应该类似于复制当前购物车、空购物车、将该产品添加到购物车计算运输、清洁购物车从副本回滚购物车?) 有人建议如何修改上面的代码,以便只计算当前产品吗

/* 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;
    }

清洁购物车…然后将其回滚以计算1个产品的装运,然后再次移除该产品???对我来说似乎是个疯狂的想法…应该想点更好的。。。例如,仅计算产品的装运价格。。没有将其添加到购物车。您有任何代码建议吗现在开车不。。。因为我不知道你使用的是哪种运输方式。。。。bcoz您只需要编写一个函数来计算该方法的发货量……但是您将很容易获得原始地址和用户地址。我需要修改当前代码,以便从$packages中删除购物车的产品并仅添加当前产品。用于计算的函数已存在。