I would like to change regular price to custom price of products in the cart for specific categories only ('t-shirts-d','socks-d','joggers-d','boxers-d') as each of the products share 2 different categories.
I tried doing so and it worked but the custom price affects on other categories too, and I want to only show the original price for other categories('t-shirts','socks','joggers','boxers').
I Need help on this.
Here is my code so far:
function changeprice($html, $cart_item, $cart_item_key){
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
//$thepro = $woocommerce->cart->get_cart();
$product = $cart_item['data'];
$heading_nicename = array('t-shirts-d','socks-d','joggers-d','boxers-d');
//$heading_nicename1 = array('t-shirts','socks','joggers','boxers');
$termsother = $heading_nicename;
foreach( $termsother as $termsnew ) {
if (is_cart()) {
$price_adjusted = 666.666666667; // your adjustments here
$price_base = $cart_item['data']->sale_price;
if (!empty($price_adjusted)) {
if (intval($price_adjusted) > 0) {
$cart_item['data']->set_price($price_adjusted);
} /*else {
$html = '<span class="amount">' . wc_price($price_base)
. '</span>';
}*/
}
}
}
}
}
add_filter('woocommerce_cart_item_price', 'changeprice', 100, 3);
THE RIGHT WORKING HOOK (Updated):
add_action( 'woocommerce_before_calculate_totals', 'change_cart_items_prices', 10, 1 );
function change_cart_items_prices( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
foreach ( $cart->get_cart() as $cart_item ) {
if( has_term( array('t-shirts-d','socks-d','joggers-d','boxers-d'), 'product_cat', $cart_item['product_id'] ) ){
$price_adjusted = 666.666666667; // your adjustments here
if ( ! empty( $price_adjusted ) && intval( $price_adjusted ) > 0) {
$cart_item['data']->set_price( $price_adjusted );
}
}
}
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This time everything work on cart and on checkout pages. Totals are updated too.