Я пытаюсь выяснить, как показать все варианты цен в виде текста на одной странице продукта.
например, если у продукта 5 разных цен, 1 – 500, 2 – 900, 3 – 2100, 4–2500, 5–3000
Я хочу, чтобы это отображалось в тексте на всех страницах продукта.
вот так:
ВАРИАЦИИ
1 – 500
2 – 900.
сейчас я делаю это вручную, записываю их все, чтобы у людей не было необходимости прокрутить раскрывающееся меню вниз, чтобы узнать цены, но я хочу, чтобы это делалось автоматически при изменении цен, чтобы мне не приходилось каждый раз менять этот текст. я пробовал разные крючки, но не нашел подходящего.
я пробовал этот, но не могу крючок сработать и показать его на странице продукта
add_filter( 'woocommerce_get_price_html', 'custom_price_format', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'custom_price_format', 10, 2 );
function custom_price_format( $price, $product ) {
// 1. Variable products
if( $product->is_type('variable') ){
// Searching for the default variation
$default_attributes = $product->get_default_attributes();
// Loop through available variations
foreach($product->get_available_variations() as $variation){
$found = true; // Initializing
// Loop through variation attributes
foreach( $variation['attributes'] as $key => $value ){
$taxonomy = str_replace( 'attribute_', '', $key );
// Searching for a matching variation as default
if( isset($default_attributes[$taxonomy]) && $default_attributes[$taxonomy] != $value ){
$found = false;
break;
}
}
// When it's found we set it and we stop the main loop
if( $found ) {
$default_variaton = $variation;
break;
} // If not we continue
else {
continue;
}
}
// Get the default variation prices or if not set the variable product min prices
$regular_price = isset($default_variaton) ? $default_variaton['display_price']: $product->get_variation_regular_price( 'min', true );
$sale_price = isset($default_variaton) ? $default_variaton['display_regular_price']: $product->get_variation_sale_price( 'min', true );
}
// 2. Other products types
else {
$regular_price = $product->get_regular_price();
$sale_price = $product->get_sale_price();
}
// Formatting the price
if ( $regular_price !== $sale_price && $product->is_on_sale()) {
// Percentage calculation and text
$percentage = round( ( $regular_price - $sale_price ) / $regular_price * 100 ).'%';
$percentage_txt = __(' Save', 'woocommerce' ).' '.$percentage;
$price = '' . wc_price($regular_price) . ' ' . wc_price($sale_price) . $percentage_txt . '';
}
return $price;
}
Подробнее здесь: https://stackoverflow.com/questions/704 ... price-list
Прайс-лист на варианты продуктов Woocommerce ⇐ Php
Кемеровские программисты php общаются здесь
-
Anonymous
1727996177
Anonymous
Я пытаюсь выяснить, как показать все варианты цен в виде текста на одной странице продукта.
например, если у продукта 5 разных цен, 1 – 500, 2 – 900, 3 – 2100, 4–2500, 5–3000
Я хочу, чтобы это отображалось в тексте на всех страницах продукта.
вот так:
ВАРИАЦИИ
1 – 500
2 – 900.
сейчас я делаю это вручную, записываю их все, чтобы у людей не было необходимости прокрутить раскрывающееся меню вниз, чтобы узнать цены, но я хочу, чтобы это делалось автоматически при изменении цен, чтобы мне не приходилось каждый раз менять этот текст. я пробовал разные крючки, но не нашел подходящего.
я пробовал этот, но не могу крючок сработать и показать его на странице продукта
add_filter( 'woocommerce_get_price_html', 'custom_price_format', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'custom_price_format', 10, 2 );
function custom_price_format( $price, $product ) {
// 1. Variable products
if( $product->is_type('variable') ){
// Searching for the default variation
$default_attributes = $product->get_default_attributes();
// Loop through available variations
foreach($product->get_available_variations() as $variation){
$found = true; // Initializing
// Loop through variation attributes
foreach( $variation['attributes'] as $key => $value ){
$taxonomy = str_replace( 'attribute_', '', $key );
// Searching for a matching variation as default
if( isset($default_attributes[$taxonomy]) && $default_attributes[$taxonomy] != $value ){
$found = false;
break;
}
}
// When it's found we set it and we stop the main loop
if( $found ) {
$default_variaton = $variation;
break;
} // If not we continue
else {
continue;
}
}
// Get the default variation prices or if not set the variable product min prices
$regular_price = isset($default_variaton) ? $default_variaton['display_price']: $product->get_variation_regular_price( 'min', true );
$sale_price = isset($default_variaton) ? $default_variaton['display_regular_price']: $product->get_variation_sale_price( 'min', true );
}
// 2. Other products types
else {
$regular_price = $product->get_regular_price();
$sale_price = $product->get_sale_price();
}
// Formatting the price
if ( $regular_price !== $sale_price && $product->is_on_sale()) {
// Percentage calculation and text
$percentage = round( ( $regular_price - $sale_price ) / $regular_price * 100 ).'%';
$percentage_txt = __(' Save', 'woocommerce' ).' '.$percentage;
$price = '' . wc_price($regular_price) . ' ' . wc_price($sale_price) . $percentage_txt . '';
}
return $price;
}
Подробнее здесь: [url]https://stackoverflow.com/questions/70420758/woocommerce-product-variation-price-list[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия