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