在 WooCommerce 单一产品页面中显示特价产品的节省金额

问题描述 投票:0回答:1

我使用以下代码来显示简单和可变产品的折扣百分比,效果很好:

add_action( 'woocommerce_before_shop_loop_item_title', 'bbloomer_show_sale_percentage_loop', 25 );

function bbloomer_show_sale_percentage_loop() {

  global $product;

  if ( !$product->is_on_sale() ) return;

  if ( $product->is_type( 'simple' ) ) {

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

  } elseif ( $product->is_type( 'variable' ) ) {

    $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;

      }

    }

  }

  if ( $max_percentage > 0 )echo "<p class='sale-perc'>" . round( $max_percentage ) . "%   Discount</p>";
}

模板页面如何调用:

 <?php bbloomer_show_sale_percentage_loop(); ?>

现在我想计算折扣金额,以向客户展示购买该产品的好处。

我找到了一个代码来实现这一点,但是这个代码有两个主要问题:

第一:仅适用于简单的产品。

第二:将此代码添加到functions.php文件中,WooCommerce支付页面将崩溃。

是否有更好的代码或解决方案,以便我可以计算并显示所有 WooCommerce 产品的客户利润(简单且可变)?

显示客户购买简单产品的利润的代码:

add_action( 'woocommerce_single_product_summary', 'simple_product_saving_amount', 11 );
function simple_product_saving_amount() {
    global $product;
    if( $product->is_type('simple') && $product->is_on_sale() ) {
        $regular_price = (float) wc_get_price_to_display( $product, array('price' => $product->get_regular_price() ) );
        $active_price  = (float) wc_get_price_to_display( $product, array('price' => $product->get_sale_price() ) );
        $saved_amount  = $regular_price - $active_price;
        echo '<p id=&quotsaving_total_price&quot>'. __(&quot your profit &quot) .': ' . wc_price($saved_amount) . ' </p>';}
}
php wordpress woocommerce product-variations
1个回答
1
投票

您的代码的问题是多个

&quot
应该替换为函数内最后一行中的
"

因此,单个产品页面上简单产品的正确工作代码:

add_action( 'woocommerce_single_product_summary', 'simple_product_saving_amount', 11 );
function simple_product_saving_amount() {
    global $product;

    if( $product->is_type('simple') && $product->is_on_sale() ) {
        $args = array( 'price' => $product->get_regular_price() );
        if ( 'incl' === get_option('woocommerce_tax_display_shop') ) {
            $regular_price = wc_get_price_including_tax( $product, $args );
            $active_price  = wc_get_price_including_tax( $product );
        } else {
            $regular_price = wc_get_price_excluding_tax( $product, $args );
            $active_price  = wc_get_price_excluding_tax( $product );
        }
        $saved_amount  = $regular_price - $active_price;
        printf( '<p id="saving_total_price">%s %s</p>', __('Your profit:'), wc_price( $saved_amount ) );
    }
}

现在,对于单变量产品页面中的产品变体,您将使用:

add_action( 'woocommerce_available_variation', 'product_variation_saving_amount', 10, 3 );
function product_variation_saving_amount( $variation_data, $product, $variation ) {
    if ( $variation->is_on_sale() ) {
        $args = array( 'price' => $variation->get_regular_price() );
        if ( 'incl' === get_option('woocommerce_tax_display_shop') ) {
            $regular_price = wc_get_price_including_tax( $variation, $args );
            $active_price  = wc_get_price_including_tax( $variation );
        } else {
            $regular_price = wc_get_price_excluding_tax( $variation, $args );
            $active_price  = wc_get_price_excluding_tax( $variation );
        }

        $saving_html   = sprintf( '<p id="saving_total_price">%s %s</p>', __('Your profit:'), 
            wc_price($regular_price - $active_price) );
        
            $variation_data['price_html'] .= '<br>' . $saving_html;
    }
    return $variation_data;
}

代码位于活动子主题(或活动主题)的functions.php 文件中。已测试并有效。

© www.soinside.com 2019 - 2024. All rights reserved.