基于WooCommerce 3.3+中产品类别的自定义购物车项目货币符号



我想显示基于产品类别的自定义货币符号。我接受了以下关于这个问题的建议片段 根据 WooCommerce 中的产品类别更改货币符号。

但是,我实际上需要自定义货币也显示在所有Woocommerce页面(包括购物车和结帐页面(中。

这是我的代码:

add_filter('woocommerce_currency_symbol', 'change_existing_currency_symbol', 10, 2);
function change_existing_currency_symbol( $currency_symbol, $currency ) {
global $post, $product, $woocommerce;
if ( has_term( 'cupones', 'product_cat' ) ) :
switch( $currency ) {
case 'EUR': $currency_symbol = 'ptos'; break;
}
return $currency_symbol;
elseif ( has_term( 'cupones', 'product_cat' ) && is_cart() ): 
switch( $currency ) {
case 'EUR': $currency_symbol = 'ptos'; break;
}
return $currency_symbol;
elseif ( has_term( 'cupones', 'product_cat' ) && is_checkout() ): 
switch( $currency ) {
case 'EUR': $currency_symbol = 'ptos'; break;
}
return $currency_symbol;  
endif;      
}

更新:对于购物车和结帐页面,有两种方法:按项目或全局。

1( 按项目( 最复杂(:

// HERE below your settings
function custom_currency_symbol_settings(){
return array(
'curr_symbol' => 'ptos', // <=== HERE define your currency symbol replacement
'currency'    => 'EUR', // <=== HERE define the targeted currency code
'category'    => array('cupones'), // <=== HERE define your product category(ies)
'taxonomy'    => 'product_cat',
);
}
// On product pages - Custom currency symbol
add_filter( 'woocommerce_currency_symbol', 'change_existing_currency_symbol', 10, 2 );
function change_existing_currency_symbol( $currency_symbol, $currency ) {
global $post, $product;
// Loading your settings
$data = custom_currency_symbol_settings();
// Others Woocommerce product pages
if ( has_term( $data['category'], $data['taxonomy'] ) && $currency == $data['currency'] ) {
return $data['curr_symbol'];
}
return $currency_symbol;
}
// On cart item product price (Cart page) - Custom currency symbol
add_filter( 'woocommerce_cart_product_price', 'change_cart_item_price_currency_symbol', 10, 2 );
function change_cart_item_price_currency_symbol( $product_price_html, $product ) {
// Loading your settings
$data = custom_currency_symbol_settings();
// Get the correct product ID
$product_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();
// Only for the defined product category and the defined currency
if ( ! has_term( $data['category'], $data['taxonomy'], $product_id  ) || get_woocommerce_currency() != $data['currency'] ) {
return $product_price_html; // Exit
}
if ( WC()->cart->display_prices_including_tax() ) {
$price = wc_get_price_including_tax( $product );
} else {
$price = wc_get_price_excluding_tax( $product );
}
return wc_price_custom( $price, $data['curr_symbol'] );
}
//  On cart item line subtotal (Cart and Checkout pages) - Custom currency symbol
add_filter( 'woocommerce_cart_product_subtotal', 'change_cart_item_subtotal_currency_symbol', 10, 8 );
function change_cart_item_subtotal_currency_symbol( $product_subtotal, $product, $quantity, $cart ) {
// Loading your settings
$data = custom_currency_symbol_settings();
// Get the correct product ID
$product_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();
// Only for the defined product category and the defined currency
if ( ! has_term( $data['category'], $data['taxonomy'], $product_id  ) || get_woocommerce_currency() != $data['currency'] ) {
return $product_price_html; // Exit
}
if ( $product->is_taxable() ) {
if ( $cart->display_prices_including_tax() ) {
$row_price        = wc_get_price_including_tax( $product, array( 'qty' => $quantity ) );
$product_subtotal = wc_price_custom( $row_price, $data['curr_symbol'] );
if ( ! wc_prices_include_tax() && $cart->get_subtotal_tax() > 0 ) {
$product_subtotal .= ' <small class="tax_label">' . WC()->countries->inc_tax_or_vat() . '</small>';
}
} else {
$row_price        = wc_get_price_excluding_tax( $product, array( 'qty' => $quantity ) );
$product_subtotal = wc_price_custom( $row_price, $data['curr_symbol'] );
if ( wc_prices_include_tax() && $cart->get_subtotal_tax() > 0 ) {
$product_subtotal .= ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
}
}
} else {
$row_price        = $product->get_price() * $quantity;
$product_subtotal = wc_price_custom( $row_price, $data['curr_symbol'] );
}
return $product_subtotal;
}
// Custom formatting price function replacement
function wc_price_custom( $price, $curr_symbol, $args = array() ){
extract( apply_filters( 'wc_price_args', wp_parse_args( $args, array(
'ex_tax_label'       => false,
'currency'           => '',
'decimal_separator'  => wc_get_price_decimal_separator(),
'thousand_separator' => wc_get_price_thousand_separator(),
'decimals'           => wc_get_price_decimals(),
'price_format'       => get_woocommerce_price_format(),
) ) ) );
$unformatted_price = $price;
$negative          = $price < 0;
$price             = apply_filters( 'raw_woocommerce_price', floatval( $negative ? $price * -1 : $price ) );
$price             = apply_filters( 'formatted_woocommerce_price', number_format( $price, $decimals, $decimal_separator, $thousand_separator ), $price, $decimals, $decimal_separator, $thousand_separator );
if ( apply_filters( 'woocommerce_price_trim_zeros', false ) && $decimals > 0 ) {
$price = wc_trim_zeros( $price );
}
$formatted_price = ( $negative ? '-' : '' ) .
sprintf( $price_format, '<span class="woocommerce-Price-currencySymbol">' . $curr_symbol . '</span>', $price );
$return          = '<span class="woocommerce-Price-amount amount">' . $formatted_price . '</span>';
if ( $ex_tax_label && wc_tax_enabled() ) {
$return .= ' <small class="woocommerce-Price-taxLabel tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
}
return apply_filters( 'wc_price', $return, $price, $args, $unformatted_price );
}

2(全球(更容易(

以下基于您的代码设置的代码将在购物车和结帐页面上全局更改货币符号,当在任何购物车商品中找到"cupones"产品类别时:

add_filter( 'woocommerce_currency_symbol', 'change_existing_currency_symbol', 10, 2 );
function change_existing_currency_symbol( $currency_symbol, $currency ) {
global $post, $product;
$custom_sym = 'ptos'; // <=== HERE define your currency symbol replacement
$custom_cur = 'EUR'; // <=== HERE define the targeted currency code
$category   = array('cupones'); // <=== HERE define your product category(ies)
$taxonomy   = 'product_cat';
// Cart and checkout
if( ( is_cart() || is_checkout() ) && $currency == $custom_cur ){
foreach( WC()->cart->get_cart() as $cart_item ){
if ( has_term( $category, $taxonomy, $cart_item['product_id'] ) ){
return $custom_sym; // Found! ==> we return the custom currency symbol
}
}
}
// Others Woocommerce product pages
if ( has_term( $category, $taxonomy ) && $currency == $custom_cur ) {
return $custom_sym;
}
return $currency_symbol;
}

代码进入函数.php活动子主题(或活动主题(的文件。经过测试并工作。

最新更新