【问题标题】:Add custom content for a specific product on WooCommerce single product pages在 WooCommerce 单一产品页面上为特定产品添加自定义内容
【发布时间】:2018-04-30 00:56:50
【问题描述】:

在 Woocommerce 中,如何在单个产品页面上为特定产品添加自定义内容?

这是一个明确的屏幕截图:

【问题讨论】:

    标签: php wordpress woocommerce product hook-woocommerce


    【解决方案1】:

    只需编辑产品即可添加内容。并且该内容将显示在其详细信息页面上。

    【讨论】:

      【解决方案2】:

      由于您的屏幕截图不太清楚您想要此自定义内容的位置,因此您有 2 个选项:

      1)根据产品价格

      通过在 woocommerce_before_single_product_summary 动作挂钩中挂接此自定义函数,您可以添加一些自定义内容到特定的产品 ID (在函数中定义) 这样:

      add_action( 'woocommerce_single_product_summary', 'add_custom_content_for_specific_product', 15 );
      function add_custom_content_for_specific_product() {
          global $product;
      
          // Limit to a specific product ID only (Set your product ID below )
          if( $product->get_id() != 37 ) return;
      
          // The content start below (with translatables texts)
          ?>
              <div class="custom-content product-id-<?php echo $product->get_id(); ?>">
                  <h3><?php _e("My custom content title", "woocommerce"); ?></h3>
                  <p><?php _e("This is my custom content text, this is my custom content text, this is my custom content text…", "woocommerce"); ?></p>
              </div>
          <?php
          // End of content
      }
      

      2) 产品图片下方:

      通过在 woocommerce_before_single_product_summary 动作挂钩中挂钩此自定义函数,您可以添加一些自定义内容到特定产品 ID (在函数中定义) 这样:

      add_action( 'woocommerce_before_single_product_summary', 'add_custom_content_for_specific_product', 25 );
      function add_custom_content_for_specific_product() {
          global $product;
      
          // Limit to a specific product ID only (Set your product ID below )
          if( $product->get_id() != 37 ) return;
      
          // The content start below (with translatables texts)
          ?>
              <div class="custom-content product-id-<?php echo $product->get_id(); ?>">
                  <h3><?php _e("My custom content title", "woocommerce"); ?></h3>
                  <p><?php _e("This is my custom content text, this is my custom content text, this is my custom content text…", "woocommerce"); ?></p>
              </div>
          <?php
          // End of content
      }
      

      如果您想删除产品简短描述,您可以在 if 语句之后添加到函数中:

      remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20 );
      

      代码进入您的活动子主题(或主题)的 functions.php 文件或任何插件文件中。

      经过测试并且可以工作……

      【讨论】:

      • 只是一个小错字 - 它应该是 'functions.php' 文件而不是 'function.php'
      猜你喜欢
      • 2022-01-22
      • 1970-01-01
      • 2021-01-29
      • 1970-01-01
      • 1970-01-01
      • 2016-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多