php - 在 Woocommerce 预订中显示折扣价
问题描述
我想显示折扣价(添加范围后的价格)以及可预订产品的基本价格我正在做的是更改 \woocommerce-bookings\includes\adminclass-wc-bookings-ajax.php 中的以下代码文件
// Build the output
$before = $product->get_price();
$after = wc_price( $display_price ) ;
$discount = $product->get_price() - wc_price( $display_price );
$output = apply_filters( 'woocommerce_bookings_booking_cost_string', __( 'Booking cost', 'woocommerce-bookings' ), $product ) .$discount ': <strong>' .$discount . $price_suffix . '</strong>';
这是正确的方法还是你能提出什么建议?
解决方案
覆盖 Woocommerce Bookings 核心代码是绝对不应该做的事情,原因有很多,我不会解释。
现在,正如您在该源代码中看到的那样,开发人员添加了一个过滤器挂钩,以允许您对$output
变量进行更改。
现在WooCommerce 可预订产品没有任何折扣价格,因为 WooCommerce 其他产品也有。
所以首先放回原始插件文件。
然后您可以按如下方式使用过滤器(您将在其中添加自己的代码):
add_filter( 'woocommerce_bookings_booking_cost_string', 'change_bookings_booking_cost_string', 10, 2 );
function change_bookings_booking_cost_string( $cost_string, $product ) {
$raw_price = $product->get_price();
$display_price = wc_get_price_to_display($product);
$price_suffix = $product->get_price_suffix();
$formatted_price = wc_price($display_price) . $price_suffix;
$additional_output = 'Here comes your code'; // Replace by your code variables
return $cost_string . ' ' . $additional_output; // Always return (never echo)
}
代码在您的活动子主题(或活动主题)的functions.php 文件中。
推荐阅读
- ios - 在 iOS 应用程序中解析 protobuf 文件
- c# - 使用 Nuget.VisualStudio 如何从存储库中获取包列表
- javascript - Inputosaurus / jQuery 加载时间
- node.js - AWS Elastic Beanstalk 上的 Dockerized 节点。错误 502 BadGateway
- c++ - 断言中的折叠表达式在某些机器上编译,但在其他机器上不编译
- python - Python3 导入类
- vb.net - 将数据网格列添加到数据库?
- ios - 如何向图层添加个性化注释?地图盒 [斯威夫特]
- mariadb - SQL获得最大值和最小值之间的最小差异
- xml - 从 WS 导入 XML 并在 VBA 中的访问表中解析