Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从WP_查询中获取WooCommerce产品常规价格和销售价格_Php_Wordpress_Woocommerce_Backend_Discount - Fatal编程技术网

Php 从WP_查询中获取WooCommerce产品常规价格和销售价格

Php 从WP_查询中获取WooCommerce产品常规价格和销售价格,php,wordpress,woocommerce,backend,discount,Php,Wordpress,Woocommerce,Backend,Discount,我成功地在特定类别中运行了大量折扣(比如在类别id 123中为-50%),并且在产品网格、产品页面和订单中一切都运行顺利。(Wordpress 5.7,Woocommerce:5.1.0,尝试了各种大众折扣插件) 然而问题是,当我试图通过我制作的一个小插件在管理页面上打印列表时,我无法获得销售价格 我试过: global $woocommerce; $args = array('post_type'=>'product','post_status'=>'publish','ignor

我成功地在特定类别中运行了大量折扣(比如在类别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;
}
global$woodcommerce;
$args=array('post\u type'=>'product'、'post\u status'=>'publish'、'ignore\u sticky\u posts'=>1);
$query=新的WP\u查询($args);
$query=新的WP\u查询($args);
如果($query->have_posts()){
而($query->have_posts()):$query->the_post();
全球$产品;
$product=wc_get_product(get_the_ID());
echo$product->get_sale_price()。“
”;//打印为空 echo$product->get_price()。“
”;//打印初始价格 echo wc_price(wc_get_price,包括税($product))。
;//打印初始价格 echo$product->get_variation_sale_price('min',true)。“
”//打印初始价格 如果($product->is_uon_usale()){ echo“正在销售中”;//它从不打印,因此无法识别正在销售的产品 } 结束时; }
为什么没有销售价格?我怎样才能得到它?大多数产品都有变化。产品页面包含销售运行类别中所有产品的销售价格。

您需要查询“产品”和“产品变化”post类型不包括循环内的“变量”产品类型,如下所示:

$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;
}
$query=新的WP\u查询(数组)(
'post_type'=>数组('product'、'product_variation'),
“发布状态”=>“发布”,
“每页帖子数”=>-1,
“忽略粘性帖子”=>1,
) );
如果($query->have_posts()){
而($query->have_posts()):$query->the_post();
$product=wc_get_product(get_the_ID());
如果(!$product->is_type('variable')){
$active_price=$product->get_price();
$regular_price=$product->get_regular_price();
回显“产品Id:”。$Product->get_Id()。(类型:”。$Product->get_type())
; 如果($product->正在销售(){ 回音“销售价格:”.$active_price.”(正常价格:“.$RECURAL_price.”)
; }否则{ 回音“价格:”.$active_价格。“
”; } } 结束时; }

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

请对下面的代码进行一些反馈。不幸的是,没有。它没有解决问题。但是,如果产品作为产品或变量有一个固定的销售价格,那么它是有效的。但是,价格发生变化的产品(-%折扣)通过批量折扣插件,无法正确显示get_sale_价格/get_价格(显示初始价格)。