获取WooCommerce可变产品的变体的销售日期

时间:2020-08-06 08:08:46

标签: php wordpress date woocommerce product-variations

是否可以使用PHP获取数组中WooCommerce变量产品的on_sale_fromon_sale_to日期?

此屏幕截图中突出显示的红色框:

the red parts in this picture

1 个答案:

答案 0 :(得分:0)

使用以下代码获取变量产品的变化on_sale_fromon_sale_to日期:

$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);
}