Reputation: 1
When I arrive to the cart I want to count the total articles of different Ids and if they are in both of them in different ranges apply a tier table for them.
For example:
If I found from both list A and B 50 they will have the price of the second range.
My first approach has been:
add_action('woocommerce_before_cart', 'check_category_in_cart');
function check_category_in_cart() {
// Set here your product ID (or variation ID)
$total = 0;
$array_ids = array();
$targeted_id = 351;
$lista = array(351,916,825,528,527,524,192,190,188,186,184,182,180,178,70,68,68,66,64);
// Get quantities for each item in cart (array of product id / quantity pairs)
$quantities = WC()->cart->get_cart_item_quantities();
for ($x=0;$x<count($lista); $x++) {
// Displaying the quantity if targeted product is in cart
if( isset($quantities[$lista[$x]]) && $quantities[$lista[$x]] > 0 ) {
$total += $quantities[$lista[$x]];
array_push($array_ids,$lista[$x]);
}
}
//If is between totals I want to change the price
if($total >= 0 || $total < 150 ){
wc_print_notice($total, 'notice' );
//print_r($array_ids);
}else if($total >= 150 || $total < 300 ){
wc_print_notice($total, 'notice' );
//print_r($array_ids);
}else{
wc_print_notice($total.$array_ids, 'notice' );
//print_r($array_ids);
}
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product = $cart_item['data'];
$price = $product->get_regular_price();
// $cart_item['data']->set_regular_price( 10 );
//$cart_item['data']->set_price( 10 );
//print_r($product_id) ;
}
}
I reach to get the total quantity of all of the ids, but when I try to change the price it only changes the sale price, and If I left this code working It doesn't allow to finish the orders in the cart .
My second approach has been:
add_action( 'woocommerce_before_calculate_totals', 'set_cart_item_sale_price', 20, 1 );
function set_cart_item_sale_price( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Iterate through each cart item
foreach( $cart->get_cart() as $cart_item ) {
$price = $cart_item['data']->get_sale_price(); // get sale price
$cart_item['data']->set_price( 5 ); // Set the sale price
}
}
And I found that the prices that are changing are only the sales not the real ones could be for a plugin?
How is seen Kind regards
Upvotes: 0
Views: 105