【问题标题】:Customizing the product sale flash badge自定义产品销售闪光徽章
【发布时间】:2017-08-10 13:42:02
【问题描述】:

我正在尝试使用下面的这个 sn-p 在销售 Flash 徽章上添加节省总额,但是由于它不起作用,所以出现了问题。 任何建议将不胜感激。

// Add save amount on the sale badge.
add_filter( 'woocommerce_sale_flash', 'woocommerce_custom_badge', 10, 2 );
function woocommerce_custom_badge( $price, $product ) {
$saved = wc_price( $product->regular_price - $product->sale_price );
return $price . sprintf( __(' <div class="savings">Save %s</div>', 'woocommerce' ), $saved );
}

谢谢

【问题讨论】:

    标签: php wordpress woocommerce product badge


    【解决方案1】:

    添加了 WC 3+ 兼容性

    您的过滤器中没有正确的参数(例如,$price 不存在),请参阅此处woocommerce_sale_flash 过滤器挂钩的相关源代码以更好地理解:

    /* 
     *  The filter hook woocommerce_sale_flash is located in:
     *  templates/loop/sale-flash.php and templates/single-product/sale-flash.php 
     */ 
    
    <?php if ( $product->is_on_sale() ) : ?>
    
    <?php echo apply_filters( 'woocommerce_sale_flash', '<span class="onsale">' . esc_html__( 'Sale!', 'woocommerce' ) . '</span>', $post, $product ); ?>
    

    所以你的工作代码将是这样的:

    add_filter( 'woocommerce_sale_flash', 'woocommerce_custom_badge', 10, 3 );
    function woocommerce_custom_badge( $output_html, $post, $product ) {
    
        // Added compatibility with WC +3
        $regular_price = method_exists( $product, 'get_regular_price' ) ? $product->get_regular_price() : $product->regular_price;
        $sale_price = method_exists( $product, 'get_sale_price' ) ? $product->get_sale_price() : $product->sale_price;
    
        $saved_price = wc_price( $regular_price - $sale_price );
        $output_html = '<span class="onsale">' . esc_html__( 'Save', 'woocommerce' ) . ' ' . $saved_price . '</span>';
    
        return $output_html;
    }
    

    代码位于活动子主题(或主题)的 function.php 文件中或任何插件文件中。

    此代码已经过测试并且可以工作。

    【讨论】:

    • 太棒了!似乎运作良好。谢谢 LoicTheAztec :)
    猜你喜欢
    • 2022-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-28
    • 2017-10-26
    • 2020-09-01
    • 1970-01-01
    相关资源
    最近更新 更多