2015-06-09 160 views
1

嘿傢伙我有一個問題與woocommerce,即時嘗試修復幾天。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

  • 我怎樣才能使產品itslef添加到購物車?

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

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

回答

2

我會說,你應該使用Product Add-Ons但似乎並未有日期選擇器。無論如何,我會嘗試修改您的驗證功能如下:

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

,而不是返回TRUE要返回$passed變量的當前狀態。我不能保證這會起作用,因爲我不會設置測試它所需的其他代碼,但這與我多次完成的代碼類似。

在另一方面,除非你的意思是這個驗證應用到每一個產品在您的商店,你需要限制此功能與一些附加條件邏輯。

+1

應該是'add_filter'不'add_action' – rg89

-1

是的,對不起,我是這個論壇的新成員。 但是我已經找到了解決辦法,我有編輯直接類-WC-cart.php在生產線900,和我以前的,如果複製功能:

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

現在對我來說它的工作原理,我知道這是不是最正確的解決方案,但它運作的瞬間,我只有小心的時候我會更新woocommerce

+2

這不是一個答案題 – Daniel

相關問題