Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从客户的产品自定义输入字段设置电子商务帐单邮政编码_Php_Wordpress_Woocommerce - Fatal编程技术网

Php 从客户的产品自定义输入字段设置电子商务帐单邮政编码

Php 从客户的产品自定义输入字段设置电子商务帐单邮政编码,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我正在尝试添加一个输入字段,允许客户(来宾)在“添加到购物车”之前在产品页面上输入他们的邮政编码,然后在购物篮和结帐中设置/预填充邮政编码,而无需他们输入详细信息或登录/创建帐户 我看到一些参考资料说,篮子页面上的装运计算器很难预填充,但我不熟悉装运计算器的工作原理。这是真的吗 根据回答代码,我成功地实现了使用以下代码将邮政编码从产品页面传递到结帐页面,允许客户将其邮政编码添加到产品页面,结帐正确显示客户邮政编码,并正确显示运费: // Add a custom product note aft

我正在尝试添加一个输入字段,允许客户(来宾)在“添加到购物车”之前在产品页面上输入他们的邮政编码,然后在购物篮和结帐中设置/预填充邮政编码,而无需他们输入详细信息或登录/创建帐户

我看到一些参考资料说,篮子页面上的装运计算器很难预填充,但我不熟悉装运计算器的工作原理。这是真的吗

根据回答代码,我成功地实现了使用以下代码将邮政编码从产品页面传递到结帐页面,允许客户将其邮政编码添加到产品页面,结帐正确显示客户邮政编码,并正确显示运费:

// 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 
} 
}
//在单个产品页面的“添加到购物车”按钮后添加自定义产品说明
添加操作(“添加到购物车按钮后的woocommerce”、“自定义字段”、“交货”、“邮政编码”,10);
功能自定义\字段\交付\邮政编码(){
woocommerce\u表单\u字段('邮政编码\u注释',数组(
'类型'=>'文本',
'class'=>数组('my field class form row-wide'),
“标签”=>“(“邮政编码”),
“占位符”=>(输入您的服务邮政编码…),
“必需”=>true,
) , '');
}
//将客户注释添加到购物车项目数据
添加过滤器('woocommerce\u添加购物车\u项目\u数据','add\u delivery\u postcode\u to\u cart\u项目\u数据',20,2);
功能将配送地址邮政编码添加到购物车项目数据($cart\u item\u data,$product\u id){
如果(isset($_POST['postcode\u note'])和&!空($_POST['postcode\u note'])){
$postcode\u note=sanitize\u textarea\u字段($\u POST['postcode\u note']);
$cart\u item\u data['postcode\u note']=$postcode\u note;
}
返回$cart\u item\u数据;
}
//如果用户未登录,请设置客户邮政编码,否则请设置保存的客户邮政编码
添加行动(“在结帐表之后添加商业”、“sab客户结帐”邮政编码);
功能sab_客人_结帐_邮政编码(){
$isLoggedIn=是否已登录用户();
if(false==$isLoggedIn){
?>
jQuery(函数($){
$(“#账单(邮政编码”).val(“”)//在此处输入邮政编码
});

您不需要任何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'] ) );
    }
}
//在单个产品页面的“添加到购物车”按钮后添加自定义产品说明
添加操作(“添加到购物车按钮后的woocommerce”,添加送货地址,邮政编码字段”,10);
函数添加\交付\邮政编码\字段(){
如果(用户登录了吗?)返回;//退出
$postcode=WC()->customer->get_shipping_postcode();
$postcode=空($postcode)?WC()->customer->get_billing_postcode():$postcode;
if(空($postcode)){
回声';
woocommerce\u表单\u字段('delivery\u postcode',数组(
'类型'=>'文本',
'class'=>数组('my field class form row-wide'),
“标签”=>“(“邮政编码”),
'占位符'=>'('输入您的送货邮政编码…'),
“必需”=>true,
) , '');
回声';
}
}
//设置提交的邮政编码
添加_操作('init','set_delivery_postcode');
功能集\交付\邮政编码(){
如果(isset($\u POST['delivery\u postcode'])和&!空($\u POST['delivery\u postcode'])){
WC()->customer->set_billing_postcode(清理_text_字段($_POST['delivery_postcode'));
WC()->customer->set_shipping_postcode(清理_text_字段($_POST['delivery_postcode']);
}
}
代码放在活动子主题(或活动主题)的functions.php文件中。经过测试,可以正常工作


注意:此“配送邮政编码”字段仅显示给客人。一旦“配送邮政编码”ben提交过一次后,该字段不再显示。

客户可以向购物车添加更多具有不同邮政编码的产品吗?抱歉@LoicTheAztec您是对的,我应该将其包括在内。已更新。@VincenzoDiGaetano否,交货邮政编码将保持不变。客户一次只能签出一个产品。谢谢。只需在这一点上观察到…现在我有了处理结帐页面的代码,我注意到如果我进入结帐页面并返回购物车/购物篮,发货计算器已经更新以显示客人的邮政编码。谢谢,这是一个更干净的方法。但是有一个小问题…我一开始不能像以前那样让它工作“添加成功后重定向到篮框页面”在WooCommerce中打开。如果我关闭此选项,代码工作正常。您能确认相同的问题吗?这可能是我从一开始就遇到困难的原因。无论此选项处于打开或关闭状态,是否有方法使其工作正常?我将尝试代码lat呃,今天来看看我能找到什么。谢谢。谢谢。是的,我今天早上早些时候尝试了这个,发现不管重定向选项如何,它都能工作。“Init”绝对是避免任何问题的正确方法。再次感谢您的输入和支持。