追加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); ?>
01すべてのプラグインファイルでも、あなたのアクティブな子テーマ(またはテーマ)のfunction.phpファイル内のコードが行く
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;
}
か:
だからあなたの作業コードのようなものになるだろう。
このコードはテスト済みであり、動作します。
優秀!うまくいっているようだ。 ありがとうLoicTheAztec :) – user7734269