检查用户是否在 WooCommerce 中购买了特定产品 [英] Check if a user has purchased specific products in WooCommerce
问题描述
我需要检查客户之前是否在 WooCommerce 中购买了特定产品.
I need to check if a customer has purchased a specific product earlier in WooCommerce.
情况是这样的:客户不能购买产品c"、d"、e",除非他们更早购买了产品a"或b".
The case is this: The customer shall not be able to purchase product "c", "d", "e" unless they have purchased product "a" or "b" at an earlier time.
如果客户之前购买过产品a"或b",则激活产品c"、d"和e"的购买按钮,允许他们购买.
If the customer has purchased product "a" or "b" earlier, then the purchase button of product "c", "d" and "e" is activated and they are allowed to buy them.
如果他们之前没有购买过a"或b",他们将不被允许购买c"、d"、e"并且购买按钮被禁用.
If they haven´t purchased "a" or "b" earlier, they will not be allowed to purchase "c", "d", "e" and the purchase button is deactivated.
我怎样才能做到这一点?
How can I achieve this?
谢谢.
推荐答案
更轻量和改进的代码版本在这里处理多个产品 ID
Lighter and improved code version in HERE that handle multiple product IDs
更新(兼容 Woocommerce 3+)
是的,如果当前客户已经购买了特定定义的产品 ID,则可以编写一个返回true"的条件函数.此代码位于活动子主题或主题的 function.php 文件中.
Yes it's possible, writing a conditional function that returns "true" if current customer has already bought specifics defined products IDs. This code goes on function.php file of your active child theme or theme.
这里是条件函数:
function has_bought_items() {
$bought = false;
// Set HERE ine the array your specific target product IDs
$prod_arr = array( '21', '67' );
// 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;
}
此代码已经过测试且有效.
用法:
例如,您可以在一些WooCommerce 模板中使用它您之前将复制到您的活动子主题或主题:
USAGE:
For example, you can use it in some WooCommerce templates that you will have previously copied to your active child theme or theme:
- 关于
加入购物车
按钮的商店页面模板是loop/add-to-cart.php
. add-to-cart
按钮的产品页面模板位于single-product/add-to-cart
文件夹,具体取决于您的产品类型.
- The template for Shop page concerning
add-to-cart
button isloop/add-to-cart.php
. - The templates for Product pages concerning
add-to-cart
button are insingle-product/add-to-cart
folder depending on your product types.
以下是您可以在这些模板中使用的示例(以上):
Here is an example that you could use in those templates (above):
// Replace the numbers by your special restricted products IDs
$restricted_products = array( '20', '32', '75' );
// compatibility with WC +3
$product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
// customer has NOT already bought a specific product for this restricted products
if ( !has_bought_items() && in_array( $product_id, $restricted_products ) ) {
// Displaying an INACTIVE add-to-cart button (With a custom text, style and without the link).
// (AND optionally) an explicit message for example.
// ALL OTHER PRODUCTS OR RESTRICTED PRODUCTS IF COSTUMER HAS ALREADY BOUGHT SPECIAL PRODUCTS
} else {
// place for normal Add-To-Cart button code here
}
这里是完整应用示例添加到购物车
商店页面上的按钮模板:
<?php
/**
* Loop Add to Cart
*
* This template can be overridden by copying it to yourtheme/woocommerce/loop/add-to-cart.php.
*
* HOWEVER, on occasion WooCommerce will need to update template files and you
* (the theme developer) will need to copy the new files to your theme to
* maintain compatibility. We try to do this as little as possible, but it does
* happen. When this occurs the version of the template file will be bumped and
* the readme will list any important changes.
*
* @see https://docs.woocommerce.com/document/template-structure/
* @author WooThemes
* @package WooCommerce/Templates
* @version 2.5.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
global $product;
// Replace the numbers by your special restricted products IDs
$restricted_products = array( '37', '53', '70' );
// compatibility with WC +3
$product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
if ( !has_bought_items() && in_array( $product_id, $restricted_products ) ) {
echo '<a class="button greyed_button">' . __("Disabled", "your_theme_slug") . '</a>';
echo '<br><span class="greyed_button-message">' . __("Your message goes here…", "your_theme_slug") . '</span>';
} else {
echo apply_filters( 'woocommerce_loop_add_to_cart_link',
sprintf( '<a rel="nofollow" href="%s" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="%s">%s</a>',
esc_url( $product->add_to_cart_url() ),
esc_attr( isset( $quantity ) ? $quantity : 1 ),
esc_attr( $product_id ),
esc_attr( $product->get_sku() ),
esc_attr( isset( $class ) ? $class : 'button' ),
esc_html( $product->add_to_cart_text() )
),
$product );
}
您将使用style.css
文件中的 greyed_button
类设置非活动按钮的样式您的活跃子主题或主题.greyed_button-message
类的消息也是如此.
这篇关于检查用户是否在 WooCommerce 中购买了特定产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!