Created
September 2, 2026 04:50
-
-
Save DmitryBychenko/6c1c2889c90abe49e9a4d7b2da160833 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Шорткод для вывода цены товара WooCommerce по ID с кастомным форматированием | |
| * [tb_product_price id="123" currency="1" sym="руб" old="1" ths="space"] | |
| */ | |
| add_shortcode( 'tb_product_price', 'tb_woo_product_price_shortcode' ); | |
| function tb_woo_product_price_shortcode( $atts ) { | |
| $atts = shortcode_atts( array( | |
| 'id' => null, | |
| 'currency' => '1', | |
| 'sym' => '', // Свой символ или текст вместо валюты | |
| 'old' => '1', // 1 - выводить старую цену, 0 - только актуальную | |
| 'ths' => null, // Разделитель тысяч: '.', ',', 'space' или любой другой символ | |
| ), $atts, 'tb_product_price' ); | |
| if ( empty( $atts['id'] ) ) return ''; | |
| $product = wc_get_product( $atts['id'] ); | |
| if ( ! $product || ! is_a( $product, 'WC_Product' ) ) { | |
| return ''; | |
| } | |
| // Определяем разделитель тысяч | |
| $default_ths = wc_get_price_thousand_separator(); | |
| if ( is_null( $atts['ths'] ) ) { | |
| $ths_separator = $default_ths; | |
| } elseif ( $atts['ths'] === 'space' ) { | |
| $ths_separator = ' '; | |
| } else { | |
| $ths_separator = $atts['ths']; | |
| } | |
| // 1. Обработка режима "без валюты" (currency="0") | |
| if ( $atts['currency'] == '0' ) { | |
| $raw_price = $product->get_price(); | |
| // Если задан кастомный разделитель тысяч, форматируем число | |
| if ( ! is_null( $atts['ths'] ) && is_numeric( $raw_price ) ) { | |
| $decimals = wc_get_price_decimals(); | |
| $dec_point = wc_get_price_decimal_separator(); | |
| $price_display = number_format( (float)$raw_price, $decimals, $dec_point, $ths_separator ); | |
| } else { | |
| $price_display = $raw_price; | |
| } | |
| } | |
| else { | |
| // 2. Получаем HTML цены в зависимости от параметра old | |
| if ( $atts['old'] == '0' ) { | |
| if ( $product->is_type( 'variable' ) ) { | |
| $price_display = wc_price( $product->get_variation_price( 'min' ) ) . ' – ' . wc_price( $product->get_variation_price( 'max' ) ); | |
| } else { | |
| $price_display = wc_price( $product->get_price() ); | |
| } | |
| } else { | |
| $price_display = $product->get_price_html(); | |
| } | |
| // 3. Замена разделителя тысяч, если он отличается от стандартного | |
| if ( ! is_null( $atts['ths'] ) && $ths_separator !== $default_ths ) { | |
| // Заменяем разделитель внутри чисел в сформированном HTML | |
| $price_display = str_replace( $default_ths, $ths_separator, $price_display ); | |
| } | |
| // 4. Замена символа валюты на кастомный sym | |
| if ( ! empty( $atts['sym'] ) ) { | |
| $current_currency_symbol = get_woocommerce_currency_symbol(); | |
| $price_display = str_replace( $current_currency_symbol, $atts['sym'], $price_display ); | |
| } | |
| } | |
| return '<span class="tb-product-price">' . $price_display . '</span>'; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment