【问题标题】:Products dropdown from same category inside Woocommerce product short descriptionWoocommerce产品简短描述中同一类别的产品下拉列表
【发布时间】:2019-01-20 06:08:38
【问题描述】:

在 Woocommerce 中,我想在产品简短描述中添加一个下拉列表,以显示具有相同类别的所有产品。如果能进入所选产品的产品页面就更好了。

我没有看到任何线程可以满足我正在尝试做的事情。

任何帮助将不胜感激。

【问题讨论】:

    标签: wordpress woocommerce dropdown product custom-taxonomy


    【解决方案1】:

    2021 年更新 - 添加了 product_id 作为参数,允许将短代码用于定义的产品 ID(例如在页面上)。

    以下将创建一个自定义短代码,您可以在产品简短描述(甚至产品描述)中使用它,并将显示与当前产品相同的产品类别的下拉列表。

    代码:

    add_shortcode( 'products_dropdown', 'wc_products_from_cat_dropdown' );
    function wc_products_from_cat_dropdown( $atts ) {
            // Shortcode Attributes
            $atts = shortcode_atts( array(
                'product_id' => '', 
            ), $atts, 'products_dropdown' );
            
        $product_id = is_product() ? get_the_id() : $atts['product_id'];
        
        if ( empty($product_id) )
            return;
    
        ob_start();
    
        $query = new WP_Query( array(
            'post_type'      => 'product',
            'post_status'    => 'publish',
            'posts_per_page' => '-1',
            'post__not_in'     => array( $product_id ),
            'tax_query' => array( array(
                    'taxonomy' => 'product_cat',
                    'field'    => 'term_id',
                    'terms'    => wp_get_post_terms( $product_id, 'product_cat', array( 'fields' => 'ids' ) ) ,
            ) ),
        ) );
    
        if ( $query->have_posts() ) :
    
        echo '<div class="products-dropdown"><select name="products-select" id="products-select">
        <option value="">'.__('Choose a related product').'</option>';
    
        while ( $query->have_posts() ) : $query->the_post();
    
        echo '<option value="'.get_permalink().'">'.get_the_title().'</option>';
    
        endwhile;
    
        echo '</select> <button type="button" style="padding:2px 10px; margin-left:10px;">'._("Go").'</button></div>';
    
        wp_reset_postdata();
    
        endif;
    
        ?>
        <script type='text/javascript'>
            jQuery(function($){
                var a = '.products-dropdown', b = a+' button', c = a+' select', s = '';
                $(c).change(function(){
                    s = $(this).val();
                    console.log(s); // just for testing (to be removed)
                });
                 $(b).click(function(){
                    if( s != '' ) location.href = s;
                });
    
            });
        </script>
        <?php
    
        return ob_get_clean();
    }
    

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


    用法

    1)。对于单个产品页面:只需在产品简短描述(或描述)中粘贴以下短代码:

    [products_dropdown]
    

    2)。对于单个产品页面,在 php 代码中:

    echo do_shortcode("[products_dropdown]");
    

    3)。在文本编辑器中的任何帖子或页面上,定义 product_id 参数(定义的产品 ID 下方是 37

    [products_dropdown product_id="37"]
    

    【讨论】:

    • 你能看看我的其他问题吗? stackoverflow.com/questions/51829990/…
    • @glennkline 你应该澄清一下,可能会添加更多细节和屏幕截图......
    • 嘿@LoicTheAztec 感谢您的解决方案。有没有办法可以按字母顺序对下拉列表进行排序?谢谢。
    • @HamzaAhmad 将此行添加到 WP_Query 参数数组:orderby =&gt; 'name',
    【解决方案2】:

    将此添加到您的主题的“functions.php”中,它将显示您当前产品类别的所有产品。

    function add_products_short_description() {
        $product_cats = wp_get_post_terms( get_the_ID(), 'product_cat' );
        if ( $product_cats ) {
            $single_cat = array_shift( $product_cats );
    
            $product_args = array( 'post_type' => 'product', 'posts_per_page' => '-1', 'product_cat' => $single_cat->name );
            $products = new WP_Query( $product_args );
            if ( $products->have_posts() ) :  echo '<ul>';
                while ( $products->have_posts() ) : $products->the_post(); global $product; 
                    echo '<li><a href="'.get_permalink( $products->ID ).'">'.get_the_title($products->ID).'</a></li>';
                endwhile;
                echo '</ul>';
            endif;
        }
    }
    add_action( 'woocommerce_single_product_summary', 'add_products_short_description', 15 );
    

    【讨论】:

    • 它不起作用.. 我将代码添加到底部的主题函数 Functions.php 中。这是我的链接。 planroom.shetlerprinting.com/product/…
    • 您是否从 WooCommerce 模板中删除了“简短描述挂钩”?
    • 我不这么认为。我可以添加描述,然后它们就会出现。帮忙?
    • 你能添加简短的描述并检查它是否正在显示吗?
    猜你喜欢
    • 2022-09-23
    • 2020-10-21
    • 1970-01-01
    • 2021-06-22
    • 1970-01-01
    • 2016-04-14
    • 2020-06-08
    • 1970-01-01
    相关资源
    最近更新 更多