WooCommerce 添加到购物车验证:防止添加到购物车 [英] WooCommerce add to cart validation: prevent add to cart

查看:45
本文介绍了WooCommerce 添加到购物车验证:防止添加到购物车的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 woocommerce 的问题,我试图修复几天.

I have a probleme with woocommerce that im trying to fix for few days.

我正在为一个人创建一个网站,他希望我在产品页面上添加自定义输入,我自己无法完成,所以我使用了在线自由职业者.

I am creating a website for a guy and he wanted me to add a custom input on the product page, I couldn't do it myself so I used a freelancer online for it.

在产品页面上,我有一个添加到购物车按钮、数量输入和日期输入.

On the product page I have an add-to-cart button, quantity input and date input.

日期输入"是自由职业者做的.

The "date input" is the freelancer did.

问题是,当 $_REQUEST['thedate'] 为空时,自定义错误弹出窗口但产品仍添加到购物车中.

The thing is that when the $_REQUEST['thedate'] is empty the custom error popup but the product still added to the cart.

代码:(functions.php)

the code:(functions.php)

function add_the_date_validation() { 
if ( empty( $_REQUEST['thedate'] )) {
    wc_add_notice( __( 'Please enter a date.', 'woocommerce' ), 'error' );
    return false;
}
return true;
}
add_action( 'woocommerce_add_to_cart_validation', 'add_the_date_validation', 10, 5 );    

观点:http://i.stack.imgur.com/a75P6.png

  • 如何防止产品自行添加到购物车?

自由职业者所做的其他代码:

other codes the freelancer did:

function save_add_the_date_field( $cart_item_key, $product_id = null, $quantity= null, $variation_id= null, $variation= null ) {
if( isset( $_REQUEST['thedate'] ) ) {
    WC()->session->set( $cart_item_key.'_the_date', $_REQUEST['thedate'] );
}
}

add_action( 'woocommerce_add_to_cart', 'save_add_the_date_field', 1, 5 );

function render_meta_on_checkout_order_review_item( $quantity = null, $cart_item = null, $cart_item_key = null ) {
if( $cart_item_key && WC()->session->__isset( $cart_item_key.'_the_date' ) ) {
    echo $quantity. '<dl class="">
             <dt class="">Date: </dt>
             <dd class=""><p>'. WC()->session->get( $cart_item_key.'_the_date') .'</p></dd>
          </dl>';
}
}

add_filter( 'woocommerce_checkout_cart_item_quantity', 'render_meta_on_checkout_order_review_item', 1, 3 );

function the_date_order_meta_handler( $item_id, $values, $cart_item_key ) {
if( WC()->session->__isset( $cart_item_key.'_the_date' ) ) {
    wc_add_order_item_meta( $item_id, "the_date", WC()->session->get( $cart_item_key.'_the_date') );
}
}

add_action( 'woocommerce_add_order_item_meta', 'the_date_order_meta_handler', 1, 3 );

function the_date_force_individual_cart_items($cart_item_data, $product_id)
{
$unique_cart_item_key = md5( microtime().rand() );
$cart_item_data['unique_key'] = $unique_cart_item_key;

return $cart_item_data;
}

add_filter( 'woocommerce_add_cart_item_data','the_date_force_individual_cart_items', 10, 2 );

推荐答案

我会说你应该使用 Product Add-Ons 但它似乎没有日期选择器.无论如何,我会尝试如下修改您的验证功能:

I would have said that you should use Product Add-Ons but it doesn't seem to have a date picker. Anyway, I would try modifying your validation function as follows:

function add_the_date_validation( $passed ) { 
if ( empty( $_REQUEST['thedate'] )) {
    wc_add_notice( __( 'Please enter a date.', 'woocommerce' ), 'error' );
    $passed = false;
}
return $passed;
}
add_filter( 'woocommerce_add_to_cart_validation', 'add_the_date_validation', 10, 5 );  

您想返回 $passed 变量的当前状态而不是返回 TRUE.我不能保证这会奏效,因为我不打算设置测试它所需的其余代码,但这与我多次做过的类似.

Instead of returning TRUE you want to return the current status of the $passed variable. I can't promise that will work, because I am not going to set up the rest of the code required to test it, but this is similar to what I have done many times.

另一方面,除非您打算将此验证应用于商店中的每个产品,否则您需要使用一些额外的条件逻辑来限制此功能.

On another note, unless you mean to apply this validation to every product in your store, you need to limit this function with some additional conditional logic.

这篇关于WooCommerce 添加到购物车验证:防止添加到购物车的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆