php - WooCommerce 购物车小计:显示“免费”而不是“0,00”
问题描述
我正在寻找在 WooCommerce 购物车/结帐中显示“免费”而不是 €0,00 的解决方案。
我知道,Single Produkt 本身有一个片段,但是即使在结帐/购物车页面上的小计/总计计算中,是否有可能更改价格标签?
add_filter( 'woocommerce_get_price_html', 'price_free_zero_empty', 9999, 2 );
function price_free_zero_empty( $price, $product ){
if ( '' === $product->get_price() || 0 == $product->get_price() ) {
$price = '<span class="woocommerce-Price-amount amount">FREE</span>';
}
return $price;
}
解决方案
尝试使用以下在产品价格为零时应显示“免费”的内容:
// Cart and minicart
add_filter( 'woocommerce_cart_item_price', 'change_cart_item_price_html', 10, 3 );
function change_cart_item_price_html( $price_html, $cart_item, $cart_item_key ) {
if( $cart_item['data']->get_price() == 0 ) {
return '<span class="woocommerce-Price-amount amount">'.__("FREE", "woocommerce").'</span>';
}
return $price_html;
}
// Cart and Checkout
add_filter( 'woocommerce_cart_item_subtotal', 'change_checkout_item_subtotal_html', 10, 3 );
function change_checkout_item_subtotal_html( $subtotal_html, $cart_item, $cart_item_key ) {
if( $cart_item['data']->get_price() == 0 ) {
return '<span class="woocommerce-Price-amount amount">'.__("FREE", "woocommerce").'</span>';
}
return $subtotal_html;
}
代码在您的活动子主题(或活动主题)的functions.php 文件中。它应该有效。
推荐阅读
- javascript - 如何使用基于 id 的分组数据从另一个数组创建一个数组?
- python - 使用 django 在 Qrcode 中的用户名和密码
- php - 从文本文件生成的链式下拉列表
- php - 按其中包含的字符串对数组进行排序(PHP)
- java - 如何从手机图库中获取图像到我的应用程序中
- c++ - 试图打印出最小堆最后一层的元素
- reactjs - 在 react/next 中使用 bootbox
- python - 使用多个变量预测每批次一个标签 (LSTM)
- c++ - 带有 RSA 加密的 SHA-256
- android - 在 Android Manifest xml 中询问许可后隐藏应用程序图标时出错