【发布时间】:2021-05-31 08:14:15
【问题描述】:
我在 Woocommerce 商店中创建了两个自定义函数。这个想法是将某种类型的产品限制为最多 3 个。我想设置一个 cookie 一周,以防止同一客户购买更多相同类型的产品。因此,如果客户今天购买了 1 个,那么在接下来的 6 天内,他们仍然可以再购买 2 个,直到 cookie 过期。但 3 永远是最大值。
该功能的工作原理是,如果在一个会话中将超过 3 个项目添加到购物篮中,它将设置一个限制,并且结帐后 cookie 仍然存在。我的预感是它没有在正确的时间被读取,或者被传递给检查它是否存在的函数。
function filter_woocommerce_add_to_cart_validation( $true, $product_id, $request_quantity, $variation_id = '', $request_variation = '' ) {
// holds checks for all products in cart to see if they're in our category
$lpc_cookie = $_COOKIE['lpc'];
if ( isset( $lpc_cookie ) ) {
$category_checks = $lpc_cookie;
} else {
$category_checks = 0;
}
$limited_product_count = 4;
$limited_slug = 'instant-print'; //replace 'instant-print' with your category's slug
// check each cart item for our category
if( ! is_admin() ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product = $cart_item['data'];
if ( has_term( $limited_slug, 'product_cat', $product->id ) ) {
$category_checks += $cart_item['quantity'];
}
}
}
// We also check the item that the user is trying to add to cart
if ( has_term( $limited_slug, 'product_cat', $product_id ) ) {
$category_checks += $request_quantity;
}
if ($category_checks >= $limited_product_count) {
$notice = 'Sorry, you have reached the maximum amount for this type of product that you can purchase during this period.';
wc_add_notice( __( $notice, 'textdomain' ), 'error' );
return false;
}
return $true;
};
add_filter( 'woocommerce_add_to_cart_validation', 'filter_woocommerce_add_to_cart_validation', 10, 3 );
function set_cookie_for_limited_products () {
$limited_slug = 'instant-print'; //replace 'instant-print' with your category's slug
// check each cart item for our category
if( ! is_admin() ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product = $cart_item['data'];
if ( has_term( $limited_slug, 'product_cat', $product->id ) ) {
$category_checks += $cart_item['quantity'];
}
}
}
// $expire = time() + 60 * 60 * 24 * 7; // expires in one week
$expire = time() + 60 * 60; // expires in one hour
setcookie('lpc', $category_checks, $expire);
}
add_filter( 'woocommerce_before_checkout_form', 'set_cookie_for_limited_products' );
我希望这是显而易见的。最初我试图 echo 和 var_dump 变量,但我发现这会导致其他东西中断。
【问题讨论】:
-
为什么要把这个放在cookies里???如果用户决定只打开其他浏览器或使用“新私人窗口”怎么办?为什么不在数据库中插入与用户相关的购买,并检查用户是否通过数据库查询购买......
-
@lharby 客户必须在商店注册?如果是这样,您应该实现与用户一起存储,如果您强制客户端注册,我可以帮助您,使用 cookie 不是一个好主意
-
@DanielRiera 这是我目前的设置,imgur.com/m5DAs0G 所以你是说我应该禁用访客结帐?我不想让人们停止订购。我知道 cookie 有缺陷,但我也不知道如何读取或写入任何数据库查询。
-
@lharby 检查我的答案 :)
-
哦,我更喜欢同时使用这两个选项的想法。我稍后会尝试检查并回复您。谢谢。
标签: php wordpress cookies woocommerce