2015-11-02 295 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

感谢您的回复,但我希望该用户可以一生中购买一次产品 注意 - 它不是一次购买一件产品 如果有人购买了产品,那么他/她将永远无法购买任何其他产品。 –

相关问题