I am using woocommerce 9.1.2 , I created a plugin to change the prices of the products while they are in the cart and complete the order; This is my code:
add_action('woocommerce_before_calculate_totals', 'custom_change_cart_item_price', 20, 1);
function custom_change_cart_item_price($cart) {
if (is_admin() && !defined('DOING_AJAX')) return;
foreach ($cart->get_cart() as $cart_item_key => $cart_item) {
$new_price = $cart_item['data']->get_price() * 3;
$cart_item['data']->set_price($new_price);
}
}
My wordpress version is 6.6
I have no errors or warnings in the debug.log; The goal is to temporarily change the prices in a certain context and have the customer complete the order with those prices, without changing the real price of the product
best regards,
For change price visibile in cart I use this:
add_filter('woocommerce_cart_item_price', 'custom_display_cart_item_price', 10, 3);
function custom_display_cart_item_price($price, $cart_item, $cart_item_key) {
// Ottieni il prezzo regolare del prodotto
$regular_price = $cart_item['data']->get_regular_price();
// Verifica che il prezzo regolare sia stato recuperato
if (!empty($regular_price)) {
// Moltiplica il prezzo regolare per 3
$new_price = $regular_price * 3;
// Sovrascrivi il prezzo visualizzato
$price = wc_price($new_price);
}
return $price;
}