Woocommerce 中的自定义结帐 select 字段会更改动态运输方式
Dynamic shipping methods change by custom checkout select field in Woocommerce
我的 woocommerce
v3.3.5 项目有一个小问题。在结帐时,我添加了具有以下选项的自定义 select
:delivery by email
、delivery by post
.
我也创建了几种发送方式
3 for delivery by post
+1 for free by email
.
是否可以根据添加的自定义字段中的选项 select 动态更改显示的交付方式?
这是我的自定义 select 字段的代码片段
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
更新 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'));
}
然后以下代码将根据您的自定义 select 字段更改送货方式:
// 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 文件。已测试并有效。
我的 woocommerce
v3.3.5 项目有一个小问题。在结帐时,我添加了具有以下选项的自定义 select
:delivery by email
、delivery by post
.
我也创建了几种发送方式
3 for delivery by post
+1 for free by email
.
是否可以根据添加的自定义字段中的选项 select 动态更改显示的交付方式?
这是我的自定义 select 字段的代码片段
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
更新 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'));
}
然后以下代码将根据您的自定义 select 字段更改送货方式:
// 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 文件。已测试并有效。