Php WooCommerce管理员创建订单&;增加订单支付的运费
我正在管理中创建订单。我向发送给客户的订单支付页面(formpay.php)添加了一些功能。我添加了从订单中删除项目以及更新您的账单和发货信息(订单和帐户)的功能。我使用ajax并在删除项目后计算_totals(),这很有效 然而,我似乎不知道如何将装运应用于订单。我需要这种情况发生时,秩序是在管理中创建的,当有人删除了前端上的项目 我试着只是设置了运输后元,但没有工作Php WooCommerce管理员创建订单&;增加订单支付的运费,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我正在管理中创建订单。我向发送给客户的订单支付页面(formpay.php)添加了一些功能。我添加了从订单中删除项目以及更新您的账单和发货信息(订单和帐户)的功能。我使用ajax并在删除项目后计算_totals(),这很有效 然而,我似乎不知道如何将装运应用于订单。我需要这种情况发生时,秩序是在管理中创建的,当有人删除了前端上的项目 我试着只是设置了运输后元,但没有工作 function my_order_update_shipping($order_id, $items) {
function my_order_update_shipping($order_id, $items) {
$order = wc_get_order($order_id);
$order_subtotal = $order->get_subtotal();
if($order_subtotal > '17.99'){
update_post_meta($order_id, '_order_shipping', '0');
}else{
update_post_meta($order_id, '_order_shipping', '4');
}
}
add_action('woocommerce_before_save_order_items', 'my_order_update_shipping');
我怎样才能做到这一点?或者以这种方式应用运输方法?我终于明白了。工作正常,但在“管理”>“创建订单”中添加/删除项目时,需要单击“重新计算”按钮。以下代码基于静态小计金额应用2种装运方法中的1种
$delivery_zones = WC_Shipping_Zones::get_zones();
foreach ((array) $delivery_zones as $key => $the_zone) {
$shipping_methods = $the_zone['shipping_methods'];
}
// Apply Correct Shipping Method
if ($order_subtotal > '17.99') {
$rate = $shipping_methods[2];
$item = new WC_Order_Item_Shipping();
$item->set_props(array('method_id' => $rate->id, 'total' => wc_format_decimal($rate->cost)));
$order->add_item($item);
} else {
$rate = $shipping_methods[1];
$item = new WC_Order_Item_Shipping();
$item->set_props(array('method_id' => $rate->id, 'total' => wc_format_decimal($rate->cost)));
$order->add_item($item);
}
$order->calculate_totals();
$order->save();