3
我正在创建一个电子商务网站。我可以在我的主题的functions.php文件中添加什么,以允许客户只从商店购买一种产品,然后在1周内禁用任何其他购买?WooCommerce每周只允许购买一次
客户可以购买任何产品,但购买后,一周内不能再购买任何产品。客户只能在一周后进行其他任何购买。
我只能禁止使用此代码对产品进行购买:
add_filter('woocommerce_add_cart_item_data', 'woo_allow_one_item_only');
function woo_allow_one_item_only($cart_item_data) {
global $woocommerce;
$woocommerce->cart->empty_cart();
// Do nothing with the data and return
return $cart_item_data;
}
$customer_orders = get_posts(array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id(),
'post_type' => wc_get_order_types(),
'post_status' => array('wc-pending', 'wc-processing', 'wc-on-hold', 'wc-completed'),
));
// Order count
$order_count = 1;
if (count($customer_orders) >= $order_count) {
add_filter('woocommerce_is_purchasable', false);
}
我尝试了一个新用户,但现在我甚至不能进行我的第一次购买。首次购买时,我收到错误“您尚未在购物车中添加任何产品”。请帮忙。谢谢。 – Abhi
@Abhi我忘记了这种情况...我的代码将被更新。回来5分钟。你可以用现有的用户测试它... – LoicTheAztec
@Ahi我已经更新了我的答案... – LoicTheAztec