2015-11-02 299 views
0

我希望客戶只要購物時返回商店頁面就可以從購物中購買一種產品,他們將被重定向到我的帳戶頁面。如何限制客戶使用woocommerce購買一種產品

<?php 
/** 
* Loop Add to Cart 
* 
* @author  WooThemes 
* @package  WooCommerce/Templates 
* @version  2.1.0 
*/ 

if (! defined('ABSPATH')) exit; // Exit if accessed directly 

global $product; 
$current_user = wp_get_current_user(); 

if (wc_customer_bought_product($current_user->user_email, $current_user->ID, $product->id)) { 
    $redirect = $myaccount; 
} 

我使用下面的代碼放置在循環文件夾中,但它不工作。

我想,用戶可以在一生 說明一旦購買的產品 - 它不是在同一時間 它就像如果有人購買了該產品,那麼他/她永遠能購買任何其他產品購買一個產品。

+0

選中此[插件](https://wordpress.org/plugins/woocommerce-max-quantity/) – Gunaseelan

回答

0

loop/add-to-cart.php模板創建鏈接。您的模板只列出了一些變量(其中$redirect$myaccount定義?)並且不創建鏈接,因此它不會什麼。

更好的解決方案是通過woocommerce_loop_add_to_cart_link過濾器過濾loop/add-to-cart.php模板中創建的鏈接。這樣,如果物品沒有被購買,你可以離開正常的鏈接。

以下內容添加到你的主題functions.php文件:

add_filter('woocommerce_loop_add_to_cart_link', 'so_add_to_cart_link', 10, 2); 
function so_add_to_cart_link($link, $product){ 

    $current_user = wp_get_current_user(); 

    if (wc_customer_bought_product($current_user->user_email, $current_user->ID, $product->id)) { 
     $link = sprintf('<a href="%s" rel="nofollow" class="button product_type_%s">%s</a>', 
      esc_url(get_permalink(wc_get_page_id('myaccount'))), 
      esc_attr($product->product_type), 
      __('View my account', 'theme-text-domain'), 
     ), 
    } 

    return $link; 
} 

注意上面還沒有經過測試,所以要謹慎錯別字。

+0

感謝答覆,但我想,用戶可以一次一生 票據購買的產品 - 它不是關於購買一種產品一次購買 如果有人購買了該產品,那麼他/她將永遠無法購買任何其他產品。 –

+0

因此,不需要'wc_customer_bought_product',您需要編寫一個自定義函數來確定客戶是否購買過任何東西。其餘的依然適用。 – helgatheviking

+0

我不是編碼,因此您可以請共享代碼 感謝 –

0

打開你的主題functions.php,並在最後輸入以下代碼。

add_filter('woocommerce_add_cart_item_data', 'woo_custom_add_to_cart'); 

function woo_custom_add_to_cart($cart_item_data) { 
global $woocommerce; 
$woocommerce->cart->empty_cart(); 

return $cart_item_data; 
} 

現在,您可以通過添加新的產品與項目現有的車進行測試,看是否會只加了最新的產品,並刪除所有之前的產品在購物車中。

+0

感謝您的回覆,但我希望該用戶可以一生中購買一次產品 注意 - 它不是一次購買一件產品 如果有人購買了產品,那麼他/她將永遠無法購買任何其他產品。 –

相關問題