【问题标题】:Wordpress/Woocommerce - Edit template after product content but before related productsWordpress/Woocommerce - 在产品内容之后但在相关产品之前编辑模板
【发布时间】:2023-03-04 00:41:02
【问题描述】:

我想编辑 woocommerce 产品模板,以允许我在我的主要产品描述之后但相关产品之前添加一些代码(横幅)。

我正在编辑的代码(在 /wp-content/plugins/woocommerce/templates/single-product.php 内);

<?php
    /**
     * woocommerce_before_main_content hook.
     *
     * @hooked woocommerce_output_content_wrapper - 10 (outputs opening divs for the content)
     * @hooked woocommerce_breadcrumb - 20
     */
    do_action( 'woocommerce_before_main_content' );
?>

    <?php while ( have_posts() ) : the_post(); ?>

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

                          /*My code start*/
        echo "<div class=\"product-footer\">";
        echo "<h1 class=\"product-footer-text\">Need some additional help? Or have an enquiry?</h1>";
        echo "<h5 class=\"product-footer-text\"><i class=\"fa fa-phone\"></i>  Call one of our sales team on <strong>01782</strong></h5>";
        echo "<h5 class=\"product-footer-text\" style=\"margin-top: -40px;\"><i class=\"fa fa-envelope\"></i>  Contact us via our contact page <strong><a style=\"color: #ffffff;\" href=\"/../contact\" target=\"_blank\">HERE</a></strong></h5>";
        echo "<p class=\"product-footer-text\"><i><strong>Order note: </strong>All prices above are subject to delivery charges and VAT where applicable </i></span></p>
    </div>";
                            /*My code end*/

    ?><?php endwhile; // end of the loop. ?>

<?php
    /**
     * woocommerce_after_main_content hook.
     *
     * @hooked woocommerce_output_content_wrapper_end - 10 (outputs closing divs for the content)
     */
    do_action( 'woocommerce_after_main_content' );
?>

<?php
    /**
     * woocommerce_sidebar hook.
     *
     * @hooked woocommerce_get_sidebar - 10
     */
    do_action( 'woocommerce_sidebar' );
?>

给我这些结果; https://i.stack.imgur.com/sTgY6.png

我希望底部的横幅出现在产品说明下方。我一直在搞乱代码很长一段时间,但似乎无法让它工作。

【问题讨论】:

    标签: php html wordpress web woocommerce


    【解决方案1】:

    您可以使用 woocommerce_after_single_product_summary 钩子,优先级介于 1015 之间,而不是尝试覆盖模板在单个产品选项卡(产品描述)和相关产品之间获取您的自定义内容。请看下面这个钩子的架构:

    /**
     * woocommerce_after_single_product_summary hook.
     *
     * @hooked woocommerce_output_product_data_tabs - 10
     * @hooked woocommerce_upsell_display - 15
     * @hooked woocommerce_output_related_products - 20
     */
    

    因此,您的代码将改为位于活动子主题(或主题)的 function.php 文件中:

    add_action( 'woocommerce_after_single_product_summary', 'custom_single_product_banner', 12 );
    function custom_single_product_banner() {
    
        $output = '<div class="product-footer">
            <h1 class="product-footer-text">Need some additional help? Or have an enquiry?</h1>
            <h5 class="product-footer-text"><i class="fa fa-phone"></i>  Call one of our sales team on <strong>01782</strong></h5>
            <h5 class="product-footer-text" style="margin-top: -40px;"><i class="fa fa-envelope"></i>  Contact us via our contact page <strong><a style="color: #ffffff;" href="/../contact" target="_blank">HERE</a></strong></h5>
            <p class="product-footer-text"><i><strong>Order note: </strong>All prices above are subject to delivery charges and VAT where applicable </i></span></p>
        </div>';
        echo $output;
    }
    

    此代码已经过测试并且可以工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-12
      • 2019-10-05
      • 1970-01-01
      • 1970-01-01
      • 2016-09-01
      • 1970-01-01
      • 2014-08-07
      • 1970-01-01
      相关资源
      最近更新 更多