Php 通过Woocommerce中的自定义签出选择字段更改动态装运方法
我的Php 通过Woocommerce中的自定义签出选择字段更改动态装运方法,php,jquery,wordpress,woocommerce,shipping,Php,Jquery,Wordpress,Woocommerce,Shipping,我的woocommercev3.3.5项目有一个小问题。在我的结帐中,我添加了自定义选择,并使用以下选项:通过电子邮件投递,通过邮寄投递 我还创建了几种交付方法 3邮递服务 +1通过电子邮件免费发送 是否可以根据在添加的自定义字段中选择的选项动态更改显示的交付方法 下面是我的自定义选择字段的代码片段 add_action( 'woocommerce_checkout_before_billing', 'before_billing_fields', 20 ); function before_
woocommerce
v3.3.5项目有一个小问题。在我的结帐中,我添加了自定义选择,并使用以下选项:通过电子邮件投递
,通过邮寄投递
我还创建了几种交付方法
3邮递服务
+1通过电子邮件免费发送
是否可以根据在添加的自定义字段中选择的选项动态更改显示的交付方法
下面是我的自定义选择字段的代码片段
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));
}
add_action('woocommerce_checkout_before_billing','before_billing_fields',20);
计费字段()前的函数{
$checkout=WC()->checkout;
woocommerce\u表单\u字段('delivery\u method',数组(
'类型'=>'选择',
“选项”=>数组(
“blank”=>uuu(“选择传递方法”、“sdm”),
'邮递'=>\('邮递','传感和诊断模块'),
'通过电子邮件发送'=>\('通过电子邮件发送','传感和诊断模块')
),
'class'=>array('delivery\u method form row-wide'),
“清除”=>true
),$checkout->get_value('delivery_method');
}
添加操作('woocommerce\u checkout\u process'、'wps\u select\u checkout\u field\u process');
函数wps_选择_签出_字段_进程(){
全球商业;
如果($_POST['delivery\u method']==“blank”)
wc_添加_通知(“请选择一种交付方法”,“错误”);
}
添加过滤器(“woocommerce\u email\u order\u meta\u keys”、“wps\u select\u order\u meta\u keys”);
功能wps\u选择\u顺序\u元\u键($keys){
$keys['Delivery Method']='Delivery_Method';
返回$keys;
}
添加动作('woocmerce\u checkout\u update\u order\u meta','checkout\u update\u order\u meta');
函数签出\更新\订单\元($order\ id)
{
$delivery\u method=$\u POST['delivery\u method'];
如果(!空($delivery_method))
更新发布元数据($order\u id,'delivery\u method',清理文本字段($delivery\u method));
}
…运输方式:
更新2
首先,woocommerce\u checkout\u before\u billing
不存在……因此它可能是:
woocommerce\u checkout\u-before\u-customer\u-details
woocommerce\u checkout\u 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
}
//Jquery脚本
添加_操作('wp_footer','custom_checkout_script');
函数自定义检查脚本(){
//仅在结帐页面上
如果(!is_checkout()&&is_wc_endpoint_url('order received'))
返回;
?>
jQuery(函数($){
var a='选择#交付方法',
b='input[name^=“shipping_方法[0]”,
c='#装运方法0',
d='flat_rate9',//默认平面Id
e='免费送货10',//免费送货Id
f='免费送货:10';//免费送货费率Id
//现场活动事件:选择“交付方法”更改
$(a).更改(函数(){
如果($(this).val()=='通过电子邮件发送')
$(c+e).prop(“选中”,为真);
其他的
$(c+d).prop(“选中”,为真);
$(document.body).trigger('update_checkout');
});
//现场活动事件:装运方法更改时
$('form.checkout')。在('change',b,function()上{
//如果未选择免费送货,我们将“送货方式”字段更改为“邮寄”
if($(this).val()!=f)
$(a).val(‘邮递’);
其他的
$(a).val(“通过电子邮件发送”);
});
});
是的,这是可能给我的-这是我在这里的第一个问题。除此之外,它是一个标准的wordpress+woocommerce 3.3.5应用程序。目前,我在本地添加了这个项目。我添加了live链接。邮件传递应该只显示“标准”和“快递”,而电子邮件应该只显示免费邮件传递。@RadekOlejniczak…Y你不能在live browser事件中隐藏发送方法…相反,我下面回答中的jquery代码将管理更改…试试看。@RadekOlejniczak我刚刚稍微更新了我的代码…谢谢你的回复。我考虑过将其隐藏在jquery中的可能性,但正如你在之前的声明中所写的那样-它只是将其隐藏,而不是tu关闭它,有人将能够打破它,并发现隐藏的选项。我想知道是否有其他方法可以实现它,可能是使用AJAX。非常感谢您的代码!它干净清晰。我感谢您的时间:)@RadekOlejniczak欢迎您……您可以做的是问一个新问题,问同样的问题在这个问题上,你可以包括你的实际代码(这是一个类似的代码)和所有必要的细节(运费ID和网站链接)。我会尝试找到一种方法,用更多的时间,因为这是一种先进和复杂的。所以标记你的问题Ajax。一旦完成,请在这里通知我。谢谢