ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

php-在WooCommerce中选择变化的销售价格后显示折扣百分比

2019-10-13 07:33:28  阅读:205  来源: 互联网

标签:php wordpress woocommerce product price


我在下面的这段代码中显示了用户选择了价格变化后的价格变化

add_filter( 'woocommerce_show_variation_price', 'filter_show_variation_price', 10, 3 );
function filter_show_variation_price( $condition, $product, $variation ){
    if( $variation->get_price() === "" ) return false;
    else return true;
}

我需要计算将显示的促销价格和正常价格之间的折扣百分比.但是仅在选择变体之后,而不是之前(因为我将从通过css之前显示的价格中删除价格).

我认为this answer接近了,但还没有到.

解决方法:

全部以下代码仅适用于单个产品页面上的可变产品.

我以前有女仆提供的链接答案中的第二个代码,效果很好.我做了一些小改动,只针对可变产品.

我添加了一个附加的挂钩函数,该函数将仅删除可变产品标题下的显示价格.

这是所有需要的代码:

// Always Display the selected variation price for variable products (already working)
add_filter( 'woocommerce_show_variation_price', 'filter_show_variation_price', 10, 3 );
function filter_show_variation_price( $condition, $product, $variation ){
    if( $variation->get_price() === "" ) return false;
    else return true;
}

// Remove the displayed price from variable products in single product pages only
add_action( 'woocommerce_single_product_summary', 'remove_the_displayed_price_from_variable_products', 9 );
function remove_the_displayed_price_from_variable_products() {
    global $product;

    // Just for variable products
    if( ! $product->is_type('variable') ) return;

    // Remove the displayed price from variable products
    remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
}

// Display the selected variation discounted price with the discounted percentage for variable products
add_filter( 'woocommerce_format_sale_price', 'woocommerce_custom_sales_price', 10, 3 );
function woocommerce_custom_sales_price( $price, $regular_price, $sale_price ) {
    global $product;

    // Just for variable products on single product pages
    if( $product->is_type('variable') && is_product() ) {

        // Getting the clean numeric prices (without html and currency)
        $regular_price = floatval( strip_tags($regular_price) );
        $sale_price = floatval( strip_tags($sale_price) );

        // Percentage calculation and text
        $percentage = round( ( $regular_price - $sale_price ) / $regular_price * 100 ).'%';
        $percentage_txt = __(' Save ', 'woocommerce' ).$percentage;

        return '<del>' . wc_price( $regular_price ) . '</del> <ins>' . wc_price( $sale_price ) . $percentage_txt . '</ins>';
    }
    return $price;
}

代码在您的活动子主题(或主题)的function.php文件中,或者在任何插件文件中.

此代码已经过测试,并且仅适用于WooCommerce版本3

标签:php,wordpress,woocommerce,product,price
来源: https://codeday.me/bug/20191013/1906151.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有