I run a mass discount plugin, lets just say for a -50% price which works in the frontend normally. Also I tested the following scenario in Wordpress 5.5.3, woocommerce both 3.7 and 4.8 and no other plugins.
I have a function to create an xml. In that function I check and get the product parent ids and then the products as objects.
When I try to get the sale price with either
$product->get_sale_price()
or print the whole object
print_r($product)
the
[sale_price]
is empty, the
[regular_price]
is empty and the
[price]
returns the original price
Obviously, what I want is to run a mass discount and return the sale price of the products in my custom function.
I tried the 2 mass discount plugins 'WooCommerce Dynamic Pricing & Discounts' and 'Finale Lite'. Then tried with the official themes 2020 and 2021 and with my normal 'Elessi' theme. No results with any plugin/theme/version combination.
I also tried
$product->get_variation_sale_price( 'min', true );
which returns the original price.
I guess this is because sale price is product specific. However, why this happens and how could overcome this and show the sale price if a discount is running?
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => '1000',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => 123,
'operator' => 'IN'
),
array(
'taxonomy' => 'product_visibility',
'field' => 'slug',
'terms' => 'exclude-from-catalog',
'operator' => 'NOT IN'
)
)
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) : $query->the_post();
$product = wc_get_product( $query->post->ID );
$product_id = $product->is_type( 'variation' ) ? ( method_exists($product, 'get_parent_id') ? $product->get_parent_id() : $product->get_parent() ) : $product->get_id();
$product_id = $product->is_type( 'variation' ) ? $product->get_parent_id() : $product->get_id();
$product = wc_get_product( $product_id );
$product_price= $product->get_sale_price() ? $product->get_sale_price() : $product->get_price();
print_r($product);
endwhile;
}