wordpress - WooCommerce Query for products -
i'm creating page shown when user add product in cart. once user click on "add cart" button, template shown.
the goal show other products other customers have bought based on item added.
it's same functionality amazon.
i know how make queries in woocommerce not know how other products contained in other orders contain product selected.
anyone can me?
finally created own template show products bought bu other users related item added cart:
<?php /* template name: template_products_orders */ get_header('shop'); global $woocommerce, $wpdb; $items = $woocommerce->cart->get_cart(); $ids = array(); foreach($items $item => $values) { $product = $values['data']->post; $ids = $product->id; } $product_selected_id = $ids; // query orders item selected.... $query = array(); $query['fields'] = "select distinct id {$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'] = " p.post_type='shop_order' , (p.post_status='wc-completed' or p.post_status='wc-pending') , itemmeta.meta_key='_product_id' , itemmeta.meta_value='$product_selected_id'"; $orders = $wpdb->get_col( implode( ' ', $query ) ); // items in other orders.... $query = array(); $query['fields'] = "select itemmeta.meta_value {$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'] = " p.id in (".implode(', ', $orders).") , (itemmeta.meta_key='_product_id' , 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 bought product 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 } ?>
Comments
Post a Comment