【问题标题】:Get WooCommerce product regular price and sale price from a WP_Query从 WP_Query 获取 WooCommerce 产品的正常价格和销售价格
【发布时间】:2021-03-28 17:44:58
【问题描述】:

我成功地在特定类别中运行大规模折扣(假设类别 id 123 中的 -50%)并且在产品网格、产品页面和订单中一切都运行顺利。 (Wordpress 5.7,Woocommerce:5.1.0,尝试了各种大减价插件)

但问题是当我尝试通过我制作的一个小插件在管理页面中打印列表时,我无法获得销售价格

我试过了:

global $woocommerce;
$args = array('post_type'=>'product','post_status'=>'publish','ignore_sticky_posts'=>1);
$query = new WP_Query( $args );
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
    while ( $query->have_posts() ) : $query->the_post();
        global $product;
        $product = wc_get_product( get_the_ID() );
        echo $product->get_sale_price()."<br>"; //prints empty
        echo $product->get_price()."<br>"; //prints the initial price
        echo wc_price( wc_get_price_including_tax( $product ) ).'<br>'; //prints the initial price
        echo $product->get_variation_sale_price( 'min', true ).'<br>'; //prints the initial price
        if ( $product->is_on_sale() ){
            echo 'is on sale<br>'; //it never prints so it does not recognise the product as in sale
        }
    endwhile;
}

为什么没有售价?我怎么才能得到它?大多数产品都有变化。产品页面包含销售运行的类别中所有产品的销售价格。

【问题讨论】:

  • 请对以下代码提供一些反馈。

标签: php wordpress woocommerce backend discount


【解决方案1】:

您需要查询“product”和“product_variation” post type,在循环内排除“variable”产品类型如下:

$query = new WP_Query( array(
    'post_type'           => array('product', 'product_variation'),
    'post_status'         => 'publish',
    'posts_per_page'      => -1,
    'ignore_sticky_posts' => 1,
) );

if ( $query->have_posts() ) {
    while ( $query->have_posts() ) : $query->the_post();
        $product = wc_get_product( get_the_ID() );
        
        if ( ! $product->is_type('variable') ){
            $active_price  = $product->get_price();
            $regular_price = $product->get_regular_price();
            
            echo 'Product Id: ' . $product->get_id() . ' <em>(type:  ' . $product->get_type() . ')<em><br>';
            
            if ( $product->is_on_sale() {
                echo 'Sale price: ' . $active_price . ' <em>(regular price: ' . $regular_price . ')<em><br>';
            } else {
                echo 'Price: '. $active_price .'<br>'; 
            }
        }
        
    endwhile;
}

它应该可以解决您的问题。

【讨论】:

  • 不幸的是,没有。它没有解决问题。不过,如果产品具有作为产品或变量的固定销售价格,那么它正在工作。但是,通过批量折扣插件更改价格(-% 折扣)的产品未正确显示 get_sale_price/get_price(显示初始价格)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-15
  • 1970-01-01
相关资源
最近更新 更多