在 Woocommerce 中,我试图在费用金额为零时显示自定义费用,以使其出现在订单详细信息表中。
基于“根据 Woocommerce 结帐中的单选按钮动态更新费用”答案代码,我设法添加了应用于购物车的动态费用并更改客户交付选择。
这是我根据需要调整的工作代码:
add_action( 'woocommerce_cart_calculate_fees', 'add_delivery_fee', 20, 1 );
function add_delivery_fee( $cart ) {
$domain = 'woocommerce';
$NipostFST = '2000';
$NIPOSTSUB = '250';
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$packing_fee = WC()->session->get( 'chosen_delivery_option' ); // Dynamic delivery fee
$Dlabel = $packing_fee == 'home_delivery' ? __('To Your Doorstep', $domain) : __('Local Pickup', $domain);
$weight_of_item = WC()->cart->cart_contents_weight;
if ( $weight_of_item > 1 ) {
$nipostFee = $NipostFST + ( ($weight_of_item - 1)*$NIPOSTSUB );
}elseif ( $weight_of_item == 1 ) {
$nipostFee = $NipostFST;
}
$fee = (isset($weight_of_item)) ? $packing_fee == 'home_delivery' ? $nipostFee : 0.00 : 'Not Available';
$cart->add_fee( !is_cart() ? __( 'Delivery Fee [ '.$Dlabel.' ]', $domain ) : __( 'Delivery Fee', $domain ), $fee );
}
// Add a custom radio fields for Delivery Option selection
add_action( 'woocommerce_checkout_before_order_review', 'checkout_delivery_fee_addition', 20 );
function checkout_delivery_fee_addition(){
$domain = 'woocommerce';
$weight_of_item = WC()->cart->cart_contents_weight;
$NipostFST = '2000';
$NIPOSTSUB = '250';
if ( $weight_of_item > 1 ) {
$nipostFee = $NipostFST + ( ($weight_of_item - 1)*$NIPOSTSUB );
}elseif ( $weight_of_item == 1 ) {
$nipostFee = $NipostFST;
}
echo '<div id="izzycart_checkout_addons"><tr class="deliveryOption-select"><th>' . __('Delivery Method', $domain) . '</th><td>';
$chosen = WC()->session->get('chosen_delivery_option');
$chosen = empty($chosen) ? WC()->checkout->get_value('radio_delivery_option') : $chosen;
$chosen = empty($chosen) ? 'local_pickup' : $chosen;
// Add a custom checkbox field
woocommerce_form_field( 'radio_delivery_option', array(
'type' => 'radio',
'class' => array( 'form-row-wide delivery_option' ),
'options' => array(
'local_pickup' => __('Local Pickup '.wc_price(0.00), $domain),
'home_delivery' => (!isset($weight_of_item)) ? __('To Your Doorstep <br><span style="color:red">Not Available</span>', $domain) : __('To Your Doorstep '.wc_price($nipostFee), $domain),
),
'default' => $chosen,
), $chosen );
echo '</td></tr></div>';
}
现在,问题是,当客户选择本地取件交付方式自定义单选按钮时,应用的费用等于零,并且根本不显示在订单接收页面、我的帐户查看订单页面和所有电子邮件通知上。
那么如何像正常费用一样显示(在订单总计表上)应该如下所示:
因此显示的总项目行将类似于:
运费:N0.00