从Woocommerce中的functions.php文件显示添加到购物车按钮上的价格 [英] Display price on add to cart button from the functions.php file in Woocommerce
问题描述
曾经尝试寻找有关此内容的方法,但是到目前为止我在这里找到的解决方案都行不通.
Been trying to find something about this, but the solutions I found so far here do not work.
我需要在单个产品页面中显示添加到购物车"按钮,如下所示:
I need, in the single product page, to display the add to cart button like this:
添加到购物车-仅需$ PRICE
这需要通过子主题中的functions.php文件完成.
It needs to be done from the functions.php file in the child theme.
非常感谢!
推荐答案
要处理购物车中所有产品价格,其他归档页面和单个产品页面上的添加到购物车按钮中的产品价格,无需覆盖模板,只需使用专用的Woocommerce过滤器挂钩即可:
To handle the display of the product price in add-to-cart button for all product prices on shop, other archives pages and single product pages, without any need of overriding templates, using dedicated Woocommerce filter hooks:
add_filter( 'woocommerce_product_add_to_cart_text', 'custom_add_to_cart_price', 20, 2 ); // Shop and other archives pages
add_filter( 'woocommerce_product_single_add_to_cart_text', 'custom_add_to_cart_price', 20, 2 ); // Single product pages
function custom_add_to_cart_price( $button_text, $product ) {
// Variable products
if( $product->is_type('variable') ) {
// shop and archives
if( ! is_product() ){
$product_price = wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_variation_price() ) ) );
return $button_text . ' - From ' . strip_tags( $product_price );
}
// Single product pages
else {
return $button_text;
}
}
// All other product types
else {
$product_price = wc_price( wc_get_price_to_display( $product ) );
return $button_text . ' - Just ' . strip_tags( $product_price );
}
}
代码进入您的活动子主题(或活动主题)的function.php文件中.经过测试,可以正常工作.
Code goes in function.php file of your active child theme (or active theme). Tested and works.
在商店页面上:
在单个产品页面上:
这篇关于从Woocommerce中的functions.php文件显示添加到购物车按钮上的价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!