【问题标题】:Woocommerce set shipping method, get shipping costWoocommerce 设置发货方式,获取运费
【发布时间】:2019-02-20 19:48:15
【问题描述】:

在我的functions.php 文件中,我需要能够设置运输方式(此站点只有一种),然后返回运输费用(设置了一些费用)。

我可以看到 flat_rate 使用这个运送:

foreach (WC()->shipping->get_shipping_methods() as $key => $value){
    echo $key;
}

所以它肯定存在。

基本上,我希望通过 API 调用返回运费。我已经路由了 API 并且所有这些都可以正常工作。它调用了我在某处捡到的这个函数,目前不记得了:

function pc_get_shipping(){
  $ret = array();
    foreach( WC()->session->get('shipping_for_package_0')['rates'] as $method_id => $rate ){
        if( WC()->session->get('chosen_shipping_methods')[0] == $method_id ){
            $rate_label = $rate->label; // The shipping method label name
            $rate_cost_excl_tax = floatval($rate->cost); // The cost excluding tax
            // The taxes cost
            $rate_taxes = 0;
            foreach ($rate->taxes as $rate_tax)
                $rate_taxes += floatval($rate_tax);
            // The cost including tax
            $rate_cost_incl_tax = $rate_cost_excl_tax + $rate_taxes;

            $ret[] = array('label' => $rate_label, 'total' => WC()->cart->get_cart_shipping_total());
        } 
    }
  return $ret;
}

但这给了我一个空数组,可能是因为WC()->session->get('shipping_for_package_0')['rates'] 的计算结果是一个空数组。

TL:DR

  • 来宾客户的送货信息通过WC()->customer->set_shipping_address_1(wc_clean($value)); 保存(所有值等)

  • 使用WC()->customer->get_shipping() 正确返回了来宾客户的送货信息,所以我相信它设置正确。

  • 发货方式flat_rate可通过WC()->shipping->get_shipping_methods()获得。

  • 如何在functions.php 中设置当前订单的运输方式,该方法将通过REST API 调用。

  • 如何在functions.php 中获取当前订单的计算运费,该方法将通过REST API 调用。

【问题讨论】:

  • 当您进行 API 调用时,您的订单是否存在?你什么时候做你的api调用(请问你用的是什么钩子)?
  • @LoicTheAztec 我不确定 - 我的购物车里有物品......这是否意味着它有?而且没有钩子——我构建了一个单页购物车,只有 API 端点可以连接。所以我正在从自定义 javascript 进行调用。我已经添加/删除/修改项目,购物车总计,存储客户账单/运输信息,甚至付款表格......只是还没有弄清楚运输
  • 好的所以我重新发布我删除的答案,看看它。希望对您有所帮助。
  • @LoicTheAztec 还没来得及尝试,会尽快更新
  • 嗨,你知道了吗?我也在寻找相同的代码,但没有运气。如果您有任何相关的答案,请提供帮助。

标签: php wordpress woocommerce


【解决方案1】:

首先在您的 API 响应中,您应该需要在 WC_Session 中将成本值设置为自定义数据,例如 (where$valueis the response cost value from your API):

if( ! WC()->session->__isset( 'shipping_cost' ) && ! empty($value) ){
    WC()->session->set( 'shipping_cost', $value );
}

您可能需要使用 jQuery ajax 刷新结帐页面:$('body').trigger('update_checkout');

然后你将使用这个钩子函数:

add_filter('woocommerce_package_rates', 'shipping_cost_based_on_api', 12, 2);
function shipping_cost_based_on_api( $rates, $package ){
    if( WC()->session->__isset( 'shipping_cost' ) ) {

        // Loop through the shipping taxes array
        foreach ( $rates as $rate_key => $rate ){
            $has_taxes = false;

            if( 'flat_rate' === $rate->method_id ){
                // Get the initial cost
                $initial_cost = $new_cost = $rates[$rate_key]->cost;

                // Get the new cost
                $new_cost = WC()->session->get( 'shipping_cost' );

                // Set the new cost
                $rates[$rate_key]->cost = $new_cost;

                // Taxes rate cost (if enabled)
                $taxes = [];
                // Loop through the shipping taxes array (as they can be many)
                foreach ($rates[$rate_key]->taxes as $key => $tax){
                    if( $rates[$rate_key]->taxes[$key] > 0 ){
                        // Get the initial tax cost
                        $initial_tax_cost = $new_tax_cost = $rates[$rate_key]->taxes[$key];
                        // Get the tax rate conversion
                        $tax_rate    = $initial_tax_cost / $initial_cost;
                        // Set the new tax cost
                        $taxes[$key] = $new_cost * $tax_rate;
                        $has_taxes   = true; // Enabling tax
                    }
                }
                if( $has_taxes )
                    $rates[$rate_key]->taxes = $taxes;
            }
        }
    }
    return $rates;
}

// Enabling, disabling and refreshing session shipping methods data
add_action( 'woocommerce_checkout_update_order_review', 'refresh_shipping_methods', 10, 1 );
function refresh_shipping_methods( $post_data ){
    $bool = true;
    if ( WC()->session->__isset('shipping_cost' ) ) $bool = false;

    // Mandatory to make it work with shipping methods
    foreach ( WC()->cart->get_shipping_packages() as $package_key => $package ){
        WC()->session->set( 'shipping_for_package_' . $package_key, $bool );
    }
    WC()->cart->calculate_shipping();
}

代码进入您的活动子主题(或活动主题)的 function.php 文件中。它应该可以工作。

基于:Remove shipping cost if custom checkbox is checked in WooCommerce Checkout

【讨论】:

    猜你喜欢
    • 2021-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-12
    • 1970-01-01
    • 2018-07-17
    • 2020-10-08
    • 2019-09-08
    相关资源
    最近更新 更多