I am having a problem getting my price updates to stick. More or less this is a sample of what I am using to update the prices:
add_action( 'woocommerce_before_calculate_totals', 'cst_product_quantity_discounter', 10 );
function cst_product_quantity_discounter( $cart_object ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ){
return;
}
if( ! isset( WC()->session->reload_checkout ) ) {
// declare woocommerce global
global $woocommerce;
// array of ID's to match that represent each item
$special_ids_array = [
0 => array( "id" => 15372, "product" => "TAF", "single_bag_price" => 42.99, "3_multiplier" => 0.82181902768 ),
1 => array( "id" => 14285, "product" => "POW", "single_bag_price" => 29.99, "3_multiplier" => 0.8890741358 )
];
// gather cart object
$wc_cart = WC()->cart->get_cart();
foreach( $wc_cart as $cart_item ){
// gather relevant data for testing and comparisons
$product_id = $cart_item['product_id'];
$product_key = $cart_item['key'];
$pack_size_attribute = $cart_item['variation']['attribute_pa_sample-check']; // returns: "full-pack" || "sample" || null
$item_quantity = $cart_item['quantity'];
foreach ( $special_ids_array as $id_test_2 ) {
if ( $product_id === $id_test_2["id"] && $pack_size_attribute !== "sample" ){
foreach ( $cart_object->cart_contents as $key => $value ) {
// if the key matches we will set the price of the correct product
if ( $key === $product_key ){
$discounted_price = 10;
$value['data']->set_price($discounted_price);
}
}
}
}
}
}
}
There is some other stuff to get the values seen but that works. this will update on my test cart page and everything looks fine.
My issue is when I go to my test checkout page the prices update briefly, then they are overwritten by the original price. I am missing some update thats being run on checkout and woocommerce_before_calculate_totals
hook does not seem to be making a permanent change on the checkout page.
Where do I need to hook my function so the changes persist whatever loads are occurring on checkout that overwrite the initial successful price change?