简体   繁体   English

仅在 Woocommerce 存档页面上显示可变产品折扣百分比

[英]Display variable product discounted percentage only on Woocommerce archive pages

I'd like to display the percentage variable products are discounted in the archive pages.我想在存档页面中显示变量产品打折的百分比。 With the code below, I was able to get both the discounts % on variable products but also for simple products.使用下面的代码,我能够获得可变产品和简单产品的折扣百分比。 Can I do this ONLY for variable products and not simple products?我可以仅对可变产品而不是简单产品执行此操作吗? I realize it's probably a simple adjustment in the code but I can't figure it out because I'm an idiot when it comes to PHP.我意识到这可能是代码中的一个简单调整,但我无法弄清楚,因为我在 PHP 方面是个白痴。

add_action( 'woocommerce_after_shop_loop_item', 'show_sale_percentage', 25 );

function show_sale_percentage() {

global $product;

if ( $product->is_on_sale() ) {

if ( ! $product->is_type( 'variable' ) ) {

$max_percentage = ( ( $product->get_regular_price() - $product->get_sale_price() ) / $product->get_regular_price() ) * 100;

} else {

$max_percentage = 0;

foreach ( $product->get_children() as $child_id ) {
$variation = wc_get_product( $child_id );
$price = $variation->get_regular_price();
$sale = $variation->get_sale_price();
if ( $price != 0 && ! empty( $sale ) ) $percentage = ( $price - $sale ) / $price * 100;
if ( $percentage > $max_percentage ) {
    $max_percentage = $percentage;
}
}

}

echo "<div class='saved-sale'>-" . round($max_percentage) . "%</div>";

}
}

To display the on sale percentage, on archives pages, for variable products only, try the following:要在档案页面上仅针对可变产品显示销售百分比,请尝试以下操作:

add_action( 'woocommerce_after_shop_loop_item', 'loop_variable_product_sale_percentage', 25 );
function loop_variable_product_sale_percentage() {
    global $product;

    if ( $product->is_on_sale() && $product->is_type( 'variable' ) ) {
        $max_percentage = 0;

        foreach ( $product->get_children() as $child_id ) {
            $variation = wc_get_product( $child_id );
            $percentage = 0;

            $price = $variation->get_regular_price();
            $sale = $variation->get_sale_price();

            if ( $price != 0 && ! empty( $sale ) ) {
                $percentage = ( $price - $sale ) / $price * 100;
            }
            if ( $percentage > $max_percentage ) {
                $max_percentage = $percentage;
            }
        }
        echo '<div class="saved-sale">-' . round($max_percentage) . '%</div>';
    }
}

Code goes in function.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 function.php 文件中。 Tested and works.测试和工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将折扣百分比添加到Woocommerce中的可变产品价格范围 - Add the discounted percentage to variable product price range in Woocommerce 在Woocommerce存档页面中显示特定产品属性 - Display specific product attribute in Woocommerce archive pages 如何添加 CSS class 以在 WooCommerce 中的单个产品页面上显示接近销售价格的折扣百分比 - How to add a CSS class for the displayed discounted percentage near sale price on single product pages in WooCommerce 显示 Woocommerce 产品的折扣价和百分比 - Display the discounted price and percentage on Woocommerce products 在 WooCommerce 中显示最低变动价格和折扣百分比 - Display lowest variation price and discounted percentage in WooCommerce 显示 Woocommerce 产品的默认折扣价和百分比 - Display the default discounted price and percentage on Woocommerce products 在WC 3.0+的单一产品页面中显示接近销售价格的折扣百分比 - Display the discounted percentage near sale price in Single product pages for WC 3.0+ 在 Woocommerce 存档页面中的产品标题下显示特定的产品属性 - Display specific product attributes under product title in Woocommerce archive pages 在 Woocommerce 存档页面中显示所有产品类型的库存可用性 - Display the stock availability for all product types in Woocommerce archive pages 在 WooCommerce 产品存档页面上显示自定义分类术语 - Display custom taxonomy terms on WooCommerce product archive pages
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM