How create a custom sort order for wp e-commerce products :Wordress


Hello Friends! ūüôā

If you want to create a custom sort order on product list page then you have need to follow my steps given below:

  1. Firstly you have need to create a custom field in admin, so for that just add given code in your theme function files
    //Start code for custom meta box
    
    add_action( 'add_meta_boxes', 'add_custom_meta_box' );
    
    /**
     * Adds the SEO meta box to the page screen
     */
    function add_custom_meta_box()
    {
     global $meta_box;
        add_meta_box($meta_box['id'], $meta_box['title'], 'meta_show_box', $meta_box['page'], $meta_box['context'], $meta_box['priority']);
    }
    
    //Define SEO Meta box Fields
    
      $prefix = 'custom_';
        $meta_box = array(
        'id' => 'my-meta-box',
        'title' => 'Product Order Information',
        'page' => '',
        'context' => 'normal',
        'priority' => 'high',
        'fields' => array(
        array(
        'name' => 'Sort Order',
        'desc' => 'Enter product sort order',
        'id' => $prefix . 'order',
        'type' => 'text',
        'std' => '1'
        )
        )
        );
    
    //Display Cuystom Box
    function meta_show_box()
    {
    global $meta_box, $post;
        // Use nonce for verification
        echo '<input type="hidden" name="my_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
        echo '';
        foreach ($meta_box['fields'] as $field) {
        // get current post meta data
        $meta = get_post_meta($post->ID, $field['id'], true);
        echo '<p>',
        '<label for="', $field['id'], '">', $field['name'], '</label>','';
        switch ($field['type']) {
        case 'text':
        echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" />', '<br />', $field['desc'];
        break;
        case 'textarea':
        echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="4" style="width:97%">', $meta ? $meta : $field['std'], '</textarea>', '<br />', $field['desc'];
        break;
        '</p>';
        }
    
        }
    }
    
    //Save Custom Meta Box fields Value
    
    add_action( 'save_post', 'save_custom_meta_box' );
    
    function save_custom_meta_box($post_id) {
        global $meta_box;
        // verify nonce
        if (!wp_verify_nonce($_POST['custom_meta_box_nonce'], basename(__FILE__))) {
        return $post_id;
        }
        // check autosave
        if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return $post_id;
        }
        // check permissions
        if ('wpsc-product' == $_POST['post_type']) {
        if (!current_user_can('edit_page', $post_id)) {
        return $post_id;
        }
        } elseif (!current_user_can('edit_post', $post_id)) {
        return $post_id;
        }
        foreach ($meta_box['fields'] as $field) {
        $old = get_post_meta($post_id, $field['id'], true);
        $new = $_POST[$field['id']];
        if ($new && $new != $old) {
        update_post_meta($post_id, $field['id'], $new);
        } elseif ('' == $new && $old) {
        delete_post_meta($post_id, $field['id'], $old);
        }
        }
    }
  2. After done it you can see the newly added custom meta box field during edit/add the product
    raghunath-customfileds
  3. Now you have need to edit in query.php file of wp e-commrece plugin , just find the wpsc_product_sort_order_query_vars() function and add here a new condition like that
    case "custom_order":
                $query_vars["meta_key"] ='custom_order';
                $query_vars["orderby"] = 'meta_value_num';
                break;
  4. And finally call your newely added custom sort order in category list short-code like that :
    sort_order='custom_order'
  5. That's all :)

If you found this code very help full then please leave a good comment and you can also donate me.

Enjoy Code! | Raghunath Blog

Advertisements

Sr. Software Engineer

Tagged with: , , , , ,
Posted in Wordpress
One comment on “How create a custom sort order for wp e-commerce products :Wordress
  1. Marilou says:

    Please let me know if you’re looking for a writer for your site.
    You have some really great articles and I believe
    I would be a good asset. If you ever want to take some of the load off, I’d love to write some material for your blog in exchange for a link back to mine.
    Please blast me an e-mail if interested. Thanks!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: