I want to write a function in php which changes the price of all products in WooCommerce depending on the number of purchased items by the customer.
Example: a customer buys one item at 80€. All others items are now at 50€ and no at 80€. He buys now a second item at 50€, all others items become at 25€.
I've tried plugins but no one works as I want. I'm a beginner in php so I wrote a little php code snippet but I don't really understand how to do it.
function recalculate_price30( $cart_object) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
foreach ( $cart_object->get_cart() as $hash => $value ) {
// I want to make discounts only for products in category with ID ***/25 → Exclusivité
if( !in_array( 25, $value['data']->get_category_ids() )) {
$newprice = $value['data']->get_regular_price() - 30;
// in case the product is already on sale and it is much cheeper with its own discount
if( $value['data']->get_sale_price() > $newprice )
$value['data']->set_price( $newprice );
}
}
}
function recalculate_price55( $cart_object) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
foreach ( $cart_object->get_cart() as $hash => $value ) {
// I want to make discounts only for products in category with ID ***/25 → Exclusivité
if( !in_array( 25, $value['data']->get_category_ids() )) {
$newprice = $value['data']->get_regular_price() - 55;
// in case the product is already on sale and it is much cheeper with its own discount
if( $value['data']->get_sale_price() > $newprice )
$value['data']->set_price( $newprice );
}
}
}
function recalculate_price70( $cart_object) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
foreach ( $cart_object->get_cart() as $hash => $value ) {
// I want to make discounts only for products in category with ID ***/25 → Exclusivité
if( !in_array( 25, $value['data']->get_category_ids() )) {
$newprice = $value['data']->get_regular_price() - 70;
// in case the product is already on sale and it is much cheeper with its own discount
if( $value['data']->get_sale_price() > $newprice )
$value['data']->set_price( $newprice );
}
}
}
function number_product_purchased() {
global $product, $woocommerce, $woocommerce_loop;
//Get user
$current_user = wp_get_current_user();
//Get user order (Completed + Processing) + AJOUTER ARTICLES PANIER
$customer_orders = get_posts( array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'meta_value' => $current_user->ID,
'post_type' => wc_get_order_types(),
'post_status' => array_keys( wc_get_is_paid_statuses() ),
) );
//parcourir les commandes pour obtenir le nombre d'articles
if ( ! $customer_orders ) return;
$product_number = 0;
foreach ( $customer_orders as $customer_order ) {
$order = wc_get_order( $customer_order->ID );
$items = $order->get_items();
foreach ( $items as $item ) {
$product_number = $product_number + 1;
}
}
$cartcount = WC()->cart->get_cart_contents_count();
$product_number= $product_number + $cartcount;
//changer affichage des prix
switch ($product_number){
case 0:
break;
case 1:
break;
case 2:
break;
default:
break;
}
}
add_action('woocommerce_before_calculate_totals', 'number_product_purchased');
I want to use the function just one time but how ? And can I use add_action in a switch case ? Thank you guys