php - Get on sale dates for the variations of a WooCommerce variable product

Is there any way to geton_sale_from
andon_sale_to
dates for WooCommerce variable products in an array using PHP?
The highlighted red boxes in this screenshot:
Answer
Solution:
Use the following to get the variationson_sale_from
andon_sale_to
dates for a variable product:
$sale_dates = array(); // Initializing
if( $product->is_type('variable') ) {
$variation_ids = $product->get_visible_children();
foreach( $variation_ids as $variation_id ) {
$variation = wc_get_product( $variation_id );
if ( $variation->is_on_sale() ) {
$date_on_sale_from = $variation->get_date_on_sale_from();
$date_on_sale_to = $variation->get_date_on_sale_to();
if( ! empty($date_on_sale_from) || ! empty($date_on_sale_to) ) {
$sale_dates[$variation_id] = array(
'from' => $date_on_sale_from,
'to' => $date_on_sale_to,
);
}
}
}
// Array row output
print_r($sale_dates);
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: mark bundle as not supporting multiuse
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.