Я хочу написать в php функцию, которая изменяет цену всех продуктов в WooCommerce в зависимости от количества приобретенных покупателем товаров.
Пример: покупатель покупает один товар за 80 €.Все остальные предметы теперь стоят 50 €, а нет 80 €.Теперь он покупает второй предмет за 50 €, все остальные товары становятся по 25 €.
Я пробовал плагины, но никто не работает так, как я хочу.Я новичок в php, поэтому я написал небольшой фрагмент кода php, но я не совсем понимаю, как это сделать.
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');
Я хочу использовать функцию только один раз, но как?И могу ли я использовать add_action в случае переключения?Спасибо, ребята