woocommerce - 如何在不影响相关产品的情况下为单个产品添加自定义价格?
问题描述
我有代码可以用 2 条不同的消息更改所有产品上的自定义消息。请看一下。
add_filter('woocommerce_empty_price_html', 'show_alert_info_if_no_price');
function show_alert_info_if_no_price ($product){
if (is_product()) {
global $product;
$price = $product->get_price();
if ($price == '') {
ob_start();
// return for the product page
return '<div class="alert-info">Produk ini hanya dapat diproses melakukan pemesanan pembelian (PO). Segera hubungi tim kami. <a href="contactus.php">Kontak kami</a></div>';
} else {
// otherwise return short text as kontak kami
return 'Contact us';
}
}
}
现在我在相关产品上有问题。我需要相关的产品价格将如下所示:
//短文本作为 kontak kami
现在我被卡住了如何在使用 hooked 时添加另一个代码。
$woocommerce_loop['name'] != 'related').
任何帮助将不胜感激!
解决方案
您的代码中有一个错误,因为这个钩子只对空的价格执行。因此,要再次检查挂钩以获取空价格,如果不是,则运行 else 条件是无用的。
要在相关产品的单个产品页面上显示不同的文本,您可以使用global $woocommerce_loop
所以你得到:
function filter_woocommerce_empty_price_html( $html, $product ) {
global $woocommerce_loop;
// True on a single product page
if ( is_product() ) {
$html = '<div class="alert-info">Produk ini hanya dapat diproses melakukan pemesanan pembelian (PO). Segera hubungi tim kami. <a href="contactus.php">Kontak kami</a></div>';
// Related
if ( $woocommerce_loop['name'] == 'related' ) {
$html = __( 'Some other text', 'woocommerce' );
}
}
return $html;
}
add_filter( 'woocommerce_empty_price_html', 'filter_woocommerce_empty_price_html', 10, 2 );
推荐阅读
- arrays - 如何像在 Scala 中一样展平数组
- java - 转换为 Java 数组时 Java Spring JSON 解析错误
- android - 找不到带有哈希字符串“android-L”的目标
- linux - 在过去 15 分钟内创建的 Tar 文件并将它们 SCP 到我的本地计算机。(使用 Cygwin)
- java - spring mvc bootsrap modal uisng spring 标签库 getById() 和基于验证注释
- react-native - 反应导航硬件后退按钮从每个屏幕退出应用程序
- javascript - 具有特定语言环境的 Javascript 日期解析
- python - 在 Python 中创建一个带有特殊字符的文件夹
- php - PHP压缩/减少blob图像数据大小
- mysql - 如何在 MySQL 中将字符串列连接为唯一约束