Woocommerce – Changing the order of the upsell products

Question

Having some trouble with a clients site.

We have a product with four upsell products linked, and they display on the site. Only problem is, those products are listed on the page in a seemingly random order (IE, no correlation between ID’s, post dates, names etc).

What we want, is for those products to be shown, as inputted in the back end.

This is currently the code set up we currently have going on (as far as I’m aware, this is vanilla WC, with one addition I will point out.

<?php
/**
 * Single Product Up-Sells
 *
 * @author      WooThemes
 * @package     WooCommerce/Templates
 * @version     1.6.4
 */

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

global $product, $woocommerce_loop;

$upsells = $product->get_upsells();

if ( sizeof( $upsells ) == 0 ) return;

$meta_query = WC()->query->get_meta_query();

$args = array(
    'post_type'           => 'product',
    'ignore_sticky_posts' => 1,
    'no_found_rows'       => 1,
    'posts_per_page'      => $posts_per_page,
    'orderby'             => 'post__in',
    'order'               => 'asc',
    'post__in'            => $upsells,
    'post__not_in'        => array( $product->id ),
    'meta_query'          => $meta_query
);

$products = new WP_Query( $args );

$woocommerce_loop['columns'] = $columns;

if ( $products->have_posts() ) : ?>

<div class="panel panel-default">
    <div class="panel-heading">
        <h2 class="panel-title"><?php _e( 'You may also like&hellip;', 
'woocommerce' ) ?></h2>
    </div>
    <div class="panel-body">

        <?php woocommerce_product_loop_start(); ?>

            <?php while ( $products->have_posts() ) : $products->the_post(); ?>

                <?php wc_get_template_part( 'content', 'product' ); ?>

            <?php endwhile; // end of the loop. ?>

        <?php woocommerce_product_loop_end(); ?>

    </div>
</div>

<?php endif;

wp_reset_postdata();

It seems that the query that gets run from this query is wrong. (I’m not amazingly versed in the ways of SQL, nor the structure of the WP database)

These args currently products this SQL query

$args = array(
'post_type'           => 'product',
'ignore_sticky_posts' => 1,
'no_found_rows'       => 1,
'posts_per_page'      => $posts_per_page,
'orderby'             => 'post__in',
'order'               => 'asc',
'post__in'            => $upsells,
'post__not_in'        => array( $product->id ),
'meta_query'          => $meta_query
);

SELECT wp_posts.ID FROM wp_posts INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) WHERE 1=1 AND wp_posts.ID IN (8487,8522,8478,11028) AND ( ( wp_postmeta.meta_key = '_visibility' AND wp_postmeta.meta_value IN ('visible','catalog') ) ) AND wp_posts.post_type = 'product' AND (wp_posts.post_status = 'publish') GROUP BY wp_posts.ID ORDER BY wp_posts.menu_order, FIELD( wp_posts.ID, 8487,8522,8478,11028 ) LIMIT 0, 100

Tried so far

  • Changing 'orderby' to both 'post__in' and 'menu_order'
  • Changing 'order' to both 'asc', 'desc', and removing completely
  • Creating an array of id’s and pass that new array into 'post__in'
  • Removing 'post__not_in'

I know this is off topic, via the woocommerce tag, but I’m hoping the core issue is with the WP query (I can’t imagine it being anything WC specific). If not, I’m more than happy to relocate.

Cheers!

0
, , , Liam Winterton 4 years 2017-05-17T04:09:02-05:00 0 Answers 114 views 0

Leave an answer

Browse
Browse