php - 如果客户购买了一些特定的产品,那么我想将其他条件放入其中
问题描述
如果用户购买我的 woocommerce 产品(id= 1900,1902,1938)。然后我想在其中添加一些其他条件。任何人都可以帮助我吗?我试过这个检查客户是否在 WooCommerce 中购买了特定产品
但它对我不起作用,因为它只适用于一种产品(但不适用于许多产品)。
这是我的代码:
function has_bought_items() {
$bought = false;
// Set HERE ine the array your specific target product IDs
$prod_arr = array( '1900','1902','1938');
// Get all customer orders
$customer_orders = get_posts( array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id(),
'post_type' => 'shop_order', // WC orders post type
'post_status' => 'wc-completed' // Only orders with status "completed"
) );
foreach ( $customer_orders as $customer_order ) {
// Updated compatibility with WooCommerce 3+
$order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;
$order = wc_get_order( $customer_order );
// Iterating through each current customer products bought in the order
foreach ($order->get_items() as $item) {
// WC 3+ compatibility
if ( version_compare( WC_VERSION, '3.0', '<' ) )
$product_id = $item['product_id'];
else
$product_id = $item->get_product_id();
// Your condition related to your 2 specific products Ids
if ( in_array( $product_id, $prod_arr ) )
$bought = true;
}
}
// return "true" if one the specifics products have been bought before by customer
return $bought;
}
然后我在 IF / ELSE 语句中以这种方式使用它:
if ( has_bought_items() ) {
// my condtion
} else {
//no condition
}
解决方案
推荐阅读
- angular - 如果 kendo-ui 默认使用英语表达但我在我的应用程序中使用另一种语言,我如何生成翻译文件?
- javascript - 无法将表单数据发送到 PHP 页面
- python-3.x - Elasticsearch 7:无法解析日期
- c# - Parallel.For 启动了意外数量的线程
- javascript - 推送订阅已取消订阅或过期,状态码为 410
- c++ - 在 CortexM0 中从 RAM 运行代码时出现硬故障
- android - 我可以将我的应用程序 sharedprefs 数据限制为 android 中的谷歌驱动器/云吗?
- javascript - 无法在带有 flex 的 slick-slider 项目中居中 div
- python - 为什么 python round(np.float16(np.pi),5) 返回无穷大?错误、限制或预期?
- android - 在隔离网络中将 MQTT 客户端连接到 Android 中的 MQTT 服务器时出现问题