首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >wc_price($price)没有通过过滤器"woocommerce_product_get_regular_price“显示折扣

wc_price($price)没有通过过滤器"woocommerce_product_get_regular_price“显示折扣
EN

Stack Overflow用户
提问于 2020-01-31 13:46:36
回答 1查看 1.4K关注 0票数 3

对于WooCommerce,我有以下功能,可以对我的产品价格进行折扣:

代码语言:javascript
复制
add_filter('woocommerce_product_get_regular_price', 'custom_price' , 99, 2 );
function custom_price( $price, $product )
{
$price = $price - 2;
return $price
}

它在任何地方都能工作(在商店、购物车、后端),但在我的定制产品列表插件中却没有:

代码语言:javascript
复制
add_action( 'woocommerce_account_nybeorderlist_endpoint', 'patrickorderlist_my_account_endpoint_content' );
function patrickorderlist_my_account_endpoint_content() {

    //All WP_Query

    echo wc_price($price);
}

这显示了没有折扣的正常价格。这两段代码都在同一个插件中。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-31 17:41:43

对于信息,wc_price()只是用来格式化价格的格式化函数,与$price本身的主要参数无关。您的问题是,在您的第二个函数中,变量$price肯定不使用WC_Product方法get_regular_price(),这在您的例子中是…所必需的因此,在WP_Query循环中,您需要获取WC_Product对象实例,然后使用get_regular_price()…方法从该对象获取价格。

所以尝试这样的方法(这是一个例子,因为你没有在你的问题中提供你的WP_Query )。

代码语言:javascript
复制
add_action( 'woocommerce_account_nybeorderlist_endpoint', 'rm_orderlist_my_account_endpoint_content' );
function rm_orderlist_my_account_endpoint_content() {

    $products = new WP_Query( $args ); // $args variable is your arguments array for this query

    if ( $products->have_posts() ) :
    while ( $products->have_posts() ) : $products->the_post();

    // Get the WC_Product Object instance
    $product = wc_get_product( get_the_id() );

    // Get the regular price from the WC_Product Object
    $price   = $product->get_regular_price();

    // Output the product formatted price
    echo wc_price($price);

    endwhile;
    wp_reset_postdata();
    endif;
}

现在,它应该能像预期的那样起作用。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60005144

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档