温馨提示:本文翻译自stackoverflow.com,查看原文请点击:wordpress - wc_price($price) not showing the discount by FILTER "woocommerce_product_get_regular_price"
product woocommerce wordpress hook-woocommerce price

wordpress - wc_price($ price)未按FILTER“ woocommerce_product_get_regular_price”显示折扣

发布于 2020-04-04 10:30:04

使用WooCommerce,我可以使用以下功能来对产品价格进行折扣:

add_filter('woocommerce_product_get_regular_price', 'custom_price' , 99, 2 );
function custom_price( $price, $product )
{
$price = $price - 2;
return $price
}

这可以在任何地方(在商店,购物车中,在后端中)使用,但在我的自定义产品列表插件中却无法使用:

add_action( 'woocommerce_account_nybeorderlist_endpoint', 'patrickorderlist_my_account_endpoint_content' );
function patrickorderlist_my_account_endpoint_content() {

    //All WP_Query

    echo wc_price($price);
}

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

查看更多

提问者
Redman
被浏览
128
LoicTheAztec 2020-02-01 05:37

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

因此,请尝试类似的操作(这是一个示例,因为您未WP_Query在问题中提供您的信息)

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

现在它应该可以按预期工作。