[英]Set postcode from a field for guests and customers in WooCommerce
[英]Set WooCommerce billing postcode from product custom input field for guests
我正在尝试添加一个输入字段,允许客户(客人)在“添加到购物车”之前在产品页面上输入他们的邮政编码,然后在购物篮和结帐中设置/预填充邮政编码,而无需输入他们的详细信息或登录/创建一个帐户。
我看到一些资源说购物篮页面上的运费计算器预填充很复杂,但我不熟悉运费计算器的工作原理。 这是真的?
基于在 Woocommerce 答案代码中的单个产品页面中添加产品注释字段,我已经成功地使用以下代码实现了将邮政编码从产品页面传递到结帐页面,该代码允许客人将他们的邮政编码添加到产品页面,并且结帐正确显示了客人邮政编码,并正确显示了运费:
// Add a custom product note after add to cart button in single product pages
add_action('woocommerce_after_add_to_cart_button', 'custom_field_delivery_postcode', 10 );
function custom_field_delivery_postcode() {
woocommerce_form_field('postcode_note', array(
'type' => 'text',
'class' => array( 'my-field-class form-row-wide') ,
'label' => __('Postcode') ,
'placeholder' => __('Enter your service postcode...') ,
'required' => true,
) , '');
}
// Add customer note to cart item data
add_filter( 'woocommerce_add_cart_item_data', 'add_delivery_postcode_to_cart_item_data', 20, 2 );
function add_delivery_postcode_to_cart_item_data( $cart_item_data, $product_id ){
if( isset($_POST['postcode_note']) && ! empty($_POST['postcode_note']) ){
$postcode_note = sanitize_textarea_field( $_POST['postcode_note'] );
$cart_item_data['postcode_note'] = $postcode_note;
}
return $cart_item_data;
}
// If user not logged in, set the guest postcode, otherwise whow saved customer postcode
add_action('woocommerce_after_checkout_form','sab_guest_checkout_postcode');
function sab_guest_checkout_postcode(){
$isLoggedIn = is_user_logged_in();
if(false == $isLoggedIn){
?>
<script type="text/javascript">
jQuery(function($){
$('#billing_postcode').val('<?php
foreach( WC()->cart->get_cart() as $cart_item ){
if( isset($cart_item['postcode_note']) )
echo $cart_item['postcode_note'];
} ?>'); //enter postcode here
});
</script>
<?php
}
}
虽然这适用于结帐页面,但是否有人知道如何使用购物车/篮子运输计算器实现此功能?
您不需要任何 Javascript 代码,也不需要将该“送货邮政编码”设置为自定义购物车商品数据。 只需使用以下简化的代码替换,这将允许客人从产品页面设置他们的“送货邮政编码”:
编码
// Add a custom product note after add to cart button in single product pages
add_action('woocommerce_after_add_to_cart_button', 'add_delivery_postcode_field', 10 );
function add_delivery_postcode_field() {
if( is_user_logged_in() ) return; // Exit
$postcode = WC()->customer->get_shipping_postcode();
$postcode = empty($postcode) ? WC()->customer->get_billing_postcode() : $postcode;
if ( empty($postcode) ) {
echo '<div class="postcode-field" style="margin-top:12px;">';
woocommerce_form_field('delivery_postcode', array(
'type' => 'text',
'class' => array( 'my-field-class form-row-wide') ,
'label' => __('Postcode') ,
'placeholder' => __('Enter your delivery postcode...') ,
'required' => true,
) , '');
echo '</div>';
}
}
// Set submitted postcode
add_action( 'init', 'set_delivery_postcode' );
function set_delivery_postcode() {
if( isset($_POST['delivery_postcode']) && ! empty($_POST['delivery_postcode']) ) {
WC()->customer->set_billing_postcode( sanitize_text_field( $_POST['delivery_postcode'] ) );
WC()->customer->set_shipping_postcode( sanitize_text_field( $_POST['delivery_postcode'] ) );
}
}
代码进入活动子主题(或活动主题)的functions.php文件。 测试和工作。
注意:此“送货邮政编码”字段仅对客人显示。 一旦“送货邮政编码”已提交一次,该字段将不再显示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.