首页 文章

通过Woocommerce中的自定义结帐选择字段更改动态送货方式

提问于
浏览
1

我的 woocommerce v3.3.5项目有一个小问题 . 在我的结帐时,我添加了自定义 select 以及以下选项: delivery by emaildelivery by post .

我还创建了几种交付方法

3 for delivery by post

+1 for free by email .

是否可以根据添加的自定义字段中选择的选项动态更改显示的交付方式?

这是我的自定义选择字段的代码片段

add_action( 'woocommerce_checkout_before_billing', 'before_billing_fields', 20 );

function before_billing_fields(){
    $checkout = WC()->checkout;

    woocommerce_form_field('delivery_method', array(
        'type' => 'select',
        'options'       => array(
            'blank'     => __( 'Select a delivery method', 'sdm' ),
            'shipping-by-post'  => __( 'Shipping by post', 'sdm' ),
            'shipping-by-email' => __( 'Shipping by email', 'sdm' )
        ),
        'class' => array('delivery_method form-row-wide'),
        'clear'     => true
    ), $checkout->get_value('delivery_method'));
}

add_action('woocommerce_checkout_process', 'wps_select_checkout_field_process');
function wps_select_checkout_field_process() {
    global $woocommerce;

    if ($_POST['delivery_method'] == "blank")
        wc_add_notice( '<strong>Please select a Delivery method</strong>', 'error' );

}

add_filter('woocommerce_email_order_meta_keys', 'wps_select_order_meta_keys');
function wps_select_order_meta_keys( $keys ) {
    $keys['Delivery Method'] = 'delivery_method';
    return $keys;
}

add_action('woocommerce_checkout_update_order_meta', 'checkout_update_order_meta');
function checkout_update_order_meta($order_id)
{

    $delivery_method = $_POST['delivery_method'];
    if (!empty($delivery_method))
        update_post_meta($order_id, 'delivery_method', sanitize_text_field($delivery_method));

}

......和运输方式:Woocommerce Shipping Methods

1 回答

  • 0

    Update 2

    首先, woocommerce_checkout_before_billing 不存在......所以可能是:

    • woocommerce_checkout_before_customer_details

    • woocommerce_checkout_billing .

    然后我用正确的钩子改变了你的第一个钩子函数,因为我们还需要在加载时设置默认的送货方法:

    add_action( 'woocommerce_checkout_billing', 'before_billing_fields', 5 );
    function before_billing_fields(){
        $checkout = WC()->checkout;
    
        woocommerce_form_field('delivery_method', array(
            'type' => 'select',
            'options'       => array(
                'blank'     => __( 'Select a delivery method', 'sdm' ),
                'shipping-by-post'  => __( 'Shipping by post', 'sdm' ),
                'shipping-by-email' => __( 'Shipping by email (evoucher)', 'sdm' )
            ),
            'class' => array('delivery_method form-row-wide'),
            'clear'     => true
        ), $checkout->get_value('delivery_method'));
    
        // Set the default shipping method on load: "Standard" flat rate
        WC()->session->set('chosen_shipping_methods', array('flat_rate:9'));
    }
    

    然后,以下代码将使用您的自定义选择字段更改送货方式:

    // The Jquery script
    add_action( 'wp_footer', 'custom_checkout_script' );
    function custom_checkout_script() {
        // Only on checkout page
        if( ! is_checkout() && is_wc_endpoint_url( 'order-received' ) )
            return;
        ?>
        <script type="text/javascript">
            jQuery( function($){
                var a = 'select#delivery_method',
                    b = 'input[name^="shipping_method[0]"]',
                    c = '#shipping_method_0_',
                    d = 'flat_rate9', // Default flat Id
                    e = 'free_shipping10', // Free shipping Id
                    f = 'free_shipping:10'; // Free shipping rate Id
    
                // Live action event: On Select "delivery_method" change
                $(a).change( function () {
                    if($(this).val() == 'shipping-by-email' )
                        $(c+e).prop("checked", true);
                    else
                        $(c+d).prop("checked", true);
                    $( document.body ).trigger( 'update_checkout' );
                });
    
                // Live action event: On Shipping method change
                $( 'form.checkout' ).on( 'change', b, function() {
                    // If Free shipping is not selected, we change "delivery_method" slect field to "post"
                    if( $(this).val() != f )
                        $(a).val('shipping-by-post');
                    else
                        $(a).val('shipping-by-email');
                });
            });
        </script>
        <?php
    }
    

    代码位于活动子主题(或活动主题)的function.php文件中 . 经过测试和工作 .

相关问题