微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php-自定义产品销售Flash徽章

我试图在下面使用此代码段在销售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 );
}

谢谢

解决方法:

Added WC 3+ compatibility

您的过滤器中没有正确的参数(例如$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文件中或任何插件文件中.

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐