首页 > 解决方案 > 订单页面列中的自定义结帐字段 | 电子商务

问题描述

我有一些自定义结帐字段,我想将其显示为列,我已经厌倦了它,但似乎只适用于国家、州、名字等标准结帐字段。

这是我用来创建自定义结帐字段的代码:

    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 );

    }

标签: wordpresswoocommerce

解决方案


推荐阅读