首页 文章

WooCommerce查询产品

提问于
浏览
2

我正在创建一个页面,当用户在购物车中添加产品时显示该页面 . 用户单击“添加到购物车”按钮后,将显示此模板 .

目标是根据添加的项目显示其他客户已购买的其他产品 .

它与亚马逊功能相同 .

我知道如何在WooCommerce中进行查询,但不知道如何获得包含所选产品的其他订单中包含的其他产品 .

有人可以帮帮我吗?

2 回答

  • 1

    最后,我创建了自己的模板,以显示与添加到购物车中的项目相关的其他用户购买的产品:

    <?php 
    /*
     Template Name: template_products_orders
     */
    
    get_header('shop');
    
    global $woocommerce, $wpdb;
    $items = $woocommerce->cart->get_cart();
    $ids = array();
    foreach($items as $item => $values) { 
        $product = $values['data']->post; 
      $ids       = $product->ID; 
    } 
    
    $product_selected_id    = $ids;
    
    // Query to get orders with the item selected....
    $query           = array();
    $query['fields'] = "SELECT DISTINCT ID FROM {$wpdb->posts} p";
    $query['join']   = " INNER JOIN wp_woocommerce_order_items items ON items.order_id = p.ID";
    $query['join']  .= " INNER JOIN wp_woocommerce_order_itemmeta itemmeta ON (itemmeta.order_item_id = items.order_item_id)";
    
    $query['where']  = " WHERE p.post_type='shop_order' AND (p.post_status='wc-completed' OR p.post_status='wc-pending')
                                                         AND itemmeta.meta_key='_product_id' AND itemmeta.meta_value='$product_selected_id'";
    
    $orders = $wpdb->get_col( implode( ' ', $query ) );
    
    // get items in other orders....
    $query           = array();
    $query['fields'] = "SELECT itemmeta.meta_value FROM {$wpdb->posts} p";
    $query['join']   = " INNER JOIN wp_woocommerce_order_items items ON items.order_id = p.ID";
    $query['join']  .= " INNER JOIN wp_woocommerce_order_itemmeta itemmeta ON (itemmeta.order_item_id = items.order_item_id)";
    $query['where']  = " WHERE p.ID in (".implode(', ', $orders).")
                                                         AND (itemmeta.meta_key='_product_id' AND itemmeta.meta_value<>'$product_selected_id')";
    
    $products = $wpdb->get_col( implode( ' ', $query ) );
    
    if ( count( $products ) > 0 ) {
    

    ?>

    <div class="home-featured-products">
            <div class="wrap">
                    <section id="text-12">
                            <h3 class="widgettitle">Those who bought this product also purchased</h4>
                            <div class="woocommerce columns-4">
                                    <div class="yit-wcan-container">
    
                                            <?php
    
                                                $args = array(      'post_type'             => 'product',
                                                                            'post__in'              => $productos,
                                                                            'posts_per_page'    => 4
                                                                        );
                                                $wp_query = new WP_Query($args);
    
                                            ?>
    
                                        <ul class="products">
    
                                        <?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
    
                                            <?php woocommerce_get_template_part('content', 'product'); ?>
    
                                        <?php endwhile; 
                                        wp_reset_query(); 
                                        ?>
    
                                            </ul>
    
                                    </div>
                            </div>
                    </div>
            </div>
    </div>
    
    <?php } ?>
    
  • 0

    目前没有提供此功能的插件 . 但以下链接可能会帮助您实现您的目的 .

    https://github.com/bhaveshbhide/woocommerce-cross-selling/

    这是我找到的唯一有用的链接,它可以回答您的问题 .

    如果这对您有用,请告诉我 .

相关问题