【问题标题】:WooCommerce Filter: Sale Percent Off [duplicate]WooCommerce 过滤器:销售折扣 [重复]
【发布时间】:2017-08-19 03:43:05
【问题描述】:

我正在尝试创建一个可以插入到 Wordpress 中的 functions.php 文件中的单个 PHP 函数(使用 WooCommerce 插件,店面主题)。我在网上找到了两段代码,它们分别完成了我想要实现的部分,但我不知道如何将它们组合在一起。只是一个免责声明我不知道如何很好地阅读或编写 PHP。

希望这张图片有助于澄清我想要实现的目标。左侧表示仅使用第一段代码时页面的外观,右侧表示当两段代码组合时我希望页面的外观:

下面看到的第一段代码正确地获取了产品的正常价格及其销售价格,然后计算客户将获得的折扣。最终结果是一个字符串,例如“22% 折扣”。问题是这个字符串是在显示价格之后插入的,而不是在销售横幅本身中。

function woocommerce_saved_sales_price( $price, $product ) {
$percentage = round( ( ( $product->regular_price - $product->sale_price ) / $product->regular_price ) * 100 );
return $price . sprintf( __('%s', 'woocommerce' ), $percentage . '% OFF' );
}
add_filter( 'woocommerce_get_price_html', 'woocommerce_saved_sales_price', 10, 2 );

第二段代码实际上编辑了我要更改的销售横幅,但我不知道如何让它包含另一个过滤器实现的计算折扣。

add_filter( 'woocommerce_sale_flash', 'wc_custom_replace_sale_text' );
function wc_custom_replace_sale_text( $html ) {
return str_replace( __( 'Sale!', 'woocommerce' ), __( 'SALE! [INSERT % OFF OUTPUT HERE]', 'woocommerce' ), $html );
}

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    我认为第二段代码应该是

    add_filter( 'woocommerce_sale_flash', 'wc_custom_replace_sale_text' );
    function wc_custom_replace_sale_text( $html ) {
      global $product;
      //$product = wc_get_product( $product->get_id() );
      $percentage = round( ( ( $product->get_regular_price() - $product->get_sale_price() ) / $product->get_regular_price() ) * 100 );
      return str_replace( __( 'Sale!', 'woocommerce' ), __( 'SALE! ', 'woocommerce' ).$percentage.'% OFF', $html );
    }
    

    测试和工作

    【讨论】:

    • 可以确认这项工作。很棒的工作,非常感谢!
    • 祝我的朋友好运:D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-04
    • 2020-12-31
    • 2018-05-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多