MarketPress Products List By Custom Priority

Hi,
According to the documentation, the [mp_list_products] shotcode can only sort the products by title, date, ID, author, price, sales, rand (random).
Is it any possible that we can sort the products list in additional setting, namely 'priority'.
Can it be consider to be put into the future updates of marketpress?

Is it possible for us to use custom field to be a temporary solution of sorting as the priority we set?

Thanks

  • fitoussi

    Hi Solarix,
    if you'll follow the steps below you can archive the custom priority.
    this will add "Custom ID" item to the "order Products By" in the presentation tab in store settings. (image 1).

    once you choose it it will display the products by the numbers you will give in the custom field "my_product_id" of each product.
    you will need to do a little editing in 3 files. not a big deal.

    those are the steps:

    1) open the file "marketplace.php" in "/wp-content/plugins/marketpress/marketpress.php"
    go to line 1022 you will see this:

    //get order by
            if ($settings['order_by'] == 'price')
              $order_by_query = '&meta_key=mp_price&orderby=mp_price';
            else if ($settings['order_by'] == 'sales')
              $order_by_query = '&meta_key=mp_sales_count&orderby=mp_sales_count';
            else
              $order_by_query = '&orderby='.$settings['order_by'];

    add the line below just before the "else""

    else if ($settings['order_by'] == 'custom')
              $order_by_query = '&meta_key=my_product_id&orderby=my_product_id';

    so now it will look like image 2.

    same thing exactly you need to do in lines 1115 and 6025.

    2) now same file go to line 5406. you will see this line :

    <option value="rand"<?php selected($settings['order_by'], 'rand') ?>><?php _e('Random', 'mp') ?></option>

    just below it add this line:

    <option value="custom_id"<?php selected($settings['order_by'], 'custom_id') ?>><?php _e('Custom ID', 'mp') ?></option>

    so now your code will look like image 3.

    do the same thing in line 6142.

    3) now open the file "template-functions.php" in "/wp-content/plugins/marketpress/marketpress-includes/template-functions.php".
    just below line 1238 which is :

    $order_by_query = '&meta_key=mp_sales_count&orderby=mp_sales_count';

    add the lines:

    else if ($settings['order_by'] == 'custom_id')
              $order_by_query = '&meta_key=my_product_id&orderby=my_product_id';

    so now it will look like image 4.

    3) open the file "functions.php of your theme.
    on the top of the page just below the <?php insert this line

    add_post_type_support( 'product', 'custom-fields' );

    this function will now display the custom fields of your products( image 5).

    now just add a new custom field "my_product_id" and in the value you'll give each product the number you wish.

    that will be it. maybe a little confusing but very simple. i quick tested on my site and it seems to work. however, i didn't get to work with the site so i am not sure it it will cause any other problems(which i believe it will not) . if you have any questions you are more then welcome to ask.

  • mysolarix

    Changes of marketpress.php

    line 1022

    //get order by
            if ($settings['order_by'] == 'price')
              $order_by_query = '&meta_key=mp_price&orderby=mp_price';
            else if ($settings['order_by'] == 'sales')
              $order_by_query = '&meta_key=mp_sales_count&orderby=mp_sales_count';
            else
              $order_by_query = '&orderby='.$settings['order_by'];
    
            //get order direction
            $order_query = '&order='.$settings['order'];

    change to

    //get order by
            if ($settings['order_by'] == 'price')
              $order_by_query = '&meta_key=mp_price&orderby=mp_price';
            else if ($settings['order_by'] == 'sales')
              $order_by_query = '&meta_key=mp_sales_count&orderby=mp_sales_count';
            else if ($settings['order_by'] == 'priority')
    					$order_by_query = '&meta_key=my_product_priority&orderby=my_product_priority';
            else
              $order_by_query = '&orderby='.$settings['order_by'];
    
            //get order direction
            $order_query = '&order='.$settings['order'];

    line 1113

    //get order by
            if ($settings['order_by'] == 'price')
              $order_by_query = '&meta_key=mp_price&orderby=mp_price';
            else if ($settings['order_by'] == 'sales')
              $order_by_query = '&meta_key=mp_sales_count&orderby=mp_sales_count';
            else
              $order_by_query = '&orderby='.$settings['order_by'];
    
            //get order direction
            $order_query = '&order='.$settings['order'];

    change to

    //get order by
            if ($settings['order_by'] == 'price')
              $order_by_query = '&meta_key=mp_price&orderby=mp_price';
            else if ($settings['order_by'] == 'sales')
              $order_by_query = '&meta_key=mp_sales_count&orderby=mp_sales_count';
            else if ($settings['order_by'] == 'priority')
    					$order_by_query = '&meta_key=my_product_priority&orderby=my_product_priority';
            else
              $order_by_query = '&orderby='.$settings['order_by'];
    
            //get order direction
            $order_query = '&order='.$settings['order'];

    line 1513

    //adds our custom column headers to edit products screen
      function edit_products_columns($old_columns)	{
        global $post_status;
    
    		$columns['cb'] = '<input type="checkbox" />';
    		$columns['thumbnail'] = __('Thumbnail', 'mp');
    		$columns['title'] = __('Product Name', 'mp');
    		$columns['variations'] = __('Variations', 'mp');
    		$columns['sku'] = __('SKU', 'mp');
    		$columns['pricing'] = __('Price', 'mp');
    		if (!$settings['disable_cart']) {
      		$columns['stock'] = __('Stock', 'mp');
      		$columns['sales'] = __('Sales', 'mp');
        }
    		$columns['product_categories'] = __('Product Categories', 'mp');
    		$columns['product_tags'] = __('Product Tags', 'mp');

    change to

    //adds our custom column headers to edit products screen
      function edit_products_columns($old_columns)	{
        global $post_status;
    
    		$columns['cb'] = '<input type="checkbox" />';
    		$columns['thumbnail'] = __('Thumbnail', 'mp');
    		$columns['priority'] = __('Product Priority', 'mp');
    		$columns['title'] = __('Product Name', 'mp');
    		$columns['variations'] = __('Variations', 'mp');
    		$columns['sku'] = __('SKU', 'mp');
    		$columns['pricing'] = __('Price', 'mp');
    		if (!$settings['disable_cart']) {
      		$columns['stock'] = __('Stock', 'mp');
      		$columns['sales'] = __('Sales', 'mp');
        }
    		$columns['product_categories'] = __('Product Categories', 'mp');
    		$columns['product_tags'] = __('Product Tags', 'mp');
  • mysolarix

    line 1538

    //adds our custom column content
    	function edit_products_custom_columns($column) {
    		global $post;
    		$settings = get_option('mp_settings');
    		$meta = get_post_custom();
        //unserialize
        foreach ($meta as $key => $val) {
    		  $meta[$key] = maybe_unserialize($val[0]);
    		  if (!is_array($meta[$key]) && $key != "mp_is_sale" && $key != "mp_track_inventory" && $key != "mp_product_link")
    		    $meta[$key] = array($meta[$key]);
    		}
    
    		switch ($column) {
    			case "thumbnail":
            if (has_post_thumbnail()) {
    					echo '<a href="' . get_edit_post_link() . '" title="' . __('Edit &raquo;') . '">';
    					the_post_thumbnail(array(50,50), array('title' => ''));
    					echo '</a>';
    				}
    				break;
    
    			case "variations":
    			  if (is_array($meta["mp_var_name"]) && count($meta["mp_var_name"]) > 1) {
    					foreach ($meta["mp_var_name"] as $value) {
                echo esc_attr($value) . '<br />';
    					}
    				} else {
    					_e('N/A', 'mp');
    				}
    			  break;

    change to

    //adds our custom column content
    	function edit_products_custom_columns($column) {
    		global $post;
    		$settings = get_option('mp_settings');
    		$meta = get_post_custom();
        //unserialize
        foreach ($meta as $key => $val) {
    		  $meta[$key] = maybe_unserialize($val[0]);
    		  if (!is_array($meta[$key]) && $key != "mp_is_sale" && $key != "mp_track_inventory" && $key != "my_product_priority"&& $key != "mp_product_link")
    		    $meta[$key] = array($meta[$key]);
    		}
    
    		switch ($column) {
    			case "thumbnail":
            if (has_post_thumbnail()) {
    					echo '<a href="' . get_edit_post_link() . '" title="' . __('Edit &raquo;') . '">';
    					the_post_thumbnail(array(50,50), array('title' => ''));
    					echo '</a>';
    				}
    				break;
    
    			case "priority":
    			  echo intval($meta["my_product_priority"]);
    			  break;
    
    			case "variations":
    			  if (is_array($meta["mp_var_name"]) && count($meta["mp_var_name"]) > 1) {
    					foreach ($meta["mp_var_name"] as $value) {
                echo esc_attr($value) . '<br />';
    					}
    				} else {
    					_e('N/A', 'mp');
    				}
    			  break;

    line 1793

    //Save our post meta when a product is created or updated
    	function save_product_meta($post_id, $post = null) {
        //skip quick edit
        if ( defined('DOING_AJAX') )
          return;
    
    		if ( $post->post_type == "product" && isset( $_POST['mp_product_meta'] ) ) {
          $meta = get_post_custom($post_id);
          foreach ($meta as $key => $val) {
    			  $meta[$key] = maybe_unserialize($val[0]);
    			  if (!is_array($meta[$key]) && $key != "mp_is_sale" && $key != "mp_track_inventory" && $key != "mp_product_link")
    			    $meta[$key] = array($meta[$key]);
    			}
    
          //price function
          $func_curr = '$price = round($price, 2);return ($price) ? $price : 0;';
    
          //sku function
          $func_sku = 'return preg_replace("/[^a-zA-Z0-9_-]/", "", $value);';
    
          update_post_meta($post_id, 'mp_var_name', $_POST['mp_var_name']);
          update_post_meta($post_id, 'mp_sku', array_map(create_function('$value', $func_sku), $_POST['mp_sku']));
          update_post_meta($post_id, 'mp_price', array_map(create_function('$price', $func_curr), $_POST['mp_price']));
          update_post_meta($post_id, 'mp_is_sale', isset($_POST['mp_is_sale']) ? 1 : 0);
          update_post_meta($post_id, 'mp_sale_price', array_map(create_function('$price', $func_curr), $_POST['mp_sale_price']));
          update_post_meta($post_id, 'mp_track_inventory', isset($_POST['mp_track_inventory']) ? 1 : 0);
          update_post_meta($post_id, 'mp_inventory', array_map('intval', (array)$_POST['mp_inventory']));
    
    			//save true first variation price for sorting
    			if ( isset($_POST['mp_is_sale']) && round($_POST['mp_sale_price'][0], 2) )
    				$sort_price = round($_POST['mp_sale_price'][0], 2);
    			else
    				$sort_price = round($_POST['mp_price'][0], 2);
          update_post_meta($post_id, 'mp_price_sort', $sort_price);
    
    			//if changing delete flag so emails will be sent again
          if ( $_POST['mp_inventory'] != $meta['mp_inventory'] )
            delete_post_meta($product_id, 'mp_stock_email_sent');
    
          update_post_meta( $post_id, 'mp_product_link', esc_url_raw($_POST['mp_product_link']) );
    
          //set sales count to zero if none set
          $sale_count = ($meta["mp_sales_count"][0]) ? $meta["mp_sales_count"][0] : 0;
          update_post_meta($post_id, 'mp_sales_count', $sale_count);
    
          //for shipping plugins to save their meta values
          $mp_shipping = maybe_unserialize($meta["mp_shipping"][0]);
          if ( !is_array($mp_shipping) )
            $mp_shipping = array();
    
          update_post_meta( $post_id, 'mp_shipping', apply_filters('mp_save_shipping_meta', $mp_shipping) );
    
          //download url
          update_post_meta( $post_id, 'mp_file', esc_url_raw($_POST['mp_file']) );
    
          //for any other plugin to hook into
          do_action( 'mp_save_product_meta', $post_id, $meta );
    		}
    	}

    change to

    //Save our post meta when a product is created or updated
    	function save_product_meta($post_id, $post = null) {
        //skip quick edit
        if ( defined('DOING_AJAX') )
          return;
    
    		if ( $post->post_type == "product" && isset( $_POST['mp_product_meta'] ) ) {
          $meta = get_post_custom($post_id);
          foreach ($meta as $key => $val) {
    			  $meta[$key] = maybe_unserialize($val[0]);
    			  if (!is_array($meta[$key]) && $key != "mp_is_sale" && $key != "mp_track_inventory" && $key != "my_product_priority" && $key != "mp_product_link")
    			    $meta[$key] = array($meta[$key]);
    			}
    
          //price function
          $func_curr = '$price = round($price, 2);return ($price) ? $price : 0;';
    
          //sku function
          $func_sku = 'return preg_replace("/[^a-zA-Z0-9_-]/", "", $value);';
    
          update_post_meta($post_id, 'mp_var_name', $_POST['mp_var_name']);
          update_post_meta($post_id, 'mp_sku', array_map(create_function('$value', $func_sku), $_POST['mp_sku']));
          update_post_meta($post_id, 'mp_price', array_map(create_function('$price', $func_curr), $_POST['mp_price']));
          update_post_meta($post_id, 'mp_is_sale', isset($_POST['mp_is_sale']) ? 1 : 0);
          update_post_meta($post_id, 'mp_sale_price', array_map(create_function('$price', $func_curr), $_POST['mp_sale_price']));
          update_post_meta($post_id, 'mp_track_inventory', isset($_POST['mp_track_inventory']) ? 1 : 0);
          update_post_meta($post_id, 'mp_inventory', array_map('intval', (array)$_POST['mp_inventory']));
    
    			//save true first variation price for sorting
    			if ( isset($_POST['mp_is_sale']) && round($_POST['mp_sale_price'][0], 2) )
    				$sort_price = round($_POST['mp_sale_price'][0], 2);
    			else
    				$sort_price = round($_POST['mp_price'][0], 2);
          update_post_meta($post_id, 'mp_price_sort', $sort_price);
    
    			//if changing delete flag so emails will be sent again
          if ( $_POST['mp_inventory'] != $meta['mp_inventory'] )
            delete_post_meta($product_id, 'mp_stock_email_sent');
    
    			update_post_meta( $post_id, 'my_product_priority', absint($_POST['my_product_priority']) );
          update_post_meta( $post_id, 'mp_product_link', esc_url_raw($_POST['mp_product_link']) );
    
          //set sales count to zero if none set
          $sale_count = ($meta["mp_sales_count"][0]) ? $meta["mp_sales_count"][0] : 0;
          update_post_meta($post_id, 'mp_sales_count', $sale_count);
    
          //for shipping plugins to save their meta values
          $mp_shipping = maybe_unserialize($meta["mp_shipping"][0]);
          if ( !is_array($mp_shipping) )
            $mp_shipping = array();
    
          update_post_meta( $post_id, 'mp_shipping', apply_filters('mp_save_shipping_meta', $mp_shipping) );
    
          //download url
          update_post_meta( $post_id, 'mp_file', esc_url_raw($_POST['mp_file']) );
    
          //for any other plugin to hook into
          do_action( 'mp_save_product_meta', $post_id, $meta );
    		}
    	}

    line 1852

    //The Product Details meta box
      function meta_details() {
        global $post;
        $settings = get_option('mp_settings');
    		$meta = get_post_custom($post->ID);
      	//unserialize
        foreach ($meta as $key => $val) {
    		  $meta[$key] = maybe_unserialize($val[0]);
    		  if (!is_array($meta[$key]) && $key != "mp_is_sale" && $key != "mp_track_inventory" && $key != "mp_product_link" && $key != "mp_file")
    		    $meta[$key] = array($meta[$key]);
    		}
        ?>
        <input type="hidden" name="mp_product_meta" value="1" />
        <table class="widefat" id="mp_product_variations_table">

    change to

    //The Product Details meta box
      function meta_details() {
        global $post;
        $settings = get_option('mp_settings');
    		$meta = get_post_custom($post->ID);
      	//unserialize
        foreach ($meta as $key => $val) {
    		  $meta[$key] = maybe_unserialize($val[0]);
    		  if (!is_array($meta[$key]) && $key != "mp_is_sale" && $key != "mp_track_inventory" && $key != "my_product_priority" && $key != "mp_product_link" && $key != "mp_file")
    		    $meta[$key] = array($meta[$key]);
    		}
        ?>
        <input type="hidden" name="mp_product_meta" value="1" />
        <table class="widefat" id="mp_product_variations_table">

    line 1912

    </table>
    		<?php if (empty($meta["mp_file"]) && empty($meta["mp_product_link"])) { ?>
    		<div id="mp_add_vars"><a href="#mp_product_variations_table"><?php _e('Add Variation', 'mp'); ?></a></div>
    		<?php } else { ?>
        <span class="description" id="mp_variation_message"><?php _e('Product variations are not allowed for Downloadable or Externally Linked products.', 'mp') ?></span>
        <?php } ?>
    
        <div id="mp_product_link_div">

    change to

    </table>
    		<?php if (empty($meta["mp_file"]) && empty($meta["mp_product_link"])) { ?>
    		<div id="mp_add_vars"><a href="#mp_product_variations_table"><?php _e('Add Variation', 'mp'); ?></a></div>
    		<?php } else { ?>
        <span class="description" id="mp_variation_message"><?php _e('Product variations are not allowed for Downloadable or Externally Linked products.', 'mp') ?></span>
        <?php } ?>
    
    		<div id="my_product_priority_div">
          <?php _e('Product Priority', 'mp'); ?>:<br />
          <input type="text" name="my_product_priority" value="<?php echo intval($meta["my_product_priority"]); ?>" />
          <p/>
        </div>
    
        <div id="mp_product_link_div">

    line 5395

    <select name="mp[order_by]">
                        <option value="title"<?php selected($settings['order_by'], 'title') ?>><?php _e('Product Name', 'mp') ?></option>
                        <option value="date"<?php selected($settings['order_by'], 'date') ?>><?php _e('Publish Date', 'mp') ?></option>
                        <option value="ID"<?php selected($settings['order_by'], 'ID') ?>><?php _e('Product ID', 'mp') ?></option>
                        <option value="author"<?php selected($settings['order_by'], 'author') ?>><?php _e('Product Author', 'mp') ?></option>
                        <option value="sales"<?php selected($settings['order_by'], 'sales') ?>><?php _e('Number of Sales', 'mp') ?></option>
                        <option value="price"<?php selected($settings['order_by'], 'price') ?>><?php _e('Product Price', 'mp') ?></option>
                        <option value="rand"<?php selected($settings['order_by'], 'rand') ?>><?php _e('Random', 'mp') ?></option>
                      </select>
                      <label><input value="DESC" name="mp[order]" type="radio"<?php checked($settings['order'], 'DESC') ?> /> <?php _e('Descending', 'mp') ?></label>
                      <label><input value="ASC" name="mp[order]" type="radio"<?php checked($settings['order'], 'ASC') ?> /> <?php _e('Ascending', 'mp') ?></label>
             	  </td>

    change to

    <select name="mp[order_by]">
                        <option value="title"<?php selected($settings['order_by'], 'title') ?>><?php _e('Product Name', 'mp') ?></option>
                        <option value="date"<?php selected($settings['order_by'], 'date') ?>><?php _e('Publish Date', 'mp') ?></option>
                        <option value="ID"<?php selected($settings['order_by'], 'ID') ?>><?php _e('Product ID', 'mp') ?></option>
                        <option value="author"<?php selected($settings['order_by'], 'author') ?>><?php _e('Product Author', 'mp') ?></option>
                        <option value="sales"<?php selected($settings['order_by'], 'sales') ?>><?php _e('Number of Sales', 'mp') ?></option>
                        <option value="price"<?php selected($settings['order_by'], 'price') ?>><?php _e('Product Price', 'mp') ?></option>
                        <option value="priority"<?php selected($settings['order_by'], 'priority') ?>><?php _e('Product Priority', 'mp') ?></option>
                        <option value="rand"<?php selected($settings['order_by'], 'rand') ?>><?php _e('Random', 'mp') ?></option>
                      </select>
                      <label><input value="DESC" name="mp[order]" type="radio"<?php checked($settings['order'], 'DESC') ?> /> <?php _e('Descending', 'mp') ?></label>
                      <label><input value="ASC" name="mp[order]" type="radio"<?php checked($settings['order'], 'ASC') ?> /> <?php _e('Ascending', 'mp') ?></label>
             	  </td>

    line 6021

    //get order by
        if ($instance['order_by']) {
          if ($instance['order_by'] == 'price')
            $order_by_query = '&meta_key=mp_price&orderby=mp_price';
          else if ($instance['order_by'] == 'sales')
            $order_by_query = '&meta_key=mp_sales_count&orderby=mp_sales_count';
          else
            $order_by_query = '&orderby='.$instance['order_by'];
        } else {
          $order_by_query = '&orderby=title';
        }
    
        //get order direction
        if ($instance['order']) {

    change to

    //get order by
        if ($instance['order_by']) {
          if ($instance['order_by'] == 'price')
            $order_by_query = '&meta_key=mp_price&orderby=mp_price';
          else if ($instance['order_by'] == 'sales')
            $order_by_query = '&meta_key=mp_sales_count&orderby=mp_sales_count';
          else if ($instance['order_by'] == 'priority')
    					$order_by_query = '&meta_key=my_product_priority&orderby=my_product_priority';
          else
            $order_by_query = '&orderby='.$instance['order_by'];
        } else {
          $order_by_query = '&orderby=title';
        }
    
        //get order direction
        if ($instance['order']) {

    line 6133

    <option value="sales"<?php selected($order_by, 'sales') ?>><?php _e('Number of Sales', 'mp') ?></option>
          <option value="price"<?php selected($order_by, 'price') ?>><?php _e('Product Price', 'mp') ?></option>
          <option value="rand"<?php selected($order_by, 'rand') ?>><?php _e('Random', 'mp') ?></option>
        </select><br />
        <label><input value="DESC" name="<?php echo $this->get_field_name('order'); ?>" type="radio"<?php checked($order, 'DESC') ?> /> <?php _e('Descending', 'mp') ?></label>

    change to

    <option value="sales"<?php selected($order_by, 'sales') ?>><?php _e('Number of Sales', 'mp') ?></option>
          <option value="price"<?php selected($order_by, 'price') ?>><?php _e('Product Price', 'mp') ?></option>
          <option value="priority"<?php selected($order_by, 'priority') ?>><?php _e('Product Priority', 'mp') ?></option>
          <option value="rand"<?php selected($order_by, 'rand') ?>><?php _e('Random', 'mp') ?></option>
        </select><br />
        <label><input value="DESC" name="<?php echo $this->get_field_name('order'); ?>" type="radio"<?php checked($order, 'DESC') ?> /> <?php _e('Descending', 'mp') ?></label>
  • mysolarix

    Please change the following to the file template-functions.php in the folder of marketpress-includes

    line 1233

    //get order by
      if (!$order_by) {
        if ($settings['order_by'] == 'price')
          $order_by_query = '&meta_key=mp_price_sort&orderby=meta_value_num';
        else if ($settings['order_by'] == 'sales')
          $order_by_query = '&meta_key=mp_sales_count&orderby=meta_value_num';
        else
          $order_by_query = '&orderby='.$settings['order_by'];
      } else {
      	if ('price' == $order_by)
      		$order_by_query = '&meta_key=mp_price_sort&orderby=meta_value_num';
        else
        	$order_by_query = '&orderby='.$order_by;
      }
    
      //get order direction

    change to

    //get order by
      if (!$order_by) {
        if ($settings['order_by'] == 'price')
          $order_by_query = '&meta_key=mp_price_sort&orderby=meta_value_num';
        else if ($settings['order_by'] == 'sales')
          $order_by_query = '&meta_key=mp_sales_count&orderby=meta_value_num';
        else if ($settings['order_by'] == 'priority')
    			$order_by_query = '&meta_key=my_product_priority&orderby=meta_value_num';
        else
          $order_by_query = '&orderby='.$settings['order_by'];
      } else {
      	if ('price' == $order_by)
      		$order_by_query = '&meta_key=mp_price_sort&orderby=meta_value_num';
        else
        	$order_by_query = '&orderby='.$order_by;
      }
    
      //get order direction

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.