• I’m using a shortcode for Woocommerce to list products. However, I want it to list the products by price from high to low. Below is the shortcode. Any suggestions would be appreciated.

    function woocommerce_products($atts){
    global $woocommerce_loop;

    if (empty($atts)) return;

    extract(shortcode_atts(array(
    ‘columns’ => ‘3’,
    ‘orderby’ => ‘name’,
    ‘order’ => ‘asc’
    ), $atts));

    $woocommerce_loop[‘columns’] = $columns;

    $args = array(
    ‘post_type’ => ‘product’,
    ‘post_status’ => ‘publish’,
    ‘ignore_sticky_posts’ => 1,
    ‘orderby’ => $orderby,
    ‘order’ => $order,
    ‘posts_per_page’ => -1,
    ‘meta_query’ => array(
    array(
    ‘key’ => ‘_visibility’,
    ‘value’ => array(‘catalog’, ‘visible’),
    ‘compare’ => ‘IN’
    )
    )
    );

    if(isset($atts[‘skus’])){
    $skus = explode(‘,’, $atts[‘skus’]);
    $skus = array_map(‘trim’, $skus);
    $args[‘meta_query’][] = array(
    ‘key’ => ‘_sku’,
    ‘value’ => $skus,
    ‘compare’ => ‘IN’
    );
    }

    if(isset($atts[‘ids’])){
    $ids = explode(‘,’, $atts[‘ids’]);
    $ids = array_map(‘trim’, $ids);
    $args[‘post__in’] = $ids;
    }

    query_posts($args);

    ob_start();
    woocommerce_get_template_part( ‘loop’, ‘shop’ );
    wp_reset_query();
    return ob_get_clean();
    }

Viewing 1 replies (of 1 total)
  • Add this to your shortcode:

    orderby=”price” order=”desc”

    or if you want the price to go from low to high:

    orderby=”price” order=”asc”

Viewing 1 replies (of 1 total)
  • The topic ‘WooCommerce Shortcode Alteration’ is closed to new replies.