【问题标题】:Move the variation description to the Woocommerce product variable description将变体描述移至 Woocommerce 产品变量描述
【发布时间】:2018-07-13 17:39:10
【问题描述】:

我正在尝试:在产品选项卡描述区域的单个产品页面上显示可变产品子项的变体描述。

在 WooCommerce 和可变产品中,父级具有存储在 wp_posts->post_content 中的描述,每个子级都有一个附加的 _variation_description 字段。在前端,父项的描述始终可见,并且子项的_variation_description 显示为选择了一个变体。它使用 js 进行更新,并显示在添加到购物车按钮之前以及变体的价格。

描述在description.php中调用代码

the_content();

输出父产品 post_content db 字段。 变体描述在variation.php中调用,代码

<script type="text/template" id="tmpl-variation-template">
    <div class="woocommerce-variation-description">{{{ data.variation.variation_description }}}</div>
</script>

据我了解,这适用于 wp.template,我还检查了Javascript Reference wp.template 上的信息 js 在 add-to-cart-variation.js 中,模板是用

构建的
template     = wp.template( 'variation-template' );

然后可以更新我不太了解的数据。我的理解是所有的js调用都是表单,

form.$singleVariation.html( $template_html );

然后我意识到为什么我不能只从variation.php 复制代码并将其粘贴到description.php,因为它会在表单标签之外。

另一方面,SKU 的数据不是从表单中调用的,而是通过 .product_meta 调用的,所以如果我放置一个 class="product_meta" 的 div,我可以调用 sku 并从任何地方用 js 更新它.例如我可以把这段代码放到description.php中

 <div class="product_meta">
       <?php if ( wc_product_sku_enabled() && ( $product->get_sku() || $product->is_type( 'variable' ) ) ) : ?>
          <?php if ( $sku = $product->get_sku() ) : ?>
             <span class="sku_wrapper"><?php esc_html_e( 'SKU:', 'woocommerce' ); ?> 
             <span class="sku" itemprop="sku"><?php echo $sku; ?></span></span>
          <?php endif; ?>
       <?php endif; ?>
 </div>

而且效果很好。

所以我在这里卡住了。我只想在 description.php 中动态显示变体描述,我想如果我了解 wp.template 在 add-to-cart-variation.js 中的工作原理,我就能弄清楚。但我不能。我不完全了解变体形式中的内容是如何更新的,以及 SKU 中的数据有什么区别。

对于我想要的东西,也许有一个完全简单易行的解决方案,也许我的方法太复杂了。我会很高兴任何想法或提示或只是指向正确的方向。

【问题讨论】:

  • 我更新了我的问题并遗漏了所有自定义字段,所以现在它更清楚了。我还试图阐明我的目标以及到目前为止我一直在尝试的内容。

标签: php jquery wordpress templates woocommerce


【解决方案1】:

这可以通过一些 jQuery 代码来完成,我们将在其中将变体描述复制到可变产品描述选项卡中。但首先您需要对某些模板进行一些非常小的更改。

这里是相关的官方文档:Template structure & Overriding templates via a theme

模板更改和必要的代码:

  1. single-product/tabs/description.php 模板文件中,您将替换以下行:

    <?php the_content(); ?>
    

    通过以下行(我们只是添加了一个带有选择器类的html &lt;div&gt; 标签)

    <div class="product-post-content"><?php the_content(); ?></div>
    
  2. single-product/add-to-cart/variation.php 模板文件中,您将替换以下行:

    <div class="woocommerce-variation-description">{{{ data.variation.variation_description }}}</div>
    

    通过以下行(我们只是隐藏变体描述)

    <div class="woocommerce-variation-description" style="display:none;">{{{ data.variation.variation_description }}}</div>
    
  3. 现在是 jQuery (已注释) 代码 (仅在可变产品的单个产品页面上)

    add_action( 'wp_footer', 'move_variation_description' );
    function move_variation_description(){
        global $product;
        // Only on single product pages for variable products
        if ( ! ( is_product() && $product->is_type('variable') ) ) return;
        // jQuery code
        ?>
        <script type="text/javascript">
            jQuery(function($){
                a = '.woocommerce-variation-description', b = a+' p', c = 'input.variation_id',
                d = '#tab-description .product-post-content', de = $(d).html();
    
                // On load, adding a mandatory very small delay
                setTimeout(function(){
                    // variation ID selected by default
                    if( '' != $(c).val() && $(a).text() != '' )
                        $(d).html($(a).html());
                }, 300);
    
                // On live event (attribute select fields change)
                $('table.variations select').on( 'blur', function(){
                    // variation ID is selected
                    if( '' != $(c).val() && $(a).text() != '' ){
                        $(d).html($(a).html()); // We copy the variation description
                    }
                    // No variation ID selected
                    else {
                        $(d).html($(a).html()); // We set back the variable product description
                    }
                    console.log($('input.variation_id').val()); // TEST: Selected variation ID … To be removed
                });
            });
        </script>
        <?php
    }
    

    此代码位于您的活动子主题(或活动主题)的 function.php 文件中。

经过测试并且有效。

其他相关话题:

【讨论】:

  • 非常感谢这个流畅的解决方案,非常棒,非常感谢!如果变体描述为空或没有任何选择,我做了一些补充以备后备。所以在 global $product 我添加了 $default_description = str_replace("\n", "", apply_filters( 'the_content', get_the_content() ));然后在 jQuery 中,我使用 传递变量,最后如果我用 $(d).html(default_description) 更改为 else 而不是 else ;
  • @Karim 所有这些都已经包含在我的实际代码中(我认为)......因为de = $(d).html(); 是默认描述,我在开始时将其复制到 js 变量中。因此,如果有人再次将任何选择字段更改为空值,则会出现默认变量产品描述。我唯一忘记的是使用重置链接重置选择器时的情况。我稍后会对此进行更新。
猜你喜欢
  • 2020-02-24
  • 1970-01-01
  • 1970-01-01
  • 2021-04-24
  • 2018-08-31
  • 1970-01-01
  • 2021-06-22
  • 2022-09-26
  • 1970-01-01
相关资源
最近更新 更多