ale*_*jos 6 php wordpress product orders woocommerce
我需要检查一下客户是否在WooCommerce中购买了特定产品.
情况是这样的:除非客户在较早时间购买产品"a"或"b",否则客户不能购买产品"c","d","e".
如果客户较早购买了产品"a"或"b",则激活产品"c","d"和"e"的购买按钮,并允许他们购买.
如果他们之前没有购买"a"或"b",他们将不被允许购买"c","d","e"并且购买按钮被停用.
我怎样才能做到这一点?
谢谢.
Loi*_*tec 14
2020 年更新: 现在通过他们的账单电子邮件处理来宾用户。
新版本紧凑,更轻,更快,兼容所有版本的woocommerce (2.4及以上版本)
这是一个新的增强和更轻的条件函数,部分基于内置的 woocommerce 函数wc_customer_bought_product源代码。
有 2 个可选参数:
$user_var将允许您:
$product_ids (array)将允许指定一个或多个要检查的产品 ID这是代码:
function has_bought_items( $user_var = 0, $product_ids = 0 ) {
global $wpdb;
// Based on user ID (registered users)
if ( is_numeric( $user_var) ) {
$meta_key = '_customer_user';
$meta_value = $user_var == 0 ? (int) get_current_user_id() : (int) $user_var;
}
// Based on billing email (Guest users)
else {
$meta_key = '_billing_email';
$meta_value = sanitize_email( $user_var );
}
$paid_statuses = array_map( 'esc_sql', wc_get_is_paid_statuses() );
$product_ids = is_array( $product_ids ) ? implode(',', $product_ids) : $product_ids;
$line_meta_value = $product_ids != ( 0 || '' ) ? 'AND woim.meta_value IN ('.$product_ids.')' : 'AND woim.meta_value != 0';
// Count the number of products
$count = $wpdb->get_var( "
SELECT COUNT(p.ID) 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-", $paid_statuses ) . "' )
AND pm.meta_key = '$meta_key'
AND pm.meta_value = '$meta_value'
AND woim.meta_key IN ( '_product_id', '_variation_id' ) $line_meta_value
" );
// Return true if count is higher than 0 (or false)
return $count > 0 ? true : false;
}
Run Code Online (Sandbox Code Playgroud)
代码位于您的活动子主题(或主题)的 functions.php 文件中或任何插件文件中。
此代码在 WooCommerce 3+ 上进行了测试并且有效(它也应该适用于以前的版本)。
使用示例:
示例 1 (登录客户):检测当前用户是否购买了定义的产品之一(产品 ID 需要是一个数组)
// Define the targeted Products IDs
$product_ids = array( 38, 41, 85, 95 );
if( has_bought_items( '', $product_ids ) )
echo "<p>You have already purchased one of this products</p>";
else
echo "<p>You have not yet purchased one of this products</p>";
Run Code Online (Sandbox Code Playgroud)
示例2 (对于定义的用户ID)检测定义的用户是否购买了定义的产品之一(产品ID需要设置在数组中)
// Define the user ID
$user_id = 85;
// Define the targeted Products IDs
$product_ids = array( 38, 41, 85, 95 );
if( has_bought_items( $user_id, $product_ids ) )
echo "<p>This user have already purchased one of this products</p>";
else
echo "<p>This user have not yet purchased one of this products</p>";
Run Code Online (Sandbox Code Playgroud)
如果$user_id未定义且当前用户未登录,则此函数将返回false。
示例 3 (对于访客用户)检测访客用户是否从他的账单电子邮件中 购买了定义的产品之一(产品 ID 需要设置在数组中)
// Define guest Billing email (string)
$user_email = 'louis.fourteen@gmail.com';
// Define the targeted Products IDs
$product_ids = array( 38, 41, 85, 95 );
if( has_bought_items( $user_email, $product_ids ) )
echo "<p>This user have already purchased one of this products</p>";
else
echo "<p>This user have not yet purchased one of this products</p>";
Run Code Online (Sandbox Code Playgroud)
如果$user_id未定义且当前用户未登录,则此函数将返回false。
示例 4 (登录客户):检测当前用户是否已经购买
if( has_bought_items() )
echo '<p>You have already made a purchase</p>';
else
echo '<p>Welcome, for your first purchase you will get a discount of 10%</p>';
Run Code Online (Sandbox Code Playgroud)
示例 5 (定义用户 ID) - 检测定义的用户是否已经购买
// Define the user ID
$user_id = 85;
if( has_bought_items( $user_id ) )
echo '<p>customer have already made a purchase</p>';
else
echo '<p>Customer with 0 purshases</p>';
Run Code Online (Sandbox Code Playgroud)
现在,如果用户 id 等于 0 并且当前用户未登录,则此函数将返回
false(如果没有为访客用户定义账单电子邮件)。
更新 (Woocommerce 3+的兼容性)
是的,如果当前客户已经购买了特定的定义产品ID,则编写一个返回"true"的条件函数.此代码位于活动子主题或主题的function.php文件中.
这是条件函数:
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;
}
Run Code Online (Sandbox Code Playgroud)
此代码经过测试和运行.
用法:
例如,您可以在以前复制到活动子主题或主题的某些WooCommerce模板中使用它:
add-to-cart按钮的模板是loop/add-to-cart.php.add-to-cart按钮的模板位于single-product/add-to-cart文件夹中,具体取决于您的产品类型.以下是您可以在这些模板中使用的示例(上图):
// 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
}
Run Code Online (Sandbox Code Playgroud)
这里完整的应用实例,以add-to-cart按钮模板店铺页:
<?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 );
}
Run Code Online (Sandbox Code Playgroud)
您将使用活动子主题或主题greyed_button的style.css文件中的类设置非活动按钮的样式.对于带有greyed_button-message类的消息也是如此.
| 归档时间: |
|
| 查看次数: |
11557 次 |
| 最近记录: |