簡體   English   中英

WooCommerce 添加到購物車驗證:防止添加到購物車

[英]WooCommerce add to cart validation: prevent add to cart

我對 woocommerce 有一個問題,我試圖修復幾天。

我正在為一個人創建一個網站,他希望我在產品頁面上添加自定義輸入,我自己做不到,所以我使用了在線自由職業者。

在產品頁面上,我有一個添加到購物車按鈕、數量輸入和日期輸入。

“日期輸入”是自由職業者所做的。

問題是,當 $_REQUEST['thedate'] 為空時,自定義錯誤彈出窗口但產品仍添加到購物車中。

代碼:(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

  • 如何防止產品將其添加到購物車?

自由職業者所做的其他代碼:

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 );

我會說你應該使用產品附加組件,但它似乎沒有日期選擇器。 無論如何,我會嘗試修改您的驗證功能,如下所示:

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 我不能保證這會奏效,因為我不會設置測試它所需的其余代碼,但這與我多次做過的類似。

另一方面,除非您打算將此驗證應用於商店中的每個產品,否則您需要使用一些額外的條件邏輯來限制此功能。

我遇到了這個問題,經過反復試驗,修復方法是調整 $priority 參數(默認為 10):

add_filter( 'woocommerce_add_to_cart_validation', 'add_the_date_validation', 10, 5 );

像往常一樣,我先嘗試了其他所有方法……將其更改為 15 和賓果……! 我猜它在優先級較低的代碼之前運行,然后將物品放入購物車。

// Allow only pre-defined quantity from specific per category in the cart
add_filter( 'woocommerce_add_to_cart_validation', 'allowed_quantity_per_category_in_the_cart', 10, 2 );
function allowed_quantity_per_category_in_the_cart( $passed, $product_id) {

  $running_qty = 0;
  $restricted_product_cats = $restricted_product_cat_slugs = array();

    //Specify the category/categories by category slug and the quantity limit
    $restricted_product_cats[] = array('cat_slug'=> 'skip-bins', 'max_num_products' => 1); // change here
    //$restricted_product_cats[] = array('cat_slug'=> 'caliper-paint-kit', 'max_num_products' => 4); //change here
    foreach($restricted_product_cats as $restricted_prod_cat) $restricted_product_cat_slugs[]= $restricted_prod_cat['cat_slug'];

    // Getting the current product category slugs in an array
    $product_cats_object = get_the_terms( $product_id, 'product_cat' );
    foreach($product_cats_object as $obj_prod_cat) $current_product_cats[]=$obj_prod_cat->slug;

    // Iterating through each cart item
    foreach (WC()->cart->get_cart() as $cart_item_key=>$cart_item ){


        // Check if restricted category/categories found
      if( array_intersect($restricted_product_cat_slugs, $current_product_cats) ) {

        foreach($restricted_product_cats as $restricted_product_cat){
          if( in_array($restricted_product_cat['cat_slug'], $current_product_cats) && has_term( $current_product_cats, 'product_cat', $cart_item['product_id'] )) {

                    // count(selected category) quantity
            $running_qty += (int) $cart_item['quantity'];

                    // Check if More than allowed products in the cart
            if( $running_qty >= $restricted_product_cat['max_num_products'] ) {

                        //limit to maximum allowed
                        //WC()->cart->set_quantity( $cart_item_key, $restricted_product_cat['max_num_products'] ); // Change quantity

                        // Get the category information
              $catinfo = get_term_by('slug', $restricted_product_cat['cat_slug'], 'product_cat');

              wc_add_notice( sprintf( 'Maximum of  %s '.($restricted_product_cat['max_num_products']>1?'skip bin ('.$catinfo->name.') are':'skip bin').' allowed in the cart. Please remove the existing Skip Bin from the cart to add the new Skip Bin',  $restricted_product_cat['max_num_products'] ), 'error' );
                         ///WC()->cart->empty_cart();
                        $passed = false; // don't add the new product to the cart
                        // We stop the loop
                        break;
                      }
                    }
                  }
                }
              }
              return $passed;
}

是的,對不起,我是這個論壇的新手。 但是我找到了一個解決方案,我在第 900 行直接編輯了class-wc-cart.php ,並且我在 if 之前復制了該函數:

if($passed!=false)
{
    do_action( 'woocommerce_add_to_cart', $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data );
}

現在對我來說它有效,我知道這不是最正確的解決方案,但目前它有效,我只需要更新 woocommerce時小心

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM