如何在没有(已填充)购物车

时间:2017-03-13 12:24:49

标签: php wordpress woocommerce

是否可以根据
获得可用的运输方法列表 只有总金额和/或国家/地区代码?

我认为可以通过构建自己的SQL查询来实现,但我想知道是否也可以使用标准的Woo-commerce功能。

我已尝试使用该功能:

$shipping_methods = WC()->shipping->get_shipping_methods(), 

但是这给了我所有的运输方法,而不过滤国家或费用。 例如: local_pickup看起来像

 [local_pickup] => WC_Shipping_Local_Pickup Object
    (
        [supports] => Array
            (
                [0] => shipping-zones
                [1] => instance-settings
                [2] => instance-settings-modal
            )

        [id] => local_pickup
        [method_title] => Afhalen
        [method_description] => Sta klanten toe bestellingen zelf op te halen. Standaard worden winkelgebaseerde belastingen toegepast wanneer gekozen is voor lokaal ophalen, onafhankelijk van het adres van de klant.
        [enabled] => yes
        [title] => 
        [rates] => Array
            (
            )

        [tax_status] => 
        [fee] => 
        [minimum_fee] => 
        [instance_id] => 0
        [instance_form_fields] => Array
            (
                [title] => Array
                    (
                        [title] => Titel
                        [type] => text
                        [description] => Dit bepaalt de titel die de gebruiker ziet tijdens het afrekenen.
                        [default] => Afhalen
                        [desc_tip] => 1
                    )

                [tax_status] => Array
                    (
                        [title] => Belastingstatus
                        [type] => select
                        [class] => wc-enhanced-select
                        [default] => taxable
                        [options] => Array
                            (
                                [taxable] => Belastbaar
                                [none] => Geen
                            )

                    )

                [cost] => Array
                    (
                        [title] => Kosten
                        [type] => text
                        [placeholder] => 0
                        [description] => Optionele kosten voor afhalen.
                        [default] => 
                        [desc_tip] => 1
                    )

            )

        [instance_settings] => Array
            (
            )

        [availability] => 
        [countries] => Array
            (
            )

        [plugin_id] => woocommerce_
        [errors] => Array
            (
            )

        [settings] => Array
            (
                [title] => 
                [tax_status] => 
                [cost] => 
            )

        [form_fields] => Array
            (
            )

        [data:protected] => Array
            (
            )

        [cost] => 
    )

接下来我尝试了以下代码,但我根本没有得到任何结果。

print_r ( WC()->shipping->calculate_shipping( get_shipping_packages()));
die('ready');

function get_shipping_packages(){
    global $wpdb, $woocommerce;

    // Clear the Cart
    $woocommerce->cart->empty_cart(); 

   // Add an existing product to the cart, so $packages[0]['contents'] will not be empty... 
    $product_id = 26;
    WC()->cart->add_to_cart($product_id);

    $packages = array();
    $packages[0]['contents']                = WC()->cart->cart_contents;
    $packages[0]['contents_cost']           = 25;
    $packages[0]['destination']['country']  = 'NL';
    $packages[0]['destination']['state']    = null;
    $packages[0]['destination']['postcode'] = null;
    $packages[0]['destination']['city']     = null;
    $packages[0]['destination']['address']  = null;

    return ($packages);
}

2 个答案:

答案 0 :(得分:1)

最后,我找到了一种获取运输方式的方法。

这是我的解决方案,欢迎反馈..

function shipping() {

    global $woocommerce;

    $active_methods   = array();
    $values = array ('country' => 'NL',
                     'amount'  => 100);


    // Fake product number to get a filled card....
    $woocommerce->cart->add_to_cart('1');

    WC()->shipping->calculate_shipping(get_shipping_packages($values));
    $shipping_methods = WC()->shipping->packages;

    foreach ($shipping_methods[0]['rates'] as $id => $shipping_method) {
        $active_methods[] = array(  'id'        => $shipping_method->method_id,
                                    'type'      => $shipping_method->method_id,
                                    'provider'  => $shipping_method->method_id,
                                    'name'      => $shipping_method->label,
                                    'price'     => number_format($shipping_method->cost, 2, '.', ''));
    }
    return $active_methods;
}


function get_shipping_packages($value) {

    // Packages array for storing 'carts'
    $packages = array();
    $packages[0]['contents']                = WC()->cart->cart_contents;
    $packages[0]['contents_cost']           = $value['amount'];
    $packages[0]['applied_coupons']         = WC()->session->applied_coupon;
    $packages[0]['destination']['country']  = $value['countries'];
    $packages[0]['destination']['state']    = '';
    $packages[0]['destination']['postcode'] = '';
    $packages[0]['destination']['city']     = '';
    $packages[0]['destination']['address']  = '';
    $packages[0]['destination']['address_2']= '';


    return apply_filters('woocommerce_cart_shipping_packages', $packages);
}

答案 1 :(得分:1)

我能够通过 WC_Shipping_Zones 课程完成这项工作。

$zones = WC_Shipping_Zones::get_zones();
$methods = array_map(function($zone) {
    return $zone['shipping_methods'];
}, $zones);