在 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屋!