如果客户已购买特定产品,则禁用WooCommerce产品一段时间

编程入门 行业动态 更新时间:2024-10-26 04:19:53
本文介绍了如果客户已购买特定产品,则禁用WooCommerce产品一段时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在WooCommerce中,如果客户购买了产品(A或B),则在一个月内要禁用对定义产品(B,C和D)的购买,因此产品B,C和D应该只能对该特定用户禁用一个月.

In WooCommerce would like to disable purchases from defined products ( B, C and D ) during a month if customer has purchased a product ( A or B ), so products B, C and D should be disabled for a month for that specific user only.

注意:在下面的代码中,我正在使用 has_bought_items()来自 检查用户是否已在WooCommerce答案中购买了特定产品 .

Note: In the code below I am using has_bought_items() a custom function from Check if a user has purchased specific products in WooCommerce answer.

我的代码尝试:

$product_ids = array( 255, 256 ); $args2 = array( 'customer_id' => get_current_user_id() ); $orders = wc_get_orders( $args2 ); $orders = has_bought_items( get_current_user_id(), $product_ids ); if($orders){ add_filter('woocommerce_is_purchasable', 'disable_product', 10, 2 ); function disable_product( $is_purchasable, $product ) { if( in_array( $product->get_id(), array( 256, 257, 258 ) ) ) { return false; } return $is_purchasable; } } So far I am able to disable the products for the user. But I don't how can I get date from the purchase and add a month to it. Any help will be great.

推荐答案

您需要自定义 has_bought_items()中的代码,以获取最高的发布日期".从购买了产品ID为 255 或 256 (针对当前用户ID)的客户订单中获得.

You need to customize a bit the code from has_bought_items() to get the highest "post date" from a customer order that has purchased product id 255 or 256 (for the current user ID).

然后使用 strtotime()函数,您将可以在一个月内禁用其他已定义产品的购买.

Then using strtotime() function you will be able to disable purchases on other defined products during a month.

使用自定义SQL查询的代码:

The code using a customized SQL query:

add_filter('woocommerce_is_purchasable', 'disable_specific_product_conditionally', 10, 2 ); function disable_specific_product_conditionally( $is_purchasable, $product ) { $targeted_ids = array( 256, 257, 258 ); // Related targeted products to be disabled if( in_array( $product->get_id(), $targeted_ids ) && is_user_logged_in() ) { global $wpdb; $product_ids = array( 255, 256 ); // Check orders for this products // Count the number of products $date = $wpdb->get_var(" SELECT p.post_date FROM {$wpdb->prefix}posts AS p INNER JOIN {$wpdb->prefix}postmeta AS pm ON p.ID = pm.post_id INNER JOIN {$wpdb->prefix}woocommerce_order_items AS woi ON p.ID = woi.order_id INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS woim ON woi.order_item_id = woim.order_item_id WHERE p.post_status IN ( 'wc-" . implode( "','wc-", array_map( 'esc_sql', wc_get_is_paid_statuses() ) ) . "' ) AND pm.meta_key = '_customer_user' AND pm.meta_value = '".get_current_user_id()."' AND woim.meta_key IN ( '_product_id', '_variation_id' ) AND woim.meta_value IN (".implode(',', $product_ids).") ORDER BY p.post_date DESC "); // When a date is found we disable during a month purchases from this date return ! empty($date) && strtotime('now') > strtotime( $date . ' + 1 month') ? false : $is_purchasable; } return $is_purchasable; }

代码进入您的活动子主题(或活动主题)的functions.php文件中.经过测试,可以正常工作.

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

更多推荐

如果客户已购买特定产品,则禁用WooCommerce产品一段时间

本文发布于:2023-10-12 20:54:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1485867.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:产品   客户   WooCommerce

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!