1

I have this result of $product->get_variation_prices(), see "price" and "sale_price":

array(3) { 
["price"]=> array(3) { 
[18042]=> string(6) "910.57" 
[18044]=> string(6) "910.57" 
[18043]=> string(7) "1050.00" } 
["regular_price"]=> array(3) { 
[18042]=> string(7) "1138.21" 
[18043]=> string(7) "1138.21" 
[18044]=> string(7) "1138.21" } 
["sale_price"]=> array(3) { 
[18042]=> string(6) "910.57"
[18044]=> string(6) "910.57" 
[18043]=> string(7) "1050.00" } 
}

I subtracted 100 from variation_prices

add_filter('woocommerce_variation_prices_price', 'theme_get_variation_price', 99, 3 );
function theme_get_variation_price($price, $variation, $product){

wc_delete_product_transients($variation->get_id());

if ( current_user_can('test') ){
return $price-100; //test
}
}

This is the $product->get_variation_prices() output:

array(3) { 
["price"]=> array(3) { 
[18042]=> string(6) "810.57" 
[18044]=> string(6) "810.57" 
[18043]=> string(6) "950.00" } 
["regular_price"]=> array(3) { 
[18042]=> string(7) "1138.21" 
[18043]=> string(7) "1138.21" 
[18044]=> string(7) "1138.21" } 
["sale_price"]=> array(3) { 
[18042]=> string(7) "1138.21" 
[18043]=> string(7) "1138.21" 
[18044]=> string(7) "1138.21" } 
}

Why that everytime I make a change in $price, the ["sale_price"] values automatically equals to ["regular_price"]? How can I keep the "sale_price" values? Am I missing something? Edit: Just to be clear, I don't want the sale_price to subtract aswell. I want to keep the original values.

unjoined
  • 11
  • 3
  • Check this answer - https://stackoverflow.com/questions/49943319/change-product-variation-prices-via-a-hook-in-woocommerce-3-3 – Snuffy Oct 28 '21 at 12:18

1 Answers1

0

In the WooCommerce Code Reference I found this

                // If sale price does not equal price, the product is not yet on sale.
                if ( $sale_price === $regular_price || $sale_price !== $price ) {
                    $sale_price = $regular_price;
                }

Here's the link: https://woocommerce.github.io/code-reference/files/woocommerce-includes-data-stores-class-wc-product-variable-data-store-cpt.html#source-view.308

Well I don't think it's supposed to have product/variation price different than regular/sale values. Also, because of that some functions don't work anymore like $product->is_on_sale();

unjoined
  • 11
  • 3