php – 在产品发售时将自定义文本标签添加到价格中

在WooCommerce产品单页上,如果产品拥有销售价格,正常价格被划掉并在其后面,则突出显示销售价格.

我的问题:
如何添加“旧价格:XX美元”和“新价格:XX美元”等标签,而不仅仅是划掉的价格和新价格(销售价格)?

Update 2 (for simple and variable products + solved the bug on same variations prices)

当产品在销售时,您可以使用挂在woocommerce_sale_price_html和woocommerce_variation_sale_price_html过滤器钩子中的自定义函数(对于简单和变量产品)添加自定义标签.

对于变量产品的最小/最大价格,我们需要一个挂在woocommerce_variation_sale_price_html过滤器钩子中的不同函数.

这是代码:

add_filter('woocommerce_variation_sale_price_html','sale_prices_custom_labels', 10, 2 );
add_filter('woocommerce_sale_price_html','sale_prices_custom_labels', 10, 2 );
function sale_prices_custom_labels( $price, $product ){
    if (isset($product->sale_price)) {
        $price = '<del class="strike">' . __('Old Price: ', 'woocommerce' ) . woocommerce_price( $product->regular_price ). '</del>
        <ins class="highlight">' . __('New Price: ', 'woocommerce' ) . woocommerce_price( $product->sale_price ) . '</ins>';
    }
    else
    {
        $price = '<ins class="highlight">'.woocommerce_price( $product->regular_price ).'</ins>';
    }
    return $price;
}

add_filter('woocommerce_variable_sale_price_html', 'sale_prices_custom_labels_min_max', 20, 2);
function sale_prices_custom_labels_min_max( $price, $product) {

    $variation_min_reg_price = $product->get_variation_regular_price('min', true);
    $variation_max_reg_price = $product->get_variation_regular_price('max', true);
    $variation_min_sale_price = $product->get_variation_sale_price('min', true);
    $variation_max_sale_price = $product->get_variation_sale_price('max', true);

    if ( $variation_min_reg_price != $variation_min_sale_price || $variation_max_reg_price != $variation_max_sale_price )
    {
        if($variation_min_reg_price == $variation_max_reg_price && $variation_min_sale_price == $variation_max_sale_price ){
            $price = '<del class="strike">' . __('Old Price: ', 'woocommerce' ) . woocommerce_price($variation_max_reg_price) . '</del>
            <ins class="highlight">' . __('New Price: ', 'woocommerce' ) . woocommerce_price($variation_max_sale_price) . '</ins>';
        }
        elseif($variation_min_reg_price != $variation_max_reg_price && $variation_min_sale_price == $variation_max_sale_price )
        {
            $price = '<del class="strike">' . __('Old Price: ', 'woocommerce' ) . woocommerce_price($variation_min_reg_price) . '-' . woocommerce_price($variation_max_reg_price) . '</del>
            <ins class="highlight">' . __('New Price: ', 'woocommerce' ) . woocommerce_price($variation_max_sale_price) . '</ins>';
        }
        elseif($variation_min_reg_price == $variation_max_reg_price && $variation_min_sale_price != $variation_max_sale_price )
        {
            $price = '<del class="strike">' . __('Old Price: ', 'woocommerce' ) . woocommerce_price($variation_max_reg_price) . '</del>
            <ins class="highlight">' . __('New Price: ', 'woocommerce' ) . woocommerce_price($variation_min_sale_price) . '-' . woocommerce_price($variation_max_sale_price) . '</ins>';
        }
        else
        {
        $price = '<del class="strike">' . __('Old Price: ', 'woocommerce' ) . woocommerce_price($variation_min_reg_price) . '-' . woocommerce_price($variation_max_reg_price) . '</del>
        <ins class="highlight">' . __('New Price: ', 'woocommerce' ) . woocommerce_price($variation_min_sale_price) . '-' . woocommerce_price($variation_max_sale_price) . '</ins>';
        }
    }
    return $price;
}

You can also replace the normal <ins> and <del> html tags by something else and change or add some classes too (if is more convenient for you). At this point everithing is possible.

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

此代码经过测试并可以使用.

相关回答:Conditional custom output around products sale price and regular price

https://stackoverflow.com/questions/42158091/adding-custom-text-labels-to-the-prices-when-products-are-on-sale

转载注明原文:php – 在产品发售时将自定义文本标签添加到价格中