问题描述
在WooCommerce中,我使用此答案中的一些代码:
In WooCommerce I am using some code from this answer:
Set WooCommerce cart item price to zero if the product has already been bought
它可以与一种产品一起使用,但我想使其与多种产品一起使用。
It works great with one product, but I would like to make it work with several products.
因此,如果客户已经购买了以下代码,对于特定产品,将购物车价格更改为 $ 0.00
如果不是,则价格设置为 $ 100
(首次购买) :
So the code below, for a specific product, change the cart item price to $0.00
if customer has already purchased and if not the price is set to $100
(for the first purchase):
add_action( 'woocommerce_before_calculate_totals', 'conditionally_change_cart_items_price', 10, 1 );
function conditionally_change_cart_items_price( $cart_object ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$targeted_product_id = 1107;
// Set Here your custom price (1st purshase)
$custom_price = 100; // First purshase for product ID 1092
// Detecting if customer has already bought The targeted product (1092)
if( is_user_logged_in() ){
$customer = wp_get_current_user();
$customer_id = $customer->ID; // customer ID
$customer_email = $customer->email; // customer email
if( wc_customer_bought_product( $customer_email, $customer_id, $targeted_product_id) )
$custom_price = 0; // Set to 0 for other purchases (product ID 1092)
}
foreach ( $cart_object->get_cart() as $cart_item ) {
// When targeted product is in cart we change the price
if ( $cart_item['product_id'] == $targeted_product_id ) {
// Woocommerce 3+ compatibility
if ( version_compare( WC_VERSION, '3.0', '<' ) )
$cart_item['data']->price = $custom_price;
else
$cart_item['data']->set_price( $custom_price );
}
}
}
我如何使其工作
推荐答案
您必须创建一个包含所有目标产品ID的数组,然后应该检查客户是否已经在foreach循环中购买了该产品:
You have to create an array with all target product ids and then you should check if a customer bought this product already in the foreach loop:
add_action( 'woocommerce_before_calculate_totals', 'conditionally_change_cart_items_price', 10, 1 );
function conditionally_change_cart_items_price( $cart_object ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// add as many ids as you need
$targeted_product_ids = array( 1107, 1108, 1109 );
// Set Here your custom price (1st purshase)
$custom_price = 100; // First purshase for target product
if ( is_user_logged_in() ) {
$customer = wp_get_current_user();
$customer_id = $customer->ID; // customer ID
$customer_email = $customer->email; // customer email
foreach ( $cart_object->get_cart() as $cart_item ) {
// When targeted product is in cart we change the price
if ( in_array( $cart_item['product_id'], $targeted_product_ids ) ) {
// Detecting if customer has already bought The targeted product
if ( wc_customer_bought_product( $customer_email, $customer_id, $targeted_product_id ) )
// Set to 0 for other purchases
$custom_price = 0;
// Woocommerce 3+ compatibility
if ( version_compare( WC_VERSION, '3.0', '<' ) )
$cart_item['data']->price = $custom_price;
else
$cart_item['data']->set_price( $custom_price );
}
}
}
}
这篇关于在WooCommerce 3中有条件地设置特定产品的购物车价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!