在WooCommerce Checkout页面中,我添加了一个下拉自定义域,其中有许多"Card type"选项.有些是免费的,有些是0.99美元(费用).在 checkout 页面上计算的费用是可以的,但当下订单时,费用不会添加到订单中(对于商业"卡类型"选项).
费用代码:
add_action('woocommerce_cart_calculate_fees', 'add_card_fee');
function add_card_fee() {
if (is_admin() && !defined('DOING_AJAX')) {
return; // Do not add the fee when in the admin area
}
if (is_checkout()) {
if (isset($_POST['post_data']) && !(strpos($_POST['post_data'], 'Bestwishes') !== false || strpos($_POST['post_data'], 'nocard') !== false || strpos($_POST['post_data'], 'happybirthday') !== false)) {
$extra_charge = 0.99;
WC()->cart->add_fee(__('Greeting Card Fee', 'woocommerce'), $extra_charge);
} else {
return;
}
}
}
下拉自定义域的代码(在签出中):
function add_custom_field_to_checkout($fields) {
$fields['shipping']['card_type'] = array(
'type' => 'select',
'label' => __('Greeting Card', 'woocommerce'),
'options' => array(
'' => __('Select an option', 'woocommerce'),
'nocard' => __('No Card', 'woocommerce'),
'Bestwishes' => __('Best Wishes (Free)', 'woocommerce'),
'congratulations' => __('Congratulations ($0.99)', 'woocommerce'),
'happybirthday' => __('Happy birthday (Free)', 'woocommerce'),
'happyanniversary' => __('Happy Anniversary ($0.99)', 'woocommerce'),
),
'required' => true,
'class' => array('form-row-wide'),
'clear' => true
);
return $fields;
}
在 checkout 页面上,一切看起来都很好,比如在订单总额中添加费用,以及从订单总额中删除费用.