code

WooCommerce - 카테고리에 따라 여러 개의 단일 제품 템플릿을 만드는 방법

starcafe 2023. 2. 27. 23:27
반응형

WooCommerce - 카테고리에 따라 여러 개의 단일 제품 템플릿을 만드는 방법

안녕하세요 저는 woocommerce에 처음 와보는 사람입니다.우리 가게에는 같은 단품 템플릿을 사용하는 4가지 카테고리가 있습니다.상품 페이지 레이아웃이 이미 사용하던 것과 크게 다른 다섯 번째 카테고리의 상품을 추가하고 싶습니다.

파일 구조는 다음과 같습니다.

  • 테마/우코메르스/단품/
  • 테마 / woocommerce / 단품 판매 /
  • 테마/우코메르스/싱글 제품/스폰/스폰서.php
  • 테마/우코메르스/콘텐츠-싱글 제품-프로덕트.php
  • 테마/우코메르스/단품.php

content-single-product-mock.php라는 파일을 만들었습니다.다음 코드를 사용하여 single-product.php에 입력

        <?php if (has_term( 'mock', 'product_cat' )) {
            woocommerce_get_template_part( 'content', 'single-product-mock' );
        } else{
         wc_get_template_part( 'content', 'single-product' ); 
        } ?>

mock 카테고리의 경우 content-single-product-mock.php로 리디렉션되지만 단일 제품 폴더의 템플릿 파일을 사용합니다.content-single-product-mock 템플릿 경로를 변경하려면 어떻게 해야 합니까?php를 사용하여 단일 제품 폴더 내의 맞춤 파일을 사용할 수 있습니까?

이 방법에는 여러 가지가 있을 수 있지만 템플릿이 포함되기 전에 필터링해야 한다는 생각을 바꾸고 있습니다.

WooCommerce는 완전히 생략할 수 있습니다.simple-product.php(템플릿을 덮어쓸 필요 없이) 직접 로 이동합니다.simple-product-mock.php거기서 모든 것을 창조할 수 있습니다.필터링을 하면 됩니다.

add_filter( 'template_include', 'so_25789472_template_include' );

function so_25789472_template_include( $template ) {
  if ( is_singular('product') && (has_term( 'mock', 'product_cat')) ) {
    $template = get_stylesheet_directory() . '/woocommerce/single-product-mock.php';
  } 
  return $template;
}

편집할 수 있습니다.single-product-mock.php부르다content-single-product-mock.php그 파일을 하드코드로 만들어요우씨의 훅과 기능을 계속 사용할 필요가 있는 것은 없습니다.요점은 커스터마이즈하기 쉽게 하는 것입니다.

또는 매우 까다롭게 하기 위해 다음과 같은 템플릿을 복제할 수 있습니다.single-product/title.phpsingle-product-mock폴더...예:single-product-mock/title.php그리고 나서 우리가 모의 카테고리의 단일 제품 템플릿에 있을 때마다single-product/something.php템플릿 및 리다이렉트single-product-mock/something.php 그것이 존재하며 계속 가리키고 있는 경우single-product/something.php그렇지 않으면.이 작업은 VIP를 통해서 실시합니다.woocommerce_locate_template필터링을 실시합니다.

add_filter( 'woocommerce_locate_template', 'so_25789472_locate_template', 10, 3 );

function so_25789472_locate_template( $template, $template_name, $template_path ){

    // on single posts with mock category and only for single-product/something.php templates
    if( is_product() && has_term( 'mock', 'product_cat' ) && strpos( $template_name, 'single-product/') !== false ){

        // replace single-product with single-product-mock in template name
        $mock_template_name = str_replace("single-product/", "single-product-mock/", $template_name );

        // look for templates in the single-product-mock/ folder
        $mock_template = locate_template(
            array(
                trailingslashit( $template_path ) . $mock_template_name,
                $mock_template_name
            )
        );

        // if found, replace template with that in the single-product-mock/ folder
        if ( $mock_template ) {
            $template = $mock_template;
        }
    }

    return $template;
}

필터로 업데이트wc_get_template대신.

/**
 * Change wc template part for product with a specific category
 *
 * @param  string $templates
 * @param  string $slug
 * @param  string $name
 * @return string
 */
function so_25789472_get_template_part( $template, $slug, $name ) {
  if ( $slug == 'content' && $name = 'single-product' && has_term( 'test', 'product_cat' ) ) {
    $template = locate_template( array( WC()->template_path() . 'content-single-product-test.php' ) );
  } 
  return $template;
}
add_filter( 'wc_get_template_part', 'so_25789472_get_template_part', 10, 3 );

언급URL : https://stackoverflow.com/questions/25789472/woocommerce-how-to-create-multiple-single-product-template-based-on-category

반응형