我有一些要显示为列的自定义结帐字段,虽然很累,但似乎仅适用于标准结帐字段,如国家/地区,州/省,名字等。
这是我用来创建自定义结帐字段的代码:
add_action( 'woocommerce_before_order_notes', 'bbloomer_checkout_radio_choice');
function bbloomer_checkout_radio_choice() {
$chosen = WC()->session->get('radio_chosen');
$chosen = empty( $chosen ) ? WC()->checkout->get_value('radio_choice') : $chosen;
$chosen = empty( $chosen ) ? 'no_option' : $chosen;
$args = array(
'type' => 'radio',
'class' => array( 'form-row-wide' ),
'options' => array(
'option_1' => 'Thursday all day (£12.50)',
'option_2' => 'Thursday AM (£13.50)',
'option_3' => 'Friday AM (£13.50)',
),
'default' => $chosen
);
echo '<div id="checkout-radio">';
echo '<h3>Please Select Delivery Day</h3>';
echo '<p>"Our cut off point is 10am on Monday for orders to be received the same week. If you are placing your order later than this we will schedule your delivery for the following week."</p>';
woocommerce_form_field( 'radio_choice', $args, $chosen );
echo '</div>';
}
// Part 2
// Add Fee and Calculate Total
// Based on session's "radio_chosen"
#2 Calculate New Total
add_action( 'woocommerce_cart_calculate_fees', 'bbloomer_checkout_radio_choice_fee', 20, 1 );
function bbloomer_checkout_radio_choice_fee( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
$radio = WC()->session->get( 'radio_chosen' );
$fee = 12.5;
$deliveryDay = "Normal";
if ( "option_1" == $radio ) {
$fee = 12.5;
$deliveryDay = "Thursday all day";
} elseif ( "option_2" == $radio ) {
$fee = 13.5;
$deliveryDay = "Thursday AM";
} elseif ( "option_3" == $radio ) {
$fee = 13.5;
$deliveryDay = "Friday AM";
}
$cart->add_fee( __('Shipping ('. $deliveryDay. ')', 'woocommerce'), $fee );
}