Answer the question
In order to leave comments, you need to log in
Answer the question
In order to leave comments, you need to log in
If YOU are constantly maintaining the site, and understand what is going on then the fastest and cleanest way is to code yourself.
To do this, you will need the woocommerce_cart_calculate_fees hook , in the handler of which you can determine the presence of the desired product in the cart and calculate the cost of all products and add the desired discount. The whole task is solved in one function.
/************************************************************************/
/************************************************************************/
/* WP - 4.6.1, WC - 2.6.11
* Добавляем возможность скидки 50% на второй (самый дешевый) товар к корзине
* только для товаров категории $category_id = 914; // slag = 'aktsiya-2-1'
*/
add_action( 'woocommerce_cart_calculate_fees','hml_fee_two_plus_one', 10, 1 );
function hml_fee_two_plus_one( WC_Cart $cart_object ) {
global $wpdb, $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) { return; }
$arr_cart = $cart_object->get_cart();
if ( 0 == sizeof($arr_cart) ) { return; }
$count = 0;
$min_price = 0;
$category_id = 914; // slag = 'aktsiya-2-1'
// получаем список всех вложенных категорий включая родительскую
$cats = hml_get_category_gender_line( $category_id );
foreach( $arr_cart as $item_key => $value ) {
if ( has_term($cats, 'product_cat', $value['product_id'] ) ) {
$count += $value["quantity"];
$product = $value['data'];
$price = $product->get_price();
if ( $price ) { $prices[] = floatval($price); }
}
}
$min_price = max( $min_price, min($prices) );
if ( $count >= 2 && $min_price > 0 ) {
$fee = -1 * $min_price/2;
$cart_object->add_fee( __('Акция: 2я вещь -50%'), $fee );
}
//}
}
function hml_get_category_gender_line( $cat_parent ){
// get_term_children() accepts integer ID only
$line = get_term_children( (int) $cat_parent, 'product_cat');
$line[] = $cat_parent;
return $line;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question