【问题标题】:Custom templates in Woocommerce 3Woocommerce 3 中的自定义模板
【发布时间】:2018-08-12 03:07:05
【问题描述】:

我正在尝试仅为一种 ID 为 5555 的产品制作单独的模板。从其页面中删除照片并更改块结构。覆盖此文件会影响所有产品页面。

wp-content\plugins\woocommerce\templates\content-single-product.php

逻辑解决方案是添加一个条件:如果产品单个产品页面的 ID 为 5555,则为其页面使用另一个模板。 我试过这个选项,但它不起作用。

/**
 * Custom single product template.
 */

add_filter( 'single_template', 'get_custom_post_type_template' );
function get_custom_post_type_template($single_template) {
    global $post;

    if ($post->post_type == 'product') {
        $single_template = dirname( __FILE__ ) . '/single-template.php';
    }
    return $single_template;
}

add_filter( 'template_include', 'portfolio_page_template', 99 );
function portfolio_page_template( $template ) {

    if ( is_page( 'slug' )  ) {
        $new_template = locate_template( array( 'single-template.php' ) );
        if ( '' != $new_template ) {
            return $new_template ;
        }
    }

    return $template;
}

或选项 2:删除此挂钩:

remove_action( 'woocommerce_before_single_product_summary', 'woocommerce_show_product_images', 20 );
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 );

仅来自一个特定的产品页面:

【问题讨论】:

    标签: php wordpress templates woocommerce product


    【解决方案1】:

    自 Woocommerce 3.3 以来的自定义模板

    由于 Woocommerce 3.3 Wordpress 钩子 template_include 似乎并不总是有效,您可以在 StackOverFlow 和其他地方的许多近期相关线程中看到。


    与 Woocommerce 模板相关的特定过滤器钩子:


    案例1.使用wc_get_template_part(你的案例)

    如果您查看single-product.php 模板,content-single-product.php 模板在这一行加载了以下内容:

    <?php wc_get_template_part( 'content', 'single-product' ); ?>
    

    所以我们将使用钩子wc_get_template_part

    假设您的自定义模板替换文件被命名为content-single-product-custom.php,并位于您的活动子主题(或活动主题)的woocommerce 文件夹中。要将此自定义模板用于特定产品 ID(5555),您将需要以下内容:

    add_filter( 'wc_get_template_part', 'custom_wc_template_part', 10, 3 );
    function custom_wc_template_part( $template, $slug, $name ) {
        // The specific product ID
        $product_id = 5555; 
    
        // The custom template file name
        $custom_template_name = 'content-single-product-custom.php'; 
    
        // For a specific product ID and content-single-product.php template
        if( get_the_ID() == $product_id && $slug == 'content' && $name == 'single-product' ){
            $template = trailingslashit( get_stylesheet_directory() ) . 'woocommerce/' . $custom_template_name;
        }
        return $template;
    }
    

    此代码位于您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。


    案例2.使用wc_get_template:

    如果模板是通过wc_get_template() 函数加载的,例如single-product/meta.php 并且您的自定义替换模板被命名为single-product/custom-meta.php (并且位于您的活动子主题的woocommerce 文件夹中)。要将此自定义模板用于特定产品 ID(5555),您将需要以下内容:

    add_filter( 'wc_get_template', 'custom_wc_template', 10, 5 );
    function custom_wc_template( $located, $template_name, $args, $template_path, $default_path ) {
        // The specific product ID
        $product_id = 5555;
    
        // The custom template file name and path
        $custom_template_name = 'single-product/custom-meta.php';
    
        if( is_product() && get_the_ID() == 37 && $template_name == 'single-product/meta.php'){
            $located = trailingslashit( get_stylesheet_directory() ) . 'woocommerce/' . $custom_template_name;
        }
        return $located;
    }
    

    此代码位于您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。


    关于覆盖 woocommerce 模板:

    【讨论】:

    • 非常感谢你,洛伊克!我检查了代码工作正常!?
    【解决方案2】:

    将此添加到您的主题的“functions.php”中,其中 5 是产品 ID,woocommerce/single-product-watermelon.php 是自定义产品模板。

    add_filter( 'template_include', 'watermelon_template_include' );
    
    function watermelon_template_include( $template ) {
        if ( is_singular('product') && get_the_ID() == 5 ) { //if category, change has_term( 'watermelon', 'product_cat') instead of get_the_ID()
            $template = get_stylesheet_directory() . '/woocommerce/single-product-watermelon.php';
        } 
        return $template;
    }
    

    【讨论】:

    • 遗憾的是没有变化... 5 - 产品 ID?
    • 是的 5 是产品 ID。
    • 首先,将一些字符添加到woocommerce/single-product-watermelon.php 并确认它是否正在为产品 ID 5 加载。请注意,WooCommerce 挂钩将与普通产品模板一样工作,最好直接应用您的自定义而不使用 WooCommerce钩子。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-21
    • 2020-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-25
    • 1970-01-01
    相关资源
    最近更新 更多