如何基于自定义字段以编程方式从自定义字段设置/覆盖变体销售价格

时间:2019-06-20 00:11:58

标签: javascript php wordpress

add_filter( 'woocommerce_product_variation_get_sale_price', 
    'custom_dynamic_sale_price_variation', 10, 2 );
add_filter( 'woocommerce_product_variation_get_price', 
    'custom_dynamic_sale_price_variation', 10, 2 );
function custom_dynamic_sale_price_variation( $price, $product ) {
    global $args;
    $args = array(
        'post_type'     => 'product_variation',
        'post_status'   => array( 'private', 'publish' ),
        'numberposts'   => -1,
        'orderby'       => 'menu_order',
        'order'         => 'asc',
        'post_parent'   => get_the_ID() // get parent post-ID
    );
    $variations = get_posts( $args );
    foreach ( $variations as $variation ) 
    {
        $variation_ID = $variation->ID;

        $new_price = get_post_meta( $variation_ID, 
            'flash_sale_price_variation', true );
        $date      = get_post_meta( $variation_ID, 'date_from_variation', 
            true );

        echo '<div class="mk-col-1-1" style="float: none; display: grid;">';
        echo '<div class="row">';
        echo $new_price;
        echo '</div>';
        echo '</div>';

        if( ! empty($date) && ! empty($new_price) ) {
            $date_time      = (int) strtotime($date); // Convert date in time
            $now_time       = (int) strtotime("now"); // Now time in seconds
            $one_day        = 86400; // One day in seconds

    // Calculate the remaining days
            $due_days = floor( ( $now_time - $date_time ) / $one_day );
            if( $due_days == 0 )
                $price = $new_price;
            }
      return $price;
  }
}

0 个答案:

没有答案