Sort or filter by Shipping method

  • Posts: 400
  • Thank you received: 21
  • Hikashop Business
4 months 1 week ago #368101

-- HikaShop version -- : 5.1.6
-- Joomla version -- : 4
-- PHP version -- : 8.2

In the order manager I would like to filter or sort by Shipping method.
If I use column sorting in the orders table, the order is more or less random.
Sorting by payment systems for example works.
A filter option would be even better.

Please Log in or Create an account to join the conversation.

  • Posts: 84812
  • Thank you received: 13811
  • MODERATOR
4 months 1 week ago #368105

Hi,

There is indeed no shipping method filter on the orders listing.
There are several reasons why we didn't add one:
- it's arguably less useful than a payment method filter. Some shops don't ship anything, and most that do only have 1 shipping carrier/method. But most shops have several payment methods.
- the filters area of the orders listing is already quite crowed with filters so we don't want to have too much of them. For example, several merchants requested a products filter, which we've added as an external plugin : www.hikashop.com/marketplace/product/272...-product-filter.html
- filtering on the shipping method is actually not easy as the data in the order table in the database contains not only the id of the shipping method but also the service selected (most carriers have several services, and when the shipping plugin connects to the carrier to get the list of available services, the plugin generate dynamic shipping methods, one per service, for only one shipping method configured in the backend).
So, due to all of this, we decided not to implement it.
Actually, I think that this could be done as a filter plugin, similar to the order product filter plugin we already provide.

Please Log in or Create an account to join the conversation.

  • Posts: 400
  • Thank you received: 21
  • Hikashop Business
4 months 1 week ago #368112

Thanks
Is there any chance that you will develop such a plugin in the near future?

Please Log in or Create an account to join the conversation.

  • Posts: 400
  • Thank you received: 21
  • Hikashop Business
4 months 1 week ago #368113

Let me give something back. This works for me. Do you have any other suggestions or comments?

<?php
/**
 * @package    HikaShop Order Shipping Filter Plugin
 * @version    1.0.1
 * @author     Custom
 * @license    GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
 */
defined('_JEXEC') or die('Restricted access');

class plgHikashopOrder_shipping_filter extends JPlugin
{
    public function __construct(&$subject, $config) {
        parent::__construct($subject, $config);
        $lang = JFactory::getLanguage();
        $lang->load('plg_hikashop_order_shipping_filter', JPATH_ADMINISTRATOR);
    }

    public function onBeforeOrderListing($paramBase, &$extrafilters, &$pageInfo, &$filters, &$tables, &$searchMap, &$select) {
        $extrafilters['shipping'] = $this;

        $app = JFactory::getApplication();
        $db = JFactory::getDbo();

        $pageInfo->filter->filter_shipping_id = $app->getUserStateFromRequest(
            $paramBase . ".filter_shipping_id",
            'filter_shipping_id',
            '',
            'int'
        );

        if (!empty($pageInfo->filter->filter_shipping_id)) {
            $filters[] = "b.order_shipping_id = " . (int)$pageInfo->filter->filter_shipping_id;
        }
    }

    public function displayFilter($name, $info) {
        $html = '';
        switch($name) {
            case 'shipping':
                $db = JFactory::getDbo();
                $query = $db->getQuery(true)
                    ->select('shipping_id, shipping_name')
                    ->from(hikashop_table('shipping'))
                    ->where('shipping_published = 1')
                    ->order('shipping_name ASC');
                $db->setQuery($query);
                $methods = $db->loadObjectList();

                $html .= '<select name="filter_shipping_id" id="filter_shipping_id" class="custom-select" onchange="document.adminForm.submit();">';
                $html .= '<option value="">' . JText::_('FILTER_BY_SHIPPING_METHOD') . '</option>';
                foreach($methods as $method) {
                    $selected = ($info->filter_shipping_id == $method->shipping_id) ? 'selected="selected"' : '';
                    $label = $method->shipping_id . ' : ' . $method->shipping_name;
                    $html .= '<option value="' . (int)$method->shipping_id . '" ' . $selected . '>' . htmlspecialchars($label, ENT_COMPAT, 'UTF-8') . '</option>';
                }
                $html .= '</select>';

                if(!defined('HIKASHOP_J40') || !HIKASHOP_J40) {
                    $html .= "
                    <style>
                    #filter_shipping_id {
                        width: 250px;
                        display: inline-block;
                        margin-bottom: -9px;
                    }
                    </style>";
                }
                break;
        }
        return $html;
    }
}

Please Log in or Create an account to join the conversation.

  • Posts: 84812
  • Thank you received: 13811
  • MODERATOR
4 months 1 week ago #368118

Hi,

Thanks for sharing your solution.
This will work if you only have manual shipping methods, without warehouses and without HikaMarket Multivendor.
So, as a simple custom solution for you, it's great, and maybe it can be used by someone else with a similar need.
Also, since it's a plugin, you won't have issues with updates in the future.

But that's not something we can publish as a proper plugin, unfortunately. We would have to support HikaMarket, warehouses and shipping services for that. And it would make the plugin more complex. Probably twice the amount of code.
But that's a good start none the less. I'll keep that in mind. I'm not free right now to work on this but maybe after summer.

Please Log in or Create an account to join the conversation.

Time to create page: 0.057 seconds
Powered by Kunena Forum