【问题标题】:Customize WooCommerce single products but not on other product loops自定义 WooCommerce 单一产品,但不在其他产品循环上
【发布时间】:2021-04-19 23:56:51
【问题描述】:

我的 woocommerce 价格挂钩中有一些自定义代码,它可以在网站上的任何地方进行更改。我只想让它影响产品页面(目前我们所有的产品都是简单的产品)。

在设计产品集合页面时,下面的相同 sn-p 会出现在产品页面上。我尝试应用 if 语句仅在产品页面上对 woocommerce_format_sale_price 进行更改,但这似乎不起作用。

有什么建议吗?

function add_discount_woocommerce_format_sale_price( $price, $regular_price, $sale_price ){
    $percentage = round( ( $regular_price - $sale_price ) / $regular_price * 100 ).'%';

    $price = '<ins>' . ( is_numeric( $sale_price ) ? wc_price( $sale_price ) : $sale_price ) . '</ins> <del>' . ( is_numeric( $regular_price ) ? wc_price( $regular_price ) : $regular_price ) . '</del><br><span class="plan-atc">*Payment Plan Available at Checkout</span><br> <span class="below-atc">Add to cart today, get ' . $percentage . __( ' off </span>', 'text-domain' );
    return $price;
}
add_filter( 'woocommerce_format_sale_price', 'add_discount_woocommerce_format_sale_price', 99, 3 );

谢谢!

【问题讨论】:

    标签: php wordpress loops woocommerce hook-woocommerce


    【解决方案1】:

    您可以使用is_product() 条件函数,但这还不够,因为单个产品页面上还有其他产品循环,例如追加销售产品、相关产品……

    要使您的功能仅对单个产品页面(而不是其他产品循环)有效,请使用以下内容:

    add_filter( 'woocommerce_format_sale_price', 'display_percentage_discount_on_sale_price_format', 99, 3 );
    function display_percentage_discount_on_sale_price_format( $price, $regular_price, $sale_price ){
        global $woocommerce_loop;
    
        // Only on single product pages (and not on other product loops)
        if( is_product() && isset($woocommerce_loop['name']) && empty($woocommerce_loop['name']) ) {
    
            $percentage = round( ( $regular_price - $sale_price ) / $regular_price * 100 ).'%';
    
            $price = '<ins>' . ( is_numeric( $sale_price ) ? wc_price( $sale_price ) : $sale_price ) . '</ins> <del>' . ( is_numeric( $regular_price ) ? wc_price( $regular_price ) : $regular_price ) . '</del><br><span class="plan-atc">*Payment Plan Available at Checkout</span><br> <span class="below-atc">Add to cart today, get ' . $percentage . __( ' off </span>', 'text-domain' );
        }
        return $price;
    }
    

    现在缺少关闭 &lt;/span&gt;。请尝试以下重新访问的代码:

    add_filter( 'woocommerce_format_sale_price', 'display_percentage_discount_on_sale_price_format', 99, 3 );
    function display_percentage_discount_on_sale_price_format( $price, $regular_price, $sale_price ){
        global $woocommerce_loop;
    
        // Only on single product pages (and not on other product loops)
        if( is_product() && isset($woocommerce_loop['name']) && empty($woocommerce_loop['name']) ) {
            $percentage  = round( ( $regular_price - $sale_price ) / $regular_price * 100 ).'%';
            $text_end    = sprintf( __("Add to cart today, get %s off", "woocommerce"), $percentage );
            $text_prefix = sprintf( ' <br><span class="plan-atc">%s</span> <br><span class="below-atc">%s</span>',
            __("Payment Plan Available at Checkout", "woocommerce"), $text_end );
    
            $price_html = '<ins>' . ( is_numeric( $sale_price ) ? wc_price( $sale_price ) : $sale_price ) . '</ins> <del>' . ( is_numeric( $regular_price ) ? wc_price( $regular_price ) : $regular_price ) . '</del>' . $text_prefix;
        }
        return $price_html;
    }
    

    代码进入活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。

    【讨论】:

      猜你喜欢
      • 2019-07-27
      • 2022-11-05
      • 2018-08-21
      • 2021-07-01
      • 2022-01-20
      • 2014-10-17
      • 2018-04-30
      • 2020-11-13
      • 2021-01-31
      相关资源
      最近更新 更多