Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Provides a shortcode for custom product listing
<?php
/*
Plugin Name: WPMUDEV MP - Custom Products List
Plugin URI: http://premium.wpmudev.org/
Description: Provides a shortcode for custom product listing
Version: 1.0.0
Author: Panos Lyrakis (WPMUDEV)
Author URI: http://premium.wpmudev.org
License: GNU General Public License (Version 2 - GPLv2)
*/
if( ! class_exists( 'WPMUDEV_MP_Custom_Products_List' ) ){
class WPMUDEV_MP_Custom_Products_List{
private static $_instance = null;
public static function get_instance() {
if ( is_null( self::$_instance ) ) {
self::$_instance = new WPMUDEV_MP_Custom_Products_List();
}
return self::$_instance;
}
private function __construct() {
add_shortcode( 'wpmudev_products_list', array( $this, 'products_list_sh' ) );
}
public function products_list_sh( $atts, $content ){
if( ! class_exists( 'MP_Product' ) ) return;
$atts = shortcode_atts(
array(
'per_page' => '3',
'page' => 1,
'post_status' => 'publish',
'category' => null,
'tag' => null,
'paginate' => null,
'order' => null,
'order_by' => null,
'echo' => true,
'nopaging' => true,
'show_options' => false,
'exclude_downloadables' => true,
'weight' => null
), $atts, 'wpmudev_products_list' );
$query = array(
'post_type' => MP_Product::get_post_type(),
'post_status' => $atts['post_status'],
);
$tax_query = array();
if ( ! is_null( $atts['category'] ) || ! is_null( $atts['tag'] ) ) {
if ( ! is_null( $atts['category'] ) ) {
$tax_query[] = array(
'taxonomy' => 'product_category',
'field' => 'slug',
'terms' => sanitize_title( $atts['category'] ),
);
}
if ( ! is_null( $atts['tag'] ) ) {
$tax_query[] = array(
'taxonomy' => 'product_tag',
'field' => 'slug',
'terms' => sanitize_title( $atts['tag'] ),
);
}
}
if ( count( $tax_query ) > 1 ) {
$query['tax_query'] = array_merge( array( 'relation' => 'AND' ), $tax_query );
} elseif ( count( $tax_query ) == 1 ) {
$query['tax_query'] = $tax_query;
}
// Setup pagination
if ( ( ! is_null( $atts['paginate'] ) && ! $atts['paginate'] ) || ( is_null( $atts['paginate'] ) && ! mp_get_setting( 'paginate' ) ) ) {
$query['nopaging'] = $atts['nopaging'] = true;
} else {
// Figure out per page
if ( ! is_null( $atts['per_page'] ) ) {
$query['posts_per_page'] = intval( $atts['per_page'] );
} else {
$query['posts_per_page'] = intval( mp_get_setting( 'per_page' ) );
}
// Figure out page
if ( ! is_null( $atts['page'] ) ) {
$query['paged'] = intval( $atts['page'] );
} elseif ( get_query_var( 'paged' ) != '' ) {
$query['paged'] = $atts['page'] = intval( get_query_var( 'paged' ) );
} elseif ( get_query_var( 'page' ) != '' ) {
$query['paged'] = $atts['page'] = intval( get_query_var( 'page' ) );
}
}
if( !isset( $_SESSION ) ){
$_SESSION = array();
}
$order_by = isset( $_SESSION['mp_product_list_order_by'] ) ? $_SESSION['mp_product_list_order_by'] : '';
$order = isset( $_SESSION['mp_product_list_order'] ) ? $_SESSION['mp_product_list_order'] : '';
if ( ! empty( $order_by ) && ! empty( $order ) ) {
$query['orderby'] = $order_by;
$query['order'] = $order;
$atts['order_by'] = $order_by;
$atts['order'] = $order;
}
// Get order by
if ( ! is_null( $atts['order_by'] ) ) {
if ( 'price' == $atts['order_by'] ) {
$query['meta_key'] = 'sort_price';
$query['orderby'] = 'meta_value_num';
} else if ( 'sales' == $atts['order_by'] ) {
$query['meta_key'] = 'mp_sales_count';
$query['orderby'] = 'meta_value_num';
} else {
$query['orderby'] = $atts['order_by'];
}
} elseif ( 'price' == mp_get_setting( 'order_by' ) ) {
$query['meta_key'] = 'sort_price';
$query['orderby'] = 'meta_value_num';
} elseif ( 'sales' == mp_get_setting( 'order_by' ) ) {
$query['meta_key'] = 'mp_sales_count';
$query['orderby'] = 'meta_value_num';
} else {
$query['orderby'] = mp_get_setting( 'order_by' );
}
// Get order direction
$query['order'] = mp_get_setting( 'order' );
if ( ! is_null( $atts['order'] ) ) {
$query['order'] = $atts['order'];
}
$meta_queries = array();
//Get producs with specific weight
if( ! is_null( $atts['weight'] ) ){
$meta_queries[]= array(
'key' => 'weight_pounds',
'value' => $atts['weight'],
'compare' => '<=',
);
}
if( $atts['exclude_downloadables'] ){
$meta_queries[] = array(
'key' => 'product_type',
'value' => 'digital',
'compare' => '!=',
);
}
if( ! empty( $meta_queries ) ){
$query['meta_query'] = array();
foreach( $meta_queries as $meta_query ){
$query['meta_query'][] = $meta_query;
}
}
$custom_query = new WP_Query( $query );
// Get layout type
$layout_type = mp_get_setting( 'list_view' );
if ( ! is_null( $atts['list_view'] ) ) {
$layout_type = $atts['list_view'] ? 'list' : 'grid';
}
// Build content
$content = '';
if ( ! mp_doing_ajax() && $atts['show_options'] ) {
$per_page = ( is_null( $atts['per_page'] ) ) ? null : $atts['per_page'];
$content .= ( $atts['filters'] || ( is_null( $atts['filters'] ) && 1 != mp_get_setting( 'hide_products_filter' ) ) ) ? mp_products_filter( false, $per_page, $custom_query ) : '';
}
$content .= '<!-- MP Product List --><section id="mp-products" class="hfeed mp_products mp_products-' . $layout_type . '">';
if ( $last = $custom_query->post_count ) {
$content .= $layout_type == 'grid' ? _mp_products_html_grid( $custom_query ) : _mp_products_html_list( $custom_query );
} else {
$content .= '<div id="mp-products-empty">' . apply_filters( 'wpmudev_mp_product_list_none', __( 'No Products', 'mp' ) ) . '</div><!-- end mp-no-products -->';
}
$content .= '</section><!-- end mp-products -->';
$content .= ( ! $atts['nopaging'] ) ? mp_products_nav( false, $custom_query ) : '';
/**
* Filter product list html
*
* @since 3.0
*
* @param string $content The current html content.
* @param array $args The arguments passed to mp_list_products
*/
$content = apply_filters( 'wpmudev_mp_list_products', $content, $atts );
if ( $atts['echo'] ) {
echo $content;
} else {
return $content;
}
}
}
add_action( 'plugins_loaded', function(){
WPMUDEV_MP_Custom_Products_List::get_instance();
} );
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.