使用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);
}
这显示了没有折扣的正常价格。这两段代码都在同一个插件中。
对于信息,wc_price()
只是用于格式化价格的格式化函数,与$price
本身的主要参数无关。您的问题是,在第二个函数中,变量$price
肯定不使用您所需要的WC_Product
method 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;
}
现在它应该可以按预期工作。
谢谢... $ product = wc_get_product(get_the_id()); $ price = $ product-> get_regular_price()这就是解决方案。将线程标记为已解决。
@Redman因为有些错误,我对代码进行了一些编辑……但是,是的
$product = wc_get_product( get_the_id() );
,这$price = $product->get_regular_price();
是正确的方法……