我正在尝试添加一个输入字段,以允许客户(来宾)在“添加到购物车”之前在产品页面上输入他们的邮政编码,然后在篮子中设置/预填充邮政编码并结帐,而无需他们输入详细信息或登录/创建一个帐户。
我已经看到一些资源,说明购物篮页面上的运费计算器很难预填充,但是我不熟悉运费计算器的工作原理。这是真的?
根据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文件中。经过测试和工作。
注意:此“送货邮政编码”字段仅显示给访客。一旦“交货邮政编码”被提交一次,该字段将不再显示。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句