首页 > 解决方案 > 如何使用“WooCommerce 的折扣规则”插件 woocommerce 以销售百分比和主页显示销售额

问题描述

使用时,在文件Discount Rules for WooCommerce中使用此钩子代码时,我在每个产品上都获得 100% 的折扣。function.php但是它通过销售完美地展示所有东西是完美的,但是当我使用钩子时,它没有以百分比格式显示产品的完美销售,只显示每种产品的 100% 折扣。现在我想以正确的值获得百分比格式的折扣。当我到处搜索然后发现我的钩子 woocommerce_sale_flash但它只适用于 woocommerce 插件我什么Discount Rules for WooCommerce插件。但是在Discount Rules for wooCommerce插件中我没有找到任何钩子来查看产品左上角的百分比格式的折扣。

add_filter( 'woocommerce_sale_flash', 'ask_percentage_sale', 11, 3 );
function ask_percentage_sale( $text, $post, $product ) {
    $discount = 0;
    if ( $product->get_type() == 'variable' ) {
        $available_variations = $product->get_available_variations(); 
        $maximumper = 0;
        for ($i = 0; $i < count($available_variations); ++$i) {
            $variation_id=$available_variations[$i]['variation_id'];
            $variable_product1= new WC_Product_Variation( $variation_id );
            $regular_price = $variable_product1->get_regular_price();
            $sales_price = $variable_product1->get_sale_price();
            if( $sales_price ) {
                $percentage= round( ( ( $regular_price - $sales_price ) / $regular_price ) * 100 ) ;
                if ($percentage > $maximumper) {
                    $maximumper = $percentage;
                }
            }        
        }
        $text = '<span class="onsale">' . $maximumper  . '%</span>';
    } elseif ( $product->get_type() == 'simple' ) {
        $percentage = round( ( ( $product->get_regular_price() - $product->get_sale_price() ) / $product->get_regular_price() ) * 100 );
        $text = '<span class="onsale">' . $percentage . '% off</span>';
    }   
 
    return $text;
}

我尝试使用此代码,但它以 woocommerce 销售产品插件的正确百分比显示,但对于“Woo 折扣规则”未显示正确百分比,它始终显示 100% 百分比。请告诉我任何获得百分比折扣的方向。

标签: wordpresswoocommerce

解决方案


推荐阅读