php - Conditionally set cart item prices for specific products in WooCommerce 3
Get the solution ↓↓↓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.
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 );
}
}
}
How could I make it work for multiple defined products (instead of one actually)?
Answer
Solution:
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 );
}
}
}
}
Answer
Solution:
Based on: Set Woocommerce cart item price to zero if the product has already been bought
You will be able to make it work for multiple defined products IDs (in an array) this way:
// For WooCommerce version 3 and above (only)
add_action( 'woocommerce_before_calculate_totals', 'conditionally_change_cart_items_prices', 10, 1 );
function conditionally_change_cart_items_prices( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Set HERE the Products to check in this array
$targeted_product_ids = array( 37, 50, 89, 124, 327 );
$products_bought = array();
// Set Here your custom price (1st purshase)
$custom_price1 = 100; // First purshase
// Set Here your custom price (more purshases than first)
$custom_price2 = 0; // Next purshases
// Detecting if customer has already bought our targeted products
if( is_user_logged_in() ){
$customer = wp_get_current_user();
$customer_id = $customer->ID; // customer ID
$customer_email = $customer->email; // customer email
// Checking each product in the array
foreach( $targeted_product_ids as $product_id ){
if( wc_customer_bought_product( $customer_email, $customer_id, $product_id) ){
// We set all (targeted) purchased products in an array
$products_purchased[] = $product_id;
}
}
}
// Checking cart items and changing item prices if needed
foreach ( $cart->get_cart() as $cart_item ) {
// When a targeted products already purchased is in cart we set price 2
if ( in_array( $cart_item['product_id'], $products_purchased ) ) {
$cart_item['data']->set_price( $custom_price2 );
}
// When a targeted products is in cart and has not been purchased we set price 1
elseif ( in_array( $cart_item['product_id'], $targeted_product_ids ) ) {
$cart_item['data']->set_price( $custom_price1 );
}
}
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works in WooCommerce 3+
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: xmlhttprequest error flutter
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.