woocommerce 的价格定制

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

在 WooCommerce 中,可以在产品循环和单个产品页面中仅显示含税或不含税的价格。此外,如果一种产品正在促销,Woocommerce 会显示常规价格,例如划掉的价格和促销价。这是默认的。

我发现许多工作片段可以根据我的需要定制价格,但也有一些覆盖了我喜欢保留的内容:例如,如果定义了销售价格,我会松开划掉的常规价格。 我正在寻找一个清晰的代码,让我能够:

  1. 对于简单和可变的产品,显示常规价格和促销价格
  2. 显示含税价格和不含税价格以及附加自定义文本(含税;不含税)

这也适用于产品循环和单个产品页面。

感谢您的任何建议。

php wordpress woocommerce product price
2个回答
1
投票

此代码应添加到主题的functions.php文件中

add_filter('woocommerce_get_price_html', 'display_both_prices', 10, 2);
function display_both_prices($price, $product) {
    if ($product->is_type('variable')) {
        $min_price = $product->get_variation_price('min', true);
        $max_price = $product->get_variation_price('max', true);

        if ($min_price !== $max_price) {
            $price = wc_format_price($min_price) . ' - ' . wc_format_price($max_price);
        } else {
            $price = wc_price($min_price);
        }
    } else {
        if ($product->is_on_sale()) {
            $regular_price = wc_price($product->get_regular_price());
            $sale_price = wc_price($product->get_sale_price());
            $price = '<del>' . $regular_price . '</del> <ins>' . $sale_price . '</ins>';
        } else {
            $price = wc_price($product->get_price());
        }
    }
    return $price;
}

并显示含税和不含税的价格以及自定义文本:

add_filter('woocommerce_get_price_html', 'display_price_with_tax', 10, 2);
function display_price_with_tax($price, $product) {
    $price_incl_tax = wc_price($product->get_price_including_tax());
    $price_excl_tax = wc_price($product->get_price_excluding_tax());
    $custom_text = ' (incl. tax: ' . $price_incl_tax . ', excl. tax: ' . $price_excl_tax . ')';
    return $price . $custom_text;
}

0
投票

我找到了一个完美满足我需求的解决方案。 这段代码满足了我在原始请求中的要求,就像 Druvi 的答案(感谢 Druvi)。

附加功能:

  • 在售价前添加自定义文本。

  • 如果产品有“noleggio”(租赁)标签,它会显示自定义文本,如果它有“vendita”(购买)标签,它会显示其他文本。

  • 如果正在销售的产品有销售日期/截止日期,则会在产品循环和单品页面中显示到期日期。

希望可以帮助别人。

添加常规价格、促销价格和特定标签的文本:

function ckde_sale_price_html( $price, $product ) {
$excl_tax = sprintf( __("escl. IVA") );
if ( $product->is_on_sale() ) {

if ( has_term( 'noleggio', 'product_tag' )) {
$has_sale_text = array(
'<ins>' => '<br>Prezzo in offerta: &nbsp;</ins>'
);

$return_string = str_replace(array_keys( $has_sale_text ), array_values( $has_sale_text ), $price . '<br><span class="woocommerce-price-suffix exc-vat-price">' . $day_excl_tax .'</span>');

} elseif ( has_term( 'vendita', 'product_tag' ) ) {
$has_sale_text = array(
'<ins>' => '<br>Prezzo in offerta: &nbsp;</ins>'
);

$return_string = str_replace(array_keys( $has_sale_text ), array_values( $has_sale_text ), $price . '<br><span class="woocommerce-price-suffix exc-vat-price">' . $excl_tax .'</span>');

} else {

$return_string = $price . '<br><span class="woocommerce-price-suffix exc-vat-price">' . $day_excl_tax .'</span>';
}
} else {
if ( has_term( 'noleggio', 'product_tag' )) {
$return_string = $price . '<br><span class="woocommerce-price-suffix exc-vat-price">' . $day_excl_tax .'</span>';

} elseif ( has_term( 'vendita', 'product_tag' ) ) {

$return_string = $price . '<br><span class="woocommerce-price-suffix exc-vat-price">' . $excl_tax .'</span>';
}
}

return $return_string;

}

add_filter( 'woocommerce_get_price_html', 'ckde_sale_price_html', 100, 2 );

在 Woocommerce 循环产品和单一产品页面中添加包含销售日期和售价的自定义文本:

add_filter( 'woocommerce_sale_flash', 'ckde_sale_end_date', 9999, 3 );
     
function ckde_sale_end_date( $html, $post, $product ) {
if ( $product->get_date_on_sale_to() ) return $html . ' <span class="onsale-date">(In offerta fino a: ' . gmdate( 'd M Y', $product->get_date_on_sale_to()->getTimestamp() ) . ')</span>';

return $html;

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