在 WooCommerce 的单个产品页面和商店存档页面上将销售徽章添加到产品缩略图
Add sale badge to product thumbnail on single product page and shop archive pages in WooCommerce
我使用以下代码在每个带有 -%off 的产品的左上角添加一个小徽章。
add_filter( 'woocommerce_single_product_image_thumbnail_html', 'filter_woocommerce_single_product_image_thumbnail_html', 10, 2 );
function filter_woocommerce_single_product_image_thumbnail_html( $thumbnail, $thumbnail_id ) {
global $post, $woocommerce;
$product = wc_get_product( $post->ID );
$is_on_sale = $product->is_on_sale();
if ( $is_on_sale ) {
$rrp_price = $product->get_regular_price();
$sale_price = $product->get_price();
$discount_amount = $rrp_price - $sale_price;
$discount_percent = '-' . round( ( $discount_amount / $rrp_price ) * 100, 0 ) . '%';
$thumbnail = '<span class="wpd-sale-thumbnail"><span class="wpd-sale-badge">' . $discount_percent . '</span>' . $thumbnail . '</span>';
}
return $thumbnail;
}
在我的产品页面上,它有效。但是,当我尝试使用以下代码在所有产品列表的类别页面上执行此操作时:
add_filter( 'post_thumbnail_html', 'filter_woocommerce_archive_product_image_thumbnail_html', 30, 5 );
function filter_woocommerce_archive_product_image_thumbnail_html( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
$product = wc_get_product( $post_id );
if ( ! is_a( $product, 'WC_Product' ) ) {
return $html;
}
$is_on_sale = $product->is_on_sale();
if ( $is_on_sale && is_product_category() ) {
$rrp_price = $product->get_regular_price();
$sale_price = $product->get_price();
$discount_amount = $rrp_price - $sale_price;
$discount_percent = '-' . round( ( $discount_amount / $rrp_price ) * 100, 0 ) . '%';
$html = '<span class="wpd-sale-thumbnail"><span class="wpd-sale-badge">' . $discount_percent . '</span>' . $html . '</span>';
}
return $html;
}
没有用,好像没有什么调整。我可以得到一些指导吗?
要将徽章添加到存档页面上的产品图片,您可以使用
/**
* Get the product thumbnail for the loop.
*/
function woocommerce_template_loop_product_thumbnail() {
$w_g_p_t = woocommerce_get_product_thumbnail();
global $product;
// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
// On sale
$is_on_sale = $product->is_on_sale();
// True
if ( $is_on_sale ) {
$rrp_price = (int) $product->get_regular_price();
$sale_price = (int) $product->get_price();
// Greater than
if ( $rrp_price > $sale_price ) {
$discount_amount = $rrp_price - $sale_price;
$discount_percent = '-' . round( ( $discount_amount / $rrp_price ) * 100, 0 ) . '%';
$w_g_p_t = '<span class="wpd-sale-thumbnail"><span class="wpd-sale-badge">' . $discount_percent . '</span>' . $w_g_p_t . '</span>';
}
}
}
echo $w_g_p_t;
}
或
function action_woocommerce_before_shop_loop_item_title() {
// Removes a function from a specified action hook.
remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10 );
global $product;
// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
$size = 'woocommerce_thumbnail';
$image_size = apply_filters( 'single_product_archive_thumbnail_size', $size );
$get_image = $product->get_image( $image_size );
// On sale
$is_on_sale = $product->is_on_sale();
// True
if ( $is_on_sale ) {
$rrp_price = (int) $product->get_regular_price();
$sale_price = (int) $product->get_price();
// Greater than
if ( $rrp_price > $sale_price ) {
$discount_amount = $rrp_price - $sale_price;
$discount_percent = '-' . round( ( $discount_amount / $rrp_price ) * 100, 0 ) . '%';
$get_image = '<span class="wpd-sale-thumbnail"><span class="wpd-sale-badge">' . $discount_percent . '</span>' . $get_image . '</span>';
}
}
}
echo $product ? $get_image : '';
}
add_action( 'woocommerce_before_shop_loop_item_title', 'action_woocommerce_before_shop_loop_item_title', 9 );
我也重写了你现有的代码(针对单品页面)
function filter_woocommerce_single_product_image_thumbnail_html( $html, $post_thumbnail_id ) {
// Get the global product object
global $product;
// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
// On sale
$is_on_sale = $product->is_on_sale();
// True
if ( $is_on_sale ) {
$rrp_price = (int) $product->get_regular_price();
$sale_price = (int) $product->get_price();
// Greater than
if ( $rrp_price > $sale_price ) {
$discount_amount = $rrp_price - $sale_price;
$discount_percent = '-' . round( ( $discount_amount / $rrp_price ) * 100, 0 ) . '%';
$html = '<span class="wpd-sale-thumbnail"><span class="wpd-sale-badge">' . $discount_percent . '</span>' . $html . '</span>';
}
}
}
return $html;
}
add_filter( 'woocommerce_single_product_image_thumbnail_html', 'filter_woocommerce_single_product_image_thumbnail_html', 10, 2 );
我使用以下代码在每个带有 -%off 的产品的左上角添加一个小徽章。
add_filter( 'woocommerce_single_product_image_thumbnail_html', 'filter_woocommerce_single_product_image_thumbnail_html', 10, 2 );
function filter_woocommerce_single_product_image_thumbnail_html( $thumbnail, $thumbnail_id ) {
global $post, $woocommerce;
$product = wc_get_product( $post->ID );
$is_on_sale = $product->is_on_sale();
if ( $is_on_sale ) {
$rrp_price = $product->get_regular_price();
$sale_price = $product->get_price();
$discount_amount = $rrp_price - $sale_price;
$discount_percent = '-' . round( ( $discount_amount / $rrp_price ) * 100, 0 ) . '%';
$thumbnail = '<span class="wpd-sale-thumbnail"><span class="wpd-sale-badge">' . $discount_percent . '</span>' . $thumbnail . '</span>';
}
return $thumbnail;
}
在我的产品页面上,它有效。但是,当我尝试使用以下代码在所有产品列表的类别页面上执行此操作时:
add_filter( 'post_thumbnail_html', 'filter_woocommerce_archive_product_image_thumbnail_html', 30, 5 );
function filter_woocommerce_archive_product_image_thumbnail_html( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
$product = wc_get_product( $post_id );
if ( ! is_a( $product, 'WC_Product' ) ) {
return $html;
}
$is_on_sale = $product->is_on_sale();
if ( $is_on_sale && is_product_category() ) {
$rrp_price = $product->get_regular_price();
$sale_price = $product->get_price();
$discount_amount = $rrp_price - $sale_price;
$discount_percent = '-' . round( ( $discount_amount / $rrp_price ) * 100, 0 ) . '%';
$html = '<span class="wpd-sale-thumbnail"><span class="wpd-sale-badge">' . $discount_percent . '</span>' . $html . '</span>';
}
return $html;
}
没有用,好像没有什么调整。我可以得到一些指导吗?
要将徽章添加到存档页面上的产品图片,您可以使用
/**
* Get the product thumbnail for the loop.
*/
function woocommerce_template_loop_product_thumbnail() {
$w_g_p_t = woocommerce_get_product_thumbnail();
global $product;
// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
// On sale
$is_on_sale = $product->is_on_sale();
// True
if ( $is_on_sale ) {
$rrp_price = (int) $product->get_regular_price();
$sale_price = (int) $product->get_price();
// Greater than
if ( $rrp_price > $sale_price ) {
$discount_amount = $rrp_price - $sale_price;
$discount_percent = '-' . round( ( $discount_amount / $rrp_price ) * 100, 0 ) . '%';
$w_g_p_t = '<span class="wpd-sale-thumbnail"><span class="wpd-sale-badge">' . $discount_percent . '</span>' . $w_g_p_t . '</span>';
}
}
}
echo $w_g_p_t;
}
或
function action_woocommerce_before_shop_loop_item_title() {
// Removes a function from a specified action hook.
remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10 );
global $product;
// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
$size = 'woocommerce_thumbnail';
$image_size = apply_filters( 'single_product_archive_thumbnail_size', $size );
$get_image = $product->get_image( $image_size );
// On sale
$is_on_sale = $product->is_on_sale();
// True
if ( $is_on_sale ) {
$rrp_price = (int) $product->get_regular_price();
$sale_price = (int) $product->get_price();
// Greater than
if ( $rrp_price > $sale_price ) {
$discount_amount = $rrp_price - $sale_price;
$discount_percent = '-' . round( ( $discount_amount / $rrp_price ) * 100, 0 ) . '%';
$get_image = '<span class="wpd-sale-thumbnail"><span class="wpd-sale-badge">' . $discount_percent . '</span>' . $get_image . '</span>';
}
}
}
echo $product ? $get_image : '';
}
add_action( 'woocommerce_before_shop_loop_item_title', 'action_woocommerce_before_shop_loop_item_title', 9 );
我也重写了你现有的代码(针对单品页面)
function filter_woocommerce_single_product_image_thumbnail_html( $html, $post_thumbnail_id ) {
// Get the global product object
global $product;
// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
// On sale
$is_on_sale = $product->is_on_sale();
// True
if ( $is_on_sale ) {
$rrp_price = (int) $product->get_regular_price();
$sale_price = (int) $product->get_price();
// Greater than
if ( $rrp_price > $sale_price ) {
$discount_amount = $rrp_price - $sale_price;
$discount_percent = '-' . round( ( $discount_amount / $rrp_price ) * 100, 0 ) . '%';
$html = '<span class="wpd-sale-thumbnail"><span class="wpd-sale-badge">' . $discount_percent . '</span>' . $html . '</span>';
}
}
}
return $html;
}
add_filter( 'woocommerce_single_product_image_thumbnail_html', 'filter_woocommerce_single_product_image_thumbnail_html', 10, 2 );