Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/237.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 Woocommerce签出页面中的Shipping carrier自定义字段验证_Php_Jquery_Wordpress_Woocommerce_Shipping Method - Fatal编程技术网

Php Woocommerce签出页面中的Shipping carrier自定义字段验证

Php Woocommerce签出页面中的Shipping carrier自定义字段验证,php,jquery,wordpress,woocommerce,shipping-method,Php,Jquery,Wordpress,Woocommerce,Shipping Method,通过更改模板/genesis-sample/woocmerce/checkout/review-order.php,我在shipping-method部分添加了两个自定义输入字段 我还设法使他们有条件地得到要求。仅当选中特定单选按钮时,输入字段才会出现并成为必需字段。我使用jQuery代码使字段出现和消失。所有这些都很好。代码如下: if ( isset($_POST['shipping_method_0_legacy_local_pickup']) && $_POST['shi

通过更改模板/genesis-sample/woocmerce/checkout/review-order.php,我在shipping-method部分添加了两个自定义输入字段 我还设法使他们有条件地得到要求。仅当选中特定单选按钮时,输入字段才会出现并成为必需字段。我使用jQuery代码使字段出现和消失。所有这些都很好。代码如下:

if ( isset($_POST['shipping_method_0_legacy_local_pickup']) && $_POST['shipping_method_0_legacy_local_pickup'] == 1 ) {
    $cheq = 'true';
}
else {
    $cheq = 'false';
}
woocommerce_form_field( 'fieldtester1' , array(
    'type'          => 'text',
    'class'         => array('wccs-field-class wccs-form-row-wide blahblah1'), 
    'label'         => 'Enter Your Carrier Name',
    'required'  => $cheq,
    'placeholder'       => 'Carrier Name',
    ), $checkout->get_value( 'fieldtester1' )); 
woocommerce_form_field( 'fieldtester2' , array(
    'type'          => 'text',
    'class'         => array('wccs-field-class wccs-form-row-wide blahblah2'), 
    'label'         => 'Enter Your Carrier Account #',
    'required'  => $cheq,
    'placeholder'       => 'Carrier Number',
    ), $checkout->get_value( 'fieldtester2' )); 
add_action('woocommerce_checkout_process', 'carrier_checkout_process');
function carrier_checkout_process() {
    if ( isset($_POST['shipping_method_0_legacy_local_pickup']) && $_POST['shipping_method_0_legacy_local_pickup'] == 1 ) {
        if( empty( $_POST['fieldtester1'] ) ) {
            wc_add_notice( ( "Please don't forget to enter your shipping carrier details." ), "error" );
        }
    }
}
但问题是,即使将这两个字段的required属性设置为true,如果按下placeorder按钮时字段为空,也不会进行验证。理想情况下,订单不应通过,并应生成错误消息。我已经在functions.php中添加了以下代码来强制验证输入字段,但它没有做任何事情。代码如下:

if ( isset($_POST['shipping_method_0_legacy_local_pickup']) && $_POST['shipping_method_0_legacy_local_pickup'] == 1 ) {
    $cheq = 'true';
}
else {
    $cheq = 'false';
}
woocommerce_form_field( 'fieldtester1' , array(
    'type'          => 'text',
    'class'         => array('wccs-field-class wccs-form-row-wide blahblah1'), 
    'label'         => 'Enter Your Carrier Name',
    'required'  => $cheq,
    'placeholder'       => 'Carrier Name',
    ), $checkout->get_value( 'fieldtester1' )); 
woocommerce_form_field( 'fieldtester2' , array(
    'type'          => 'text',
    'class'         => array('wccs-field-class wccs-form-row-wide blahblah2'), 
    'label'         => 'Enter Your Carrier Account #',
    'required'  => $cheq,
    'placeholder'       => 'Carrier Number',
    ), $checkout->get_value( 'fieldtester2' )); 
add_action('woocommerce_checkout_process', 'carrier_checkout_process');
function carrier_checkout_process() {
    if ( isset($_POST['shipping_method_0_legacy_local_pickup']) && $_POST['shipping_method_0_legacy_local_pickup'] == 1 ) {
        if( empty( $_POST['fieldtester1'] ) ) {
            wc_add_notice( ( "Please don't forget to enter your shipping carrier details." ), "error" );
        }
    }
}
所以,以下是我想要的:

有人能帮我强制验证两个输入字段吗 你又加了什么? 在验证之后,我还想在新订单电子邮件中添加这两个字段,并在Wordpress仪表板中添加订单详细信息。 如果您想知道为什么我没有使用woocommerce签出字段挂钩来添加输入字段,那么我没有将字段添加到签出表单中,因此挂钩没有任何帮助。这些字段将添加到shipping method部分。另一个原因是,每当用户切换配送方式时,我都想更新所需的属性。无论出于何种原因,使用Jquery更改所需属性都不起作用,相信我,我已经尝试了两天。不管怎么说,这部分已经起作用了。唯一的问题是获取要验证的字段,并将其添加到订单电子邮件和订单详细信息中。我到处找了找,我得到的最贴切的帮助是这篇文章。这可以在没有jQuery的情况下完成,使用一个特殊的钩子,在选择传统的本地皮卡运送方法时,它会在下面显示两个承运人自定义字段。如果客户更改为其他装运方式,这些字段将被删除

因此,您应该需要删除您的自定义模板和所有相关的代码之前

现在验证工作非常完美,当Carrier自定义字段被填充时,它们会按顺序保存元数据

// Add custom fields to a specific selected shipping method
add_action( 'woocommerce_after_shipping_rate', 'carrier_custom_fields', 20, 2 );
function carrier_custom_fields( $method, $index ) {
    if( ! is_checkout()) return; // Only on checkout page

    $customer_carrier_method = 'legacy_local_pickup';

    if( $method->id != $customer_carrier_method ) return; // Only display for "local_pickup"

    $chosen_method_id = WC()->session->chosen_shipping_methods[ $index ];

    // If the chosen shipping method is 'legacy_local_pickup' we display
    if($chosen_method_id == $customer_carrier_method ):

    echo '<div class="custom-carrier">';

    woocommerce_form_field( 'carrier_name' , array(
        'type'          => 'text',
        'class'         => array('form-row-wide carrier-name'),
        'label'         => 'Carrier Information:',
        'required'      => true,
        'placeholder'   => 'Carrier Name',
    ), WC()->checkout->get_value( 'carrier_name' ));

    woocommerce_form_field( 'carrier_number' , array(
        'type'          => 'text',
        'class'         => array('form-row-wide carrier-number'),
        'required'      => true,
        'placeholder'   => 'Carrier Number',
    ), WC()->checkout->get_value( 'carrier_number' ));

    echo '</div>';
    endif;
}

// Check custom fields validation
add_action('woocommerce_checkout_process', 'carrier_checkout_process');
function carrier_checkout_process() {
    if( isset( $_POST['carrier_name'] ) && empty( $_POST['carrier_name'] ) )
        wc_add_notice( ( "Please don't forget to enter the shipping carrier name." ), "error" );

    if( isset( $_POST['carrier_number'] ) && empty( $_POST['carrier_number'] ) )
        wc_add_notice( ( "Please don't forget to enter the shipping carrier account number." ), "error" );
}

// Save custom fields to order meta data
add_action( 'woocommerce_checkout_update_order_meta', 'carrier_update_order_meta', 30, 1 );
function carrier_update_order_meta( $order_id ) {
    if( isset( $_POST['carrier_name'] ))
        update_post_meta( $order_id, '_carrier_name', sanitize_text_field( $_POST['carrier_name'] ) );

    if( isset( $_POST['carrier_number'] ))
        update_post_meta( $order_id, '_carrier_number', sanitize_text_field( $_POST['carrier_number'] ) );
}
此代码位于活动子主题或主题的functions.php文件中。测试和工作

未显示:

选择本地拾取时显示:

这可以在不使用jQuery的情况下使用一个特殊的钩子来完成,当选中该钩子时,它将在legacy_local_Pick shipping method下面显示两个Carrier自定义字段。如果客户更改为其他装运方式,这些字段将被删除

因此,您应该需要删除您的自定义模板和所有相关的代码之前

现在验证工作非常完美,当Carrier自定义字段被填充时,它们会按顺序保存元数据

// Add custom fields to a specific selected shipping method
add_action( 'woocommerce_after_shipping_rate', 'carrier_custom_fields', 20, 2 );
function carrier_custom_fields( $method, $index ) {
    if( ! is_checkout()) return; // Only on checkout page

    $customer_carrier_method = 'legacy_local_pickup';

    if( $method->id != $customer_carrier_method ) return; // Only display for "local_pickup"

    $chosen_method_id = WC()->session->chosen_shipping_methods[ $index ];

    // If the chosen shipping method is 'legacy_local_pickup' we display
    if($chosen_method_id == $customer_carrier_method ):

    echo '<div class="custom-carrier">';

    woocommerce_form_field( 'carrier_name' , array(
        'type'          => 'text',
        'class'         => array('form-row-wide carrier-name'),
        'label'         => 'Carrier Information:',
        'required'      => true,
        'placeholder'   => 'Carrier Name',
    ), WC()->checkout->get_value( 'carrier_name' ));

    woocommerce_form_field( 'carrier_number' , array(
        'type'          => 'text',
        'class'         => array('form-row-wide carrier-number'),
        'required'      => true,
        'placeholder'   => 'Carrier Number',
    ), WC()->checkout->get_value( 'carrier_number' ));

    echo '</div>';
    endif;
}

// Check custom fields validation
add_action('woocommerce_checkout_process', 'carrier_checkout_process');
function carrier_checkout_process() {
    if( isset( $_POST['carrier_name'] ) && empty( $_POST['carrier_name'] ) )
        wc_add_notice( ( "Please don't forget to enter the shipping carrier name." ), "error" );

    if( isset( $_POST['carrier_number'] ) && empty( $_POST['carrier_number'] ) )
        wc_add_notice( ( "Please don't forget to enter the shipping carrier account number." ), "error" );
}

// Save custom fields to order meta data
add_action( 'woocommerce_checkout_update_order_meta', 'carrier_update_order_meta', 30, 1 );
function carrier_update_order_meta( $order_id ) {
    if( isset( $_POST['carrier_name'] ))
        update_post_meta( $order_id, '_carrier_name', sanitize_text_field( $_POST['carrier_name'] ) );

    if( isset( $_POST['carrier_number'] ))
        update_post_meta( $order_id, '_carrier_number', sanitize_text_field( $_POST['carrier_number'] ) );
}
此代码位于活动子主题或主题的functions.php文件中。测试和工作

未显示:

选择本地拾取时显示:


刚刚在该链接上更新了您的答案,下面是模板的完整代码,其中还包含隐藏和显示字段的JQuery代码。WooCommerce版本是3.0.5。现场站点在这里我已经尝试了你说的…将字段设置为false,然后尝试使用JQuery…不起作用这是WooCommerce状态报告,这样你就可以看到安装了哪些插件:只是在该链接上对你的答案进行了升级,这是模板的完整代码,还有JQuery代码可以隐藏和显示字段。WooCommerce版本是3.0.5。现场站点在这里我已经尝试了你所说的…将必填字段设置为false,然后尝试使用JQuery…不起作用这是WooCommerce状态报告,以便您可以查看安装了哪些插件:Thank you@LoicTheAztec..这正是我需要的。你是救世主!我正在创建代码,用于在订单编辑页面和管理电子邮件中添加这些字段…我将把这些代码放在这里,以供参考Hank you@LoicTheAztec..这正是我需要的。你是救世主!我正在创建代码,用于在订单编辑页面和管理电子邮件中添加这些字段……我将把这些代码也放在这里以供参考