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

时间:2018-05-28 12:37:51

标签: php jquery wordpress woocommerce shipping

我的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 :(得分:0)

更新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文件中。经过测试和工作。