对于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);
}这显示了没有折扣的正常价格。这两段代码都在同一个插件中。
发布于 2020-01-31 17:41:43
对于信息,wc_price()只是用来格式化价格的格式化函数,与$price本身的主要参数无关。您的问题是,在您的第二个函数中,变量$price肯定不使用WC_Product方法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;
}现在,它应该能像预期的那样起作用。
https://stackoverflow.com/questions/60005144
复制相似问题