【问题标题】:Wordpress, Woocommerce Storefront Related ProductsWordpress、Woocommerce 店面相关产品
【发布时间】:2016-09-01 20:53:48
【问题描述】:

我想将产品页面上的相关产品移动到具有全宽列的较低位置,因此我在 functions.php 文件中定义了以下内容;

// Move related products
   remove_action('woocommerce_after_single_product_summary','woocommerce_output_related_products',20);
   add_action('storefront_before_footer','woocommerce_output_related_products',20);

但是,通过这样做,相关产品现在可能已超出某些重要循环,因为它们现在显示在类别存档页面上。

有谁知道(a)如何限制输出到产品页面(没有显示:无;在样式表中)-或-(b)另一个定义的位置,我可以将内容放在外面(下面) div 和大概是侧边栏,但保留相关产品与显示的产品页面的相关性?

我不想尝试为 and 重新设计 html 输出,我相信您会理解这样做会产生很大的影响。

我猜的另一个想法(虽然不理想或正确的方法)可能是使用 jQuery。

【问题讨论】:

    标签: php html wordpress function woocommerce


    【解决方案1】:

    已编辑:这应该将输出限制为产品页面。

    remove_action('woocommerce_after_single_product_summary','woocommerce_output_related_products',20);
    
    add_action('storefront_before_footer','woo_related_product_addition');
    
    function woo_related_product_addition() {
    
        global $post;
    
        if (function_exists( 'get_product' )) {
          add_action('storefront_before_footer','woocommerce_output_related_products',20);
        }
    }
    

    如果这对你不起作用......

    remove_action('woocommerce_after_single_product_summary','woocommerce_output_related_products',20);
    
    add_action('storefront_before_footer','woo_related_product_addition');
    
    function woo_related_product_addition() {
    
        global $post;
    
        if (function_exists( 'get_product' )) {
        $product = get_product( $post->ID );
    
        if ($product->is_type( 'single' || 'grouped' || 'external' || 'variable' )) {
          add_action('storefront_before_footer','woocommerce_output_related_products',20);
        }
    }
    }
    

    编辑 2:然后您可以通过执行类似操作从存档页面中删除产品。

    remove_action('woocommerce_after_single_product_summary','woo_related_product_removal');
    
    function woo_related_product_removal() {
    
        global $post;
    
        if (function_exists( 'get_product' )) {
        $product = get_product( $post->ID );
    
        if (!$product->is_type( 'single' || 'grouped' || 'external' || 'variable' )) {
            remove_action('woocommerce_after_single_product_summary','woocommerce_output_related_products',20);
        }
    }
    }
    

    【讨论】:

    • 简码在某些情况下更容易使用,因为您可以直接将其写入产品页面中所需位置并指定每页和每列的产品,我看不到您不清楚的地方?跨度>
    • 我同意@cale_b 这个答案,抱歉,这只是我已经做过的另一个例子。
    • 是的,我马上就看到了问题我想我有一个解决方案我想我只是快速阅读它会修改答案..
    • 尝试未测试但应该阻止相关产品出现在错误位置
    • 嗯,它看起来很有希望,我认为这会奏效,但不会。我猜你忘了注释掉第二行?注释掉它就没有输出。当然,如果我在存档页面上发表评论,但遗憾的是。
    猜你喜欢
    • 2017-03-12
    • 2022-08-21
    • 2017-06-24
    • 2019-09-08
    • 2014-05-27
    • 1970-01-01
    • 1970-01-01
    • 2017-04-07
    • 1970-01-01
    相关资源
    最近更新 更多