Как назначить один шаблон продукта продукту по его категории (тема WordPress/WooCommerce/Блок)Php

Кемеровские программисты php общаются здесь
Ответить
Anonymous
 Как назначить один шаблон продукта продукту по его категории (тема WordPress/WooCommerce/Блок)

Сообщение Anonymous »

Я попытался назначить продукту один шаблон по его категории. Я искал похожие вопросы, но нашел ответы в классической теме. Ниже приведены два примера нескольких кодов, которые мне предоставили Gemini, и ни один из них не сработал -> каждый продукт вернулся к шаблону по умолчанию вместо назначенных шаблонов.
Код 1
function custom_product_template_by_category_fse( $block_template, $id, $template_type ) {
if ( $id !== 'single-product' || $template_type !== 'single' ) {
return $block_template;
}
$category_template_map = array(
// Product Category Slug => FSE Template Slug
'electronics' => 'single-product-tech',
'apparel' => 'single-product-fashion',
'books' => 'single-product-media',
);
if ( is_singular( 'product' ) ) {
$product_id = get_the_ID();
$terms = get_the_terms( $product_id, 'product_cat' );
if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
foreach ( $terms as $term ) {
$category_slug = $term->slug;
if ( array_key_exists( $category_slug, $category_template_map ) ) {
$template_slug_to_use = $category_template_map[$category_slug];
$theme_slug = get_stylesheet();
$new_template = get_block_template( "{$theme_slug}//{$template_slug_to_use}", 'single' );
if ( $new_template ) {
return $new_template;
}
}
}
}
}
return $block_template;
}
add_filter( 'get_block_template', 'custom_product_template_by_category_fse', 10, 3 );

Код 2
function custom_block_template_include_by_category_final_25( $template ) {
if ( is_singular( 'product' ) ) {
$product_id = get_the_ID();
$terms = get_the_terms( $product_id, 'product_cat' );
$theme_dir = get_stylesheet_directory();
$template_origin_slug = 'twentytwentyfour';
$category_template_map = array(
'electronics' => 'single-product-tech',
'apparel' => 'single-product-fashion',
'books' => 'single-product-media',
);
if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
foreach ( $terms as $term ) {
$category_slug = $term->slug;
if ( array_key_exists( $category_slug, $category_template_map ) ) {
$template_slug_to_use = $category_template_map[ $category_slug ];
$wrapper_template_path = $theme_dir . '/templates/single-product.html';
if ( file_exists( $wrapper_template_path ) ) {
add_filter( 'block_template_meta', function( $meta, $slug ) use ( $template_slug_to_use, $template_origin_slug ) {
if ( $slug === 'single-product' ) {
$meta['slug'] = $template_slug_to_use;
$meta['theme'] = $template_origin_slug;
}
return $meta;
}, 99, 2 );
return $wrapper_template_path;
}
}
}
}
}
return $template;
}
add_filter( 'template_include', 'custom_block_template_include_by_category_final_25', 100 );


Подробнее здесь: https://stackoverflow.com/questions/797 ... dpress-woo
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Php»