首页 > 解决方案 > 在 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>';

这是正确的方法还是你能提出什么建议?

标签: phpwordpresswoocommercehook-woocommercewoocommerce-bookings

解决方案


覆盖 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 文件中。


推荐阅读