Php 在WooCommerce中首先选择提货或送货方式

Php 在WooCommerce中首先选择提货或送货方式,php,wordpress,session,woocommerce,shipping-method,Php,Wordpress,Session,Woocommerce,Shipping Method,我使用woocommerce制作网站的要求是,用户首先在第一页选择送货类型,无论是收货还是送货上门,然后它将进入购物页面。若用户在第一页选择取货,那个么在结账页我必须给出选择日期和时间的选项 我已经创建了这样的第一页 <form method="post"> <input name="postcode" type="text" /> <button name="shipping" value="delivery" type="submit">L

我使用woocommerce制作网站的要求是,用户首先在第一页选择送货类型,无论是收货还是送货上门,然后它将进入购物页面。若用户在第一页选择取货,那个么在结账页我必须给出选择日期和时间的选项

我已经创建了这样的第一页

<form method="post">
    <input name="postcode" type="text" />
    <button name="shipping" value="delivery" type="submit">Local delivery</button>
    <button name="shipping" value="pickup" type="submit">Local pickup</button>
</form>
当我尝试在结帐页面上使用此

$chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods' );
它显示空白数组


任何机构都可以就此提供建议或任何更好的解决方案。

我已轻轻更改了您的表格:

<?php
// Postcode validation (error message)
if( 'delivery' === WC()->session->get('chosen_shipping') && empty($_POST['postcode']) ) {
    ?>
    <div class="woocommerce"><ul class="woocommerce-error" role="alert"><ul>
        <li><?php _e('The postcode is a required field for "Local delivery"'); ?></li>
    </ul></div>
    <?php
}
?>
<form method="post">
    <label><?php _e("Postcode"); ?><br>
    <input id="input-postcode" name="postcode" type="text" />
    <button name="shipping_type" value="delivery" type="submit"><?php _e("Local delivery"); ?></button>
    <button name="shipping_type" value="pickup" type="submit"><?php _e("Local pickup"); ?></button>
</form>
代码进入活动子主题或活动主题的functions.php文件。测试和工作

对于邮政编码,您可以通过以下方式从WC会话变量中检索值:

$postcode = WC()->session->get( 'postcode_input' );
// Utility function with your shipping method settings
function get_shipping_rates_id_from_chosen( $chosen_shipping ) { 
    // For "pickup"
    if ( 'pickup' === $chosen_shipping ) {
        return 'local_pickup:13'; // <== Set your "Local pickup" shipping method rate ID
    } 
    // For delivery
    elseif ( 'delivery' === $chosen_shipping ) {
        return 'flat_rate:14'; // <== Set your "Flat rate" shipping method rate ID
    }
}
    
add_action('init', 'set_chosen_shipping_type_to_session');
function set_chosen_shipping_type_to_session() {
    if ( isset($_POST['shipping_type']) ) {
        // Early enable customer WC Session
        if ( isset(WC()->session) && ! WC()->session->has_session() ) {
            WC()->session->set_customer_session_cookie( true );
        }
        
        // Get the chosen shipping type
        if ( isset($_POST['shipping_type']) ) {
            // Set chosen shipping type in a session variable
            WC()->session->set('chosen_shipping', wc_clean($_POST['shipping_type']));
        }
        // Get the inputed postcode 
        if ( isset($_POST['postcode']) && ! empty($_POST['postcode']) ) {
            // Set chosen postcode in a session variable
            WC()->session->set('postcode_input', wc_clean($_POST['postcode']) );

            // Set customer shipping postcode
            WC()->customer->set_shipping_postcode( wc_clean($_POST['postcode']) );
        }
    }
}
// redirection after post | Set the chosen shipping method on cart and checkout
add_action('template_redirect', 'action_template_redirect');
function action_template_redirect($shipping) {
    // For "Local delivery" without postcode (no redirection)
    if( isset($_POST['shipping_type']) && 'delivery' === $_POST['shipping_type'] 
    && isset($_POST['postcode']) && empty($_POST['postcode']) ) {
        return;
    }
    // Redirection
    elseif( isset($_POST['shipping_type']) && WC()->session->get('chosen_shipping') ) {
        wp_redirect(home_url('/explore/'));
        exit();
    }
    // Set the chosen shipping method (in cart or checkout)
    elseif( is_cart() || ( is_checkout() && ! is_wc_endpoint_url( 'order-received' ) ) ) {
        $chosen_shipping  = WC()->session->get('chosen_shipping');
        // Get the 
        $shipping_rate_id = get_shipping_rates_id_from_chosen( $chosen_shipping );
        
        // Set for real the chosen shipping method
        WC()->session->set( 'chosen_shipping_methods', [$shipping_rate_id] );
    }
}
$postcode = WC()->session->get( 'postcode_input' );