我有三个自定义结账字段,人们必须至少检查一个才能完成订单。这只适用于1个产品。
因此,我循环浏览购物车项目以检查产品是否在购物车中,然后添加字段。这部分工作良好:
add_action( 'woocommerce_before_order_notes', 'mmm_add_custom_checkout_field' );
function mmm_add_custom_checkout_field( $checkout ) {
$product_id = 214884;
$in_cart = false;
foreach( WC()->cart->get_cart() as $cart_item ) {
$product_in_cart = $cart_item['product_id'];
if ( $product_in_cart === $product_id ) $in_cart = true;
}
if ( $in_cart ) {
echo '<h2>Membership Application</h2>';
echo '<p>Select all that applies</p>';
woocommerce_form_field( 'read_wog', array(
'type' => 'checkbox',
'class' => array( 'form-row-wide no-req' ),
'required' => true,
'label' => 'I accept term 1',
), $checkout->get_value( 'read_wog' ) );
woocommerce_form_field( 'one_on_one', array(
'type' => 'checkbox',
'class' => array( 'form-row-wide no-req' ),
'required' => true,
'label' => 'I accept term 2',
), $checkout->get_value( 'one_on_one' ) );
woocommerce_form_field( 'mm_sn', array(
'type' => 'checkbox',
'required' => true,
'class' => array( 'form-row-wide no-req' ),
'label' => 'I accept term 3).',
), $checkout->get_value( 'mm_sn' ) );
}
}
该网站使用Paypal Express作为支付网关,无论复选框验证如何,验证都可以让人们通过Paypal。默认字段的验证工作正常。错误通知是在手动刷新页面时添加的!
这是验证代码:
add_action( 'woocommerce_checkout_process', 'mmm_validate_new_checkout_field' );
function mmm_validate_new_checkout_field() {
$product_id = 214884;
$in_cart = false;
foreach( WC()->cart->get_cart() as $cart_item ) {
$product_in_cart = $cart_item['product_id'];
if ( $product_in_cart === $product_id ) $in_cart = true;
}
if( $in_cart && !isset($_POST['mm_sn']) && !isset($_POST['one_on_one']) && !isset($_POST['read_wog']) ) {
wc_add_notice( 'You can only have a full membership if you accept at least 1 term', 'error' );
}
}
知道如何让它工作吗?
"该网站使用PayPal Express作为支付网关";
这还不够具体,无法提供建议。如果正在使用PayPal JS SDK按钮(在WooCommerce PayPal插件配置中称为智能按钮(,则您可以添加onClick
处理程序,如下所示:https://developer.paypal.com/docs/business/javascript-sdk/javascript-sdk-reference/#oninitonclick
您需要编辑WooCommerce插件如何调用paypal.Buttons
以包含这样一个函数的输出JS。
如果签出时至少有一个复选框未选中,则以下重新访问的代码将抛出错误验证消息:
// Custom function that check if a specific product is in cart
function is_product_in_cart( $product_id ) {
foreach( WC()->cart->get_cart() as $item ) {
if ( in_array( $product_id, array($item['product_id'], $item['variation_id']) ) ) {
return true;
}
}
return false;
}
// Add Custom checkout checkboxes fields
add_action( 'woocommerce_before_order_notes', 'mmm_add_custom_checkout_field' );
function mmm_add_custom_checkout_field( $checkout ) {
$targeted_id = 214884;
if ( is_product_in_cart( $targeted_id ) ) {
echo '<h2>Membership Application</h2>
<p>Select all that applies</p>';
woocommerce_form_field( 'read_wog', array(
'type' => 'checkbox',
'class' => array( 'form-row-wide no-req' ),
'required' => true,
'label' => 'I accept term 1',
), $checkout->get_value( 'read_wog' ) );
woocommerce_form_field( 'one_on_one', array(
'type' => 'checkbox',
'class' => array( 'form-row-wide no-req' ),
'required' => true,
'label' => 'I accept term 2',
), $checkout->get_value( 'one_on_one' ) );
woocommerce_form_field( 'mm_sn', array(
'type' => 'checkbox',
'required' => true,
'class' => array( 'form-row-wide no-req' ),
'label' => 'I accept term 3).',
), $checkout->get_value( 'mm_sn' ) );
}
}
// Custom checkout checkboxes fields validation
add_action( 'woocommerce_checkout_process', 'mmm_validate_new_checkout_field' );
function mmm_validate_new_checkout_field() {
$targeted_id = 214884;
if ( is_product_in_cart( $targeted_id ) && ! ( isset($_POST['read_wog']) || isset($_POST['one_on_one']) || isset($_POST['mm_sn']) ) ) {
wc_add_notice( 'You can only have a full membership if you accept at least 1 term', 'error' );
}
}
代码位于活动子主题(或活动主题(的functions.php文件中。测试并工作。