Contact Us

You can call us at +972-2-6483441
or fill out the form below and we will get in touch with you shortly

Your name:

Phone Number:

Email:

Website:

Message:

Please answer this security question
What sweet substance do bees make?

Shopp sort products

We are currently using the Shopp wordpress plugin for an client who has an e-commerce site.  Overall I found it has a nice balance between working out of the box, and being customizable.  Some e-commerce systems such as Mangento work great out of the box but are nearly impossible to customize.

One problem I had was with ordering the product within a category.  The system comes build in with 7 sorting options: name, bestsellers, random, price (low to high), price (high to low), newest to oldest and oldest to newest.  There is a drop down list that the user can change the sort order, but the site administrator can set the default.  I have found that there is a large bias towards whichever product is first in the list and want to be able to manually sort it.

I cannot add another sort method as I would need to edit the core, so I thought the best option would be to change the age of the product.  (Bestsellers is difficult to change as the system counts how many order there were for that product every time – there is no field in the database of “amount sold”). There is field in the database in the product table for ‘created’ so I thought that changing the date created would be very easy. It turned out to be a bit more difficult because of a simple error in the code. (/shopp/core/model/Category.php line 246)

 

switch ($ordering) {
...
case "newest": $loading['order'] = "pd.created DESC"; break;
case "oldest": $loading['order'] = "pd.created ASC"; break;
...
default: $loading['order'] = "p.name ASC"; break;
}

The “pd” refers to the price table NOT the product table.  The code should be “p.created DESC…”.  This unexpected behavior cost me a few hours of my life.  Still it was overall pretty easy to add this feature. First I create a custom box in the edit product page. For those that have done this for a regular wordpress post you should find this code easy to understand.

function shopp_edit_date_created_meta_box() {
global $Shopp, $wpdb;
echo "This is controling the display of 'newest to olderest' sorting, so all numbers you change here will also effect the 'oldest to newest' sort as well. You can use negative numbers.";
$created=strtotime ($wpdb->get_var( "Select `created` from `wp_shopp_price` WHERE `product` = ".$Shopp->Product->id."; " ));
echo "<input style="width: 75%;" name="shopp_edit_date_created" type="text" value="".$created."" />";
echo '<input id="shopp_edit_date_created_noncename" name="shopp_edit_date_created_noncename" type="hidden" value="'.wp_create_nonce( " />';

}

 

function create_shopp_edit_date_created_meta_box() {
if ( function_exists('add_meta_box') ) {
add_meta_box( 'new-meta-boxes', 'Edit Product order', 'shopp_edit_date_created_meta_box', 'admin_page_shopp-products-edit', 'normal', 'low' );
}
}

add_action('admin_menu', 'create_shopp_edit_date_created_meta_box');

You may need to change ‘wp_shopp_price’ if you have a different table prefix.  Next you need to deal with the content that is changed.

 

function product_save_date_created( $post_id ) {
global $Shopp, $wpdb;
// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times

if ( !wp_verify_nonce( $_POST['shopp_edit_date_created_noncename'], "shopp_edit_date_created" )) {
return $post_id;
}

// verify if this is an auto save routine. If it is our form has not been submitted, so we dont want
// to do anything
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return $post_id;

if ( !current_user_can( 'edit_page', $post_id ) ){
return $post_id;
}

// OK, we're authenticated: we need to find and save the data
$newDate=date ("Y-m-d H:i:s", $_POST['shopp_edit_date_created']);
$wpdb->query( $wpdb->prepare( "UPDATE `wp_shopp_price` SET `created` = %s WHERE `product` = ".$Shopp->Product->id."; ", $newDate ) );

}

add_action('shopp_product_saved', 'product_save_date_created');

 

I have purposely made the input a unix time-stamp and NOT a formatted date.  This way you can easy set the order as 1000, 2000, 3000 ect, and not have to think about the fact that you are, in fact, setting dates in 1970.  Again make sure to change ‘wp_shopp_price’ if you have a different table prefix.

Feel free to use this code as you wish, but if you do I ask that you link to this post from somewhere on your site.