0

I have one simple product it's price zero(0). it has a custom option with name and price like:

-------------------------
   Name           Price
-------------------------
 product A         10

 product B         00

 product C         30

 product D         20

by default price sort is working like 0,10,20,30. But i want to sort start from 10,20,30 while any custom product has 0 price.

Please check image. enter image description here

thanks

1 Answer 1

0

This can be done using Plugin

Create the following files in your module: (Assuming you have your own module registered already)

Create your Plugin under Vendor/Modulename/etc/di.xml

di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Catalog\Model\Config">
    <plugin name="Vendor_Modulename::addCustomOptions" type="Vendor\Modulename\Plugin\Model\Config" />
</type>
<type name="Magento\Catalog\Block\Product\ProductList\Toolbar">
    <plugin name="Vendor::addPriceDescendingFilterInToolbar" type="Vendor\Modulename\Plugin\Product\ProductList\Toolbar" />
</type>

Then create a config.php under namespace Vendor/Modulename/Plugin/Model/Config.php

and put this code:

<?php

namespace Vendor\Modulename\Plugin\Model;

use Magento\Store\Model\StoreManagerInterface;

class Config  {


protected $_storeManager;

public function __construct(
    StoreManagerInterface $storeManager
) {
    $this->_storeManager = $storeManager;
}


public function afterGetAttributeUsedForSortByArray(\Magento\Catalog\Model\Config $catalogConfig, $options)
{
    $store = $this->_storeManager->getStore();
    $currencySymbol = $store->getCurrentCurrency()->getCurrencySymbol();

    // Remove specific default sorting options
    $default_options = [];
    $default_options['name'] = $options['name'];

    //New sorting options
    $customOption['price_pcs'] = __( ' Price customSorting' );

    //Merge default sorting options with custom options
    $options = array_merge($customOption, $options);

    return $options;
}
}

Then override the Toolbar.php ***Vendor/Modulename/Plugin/Product/ProductList/Toolbar.php

use this code

<?php
namespace Vendor\Modulename\Plugin\Product\ProductList;

class Toolbar
{

public function aroundSetCollection(
    \Magento\Catalog\Block\Product\ProductList\Toolbar $subject,
    \Closure $proceed,
    $collection
) {
    $currentOrder = $subject->getCurrentOrder();
    $result = $proceed($collection);

    if ($currentOrder) {
        if ($currentOrder == 'price_pcs') {
            $subject->getCollection()->setOrder('(price = 0, price)', 'asc');
        }
    }


    return $result;
}
}
8
  • This uploaded module is working like default module working for sorting price. please check my update quetions with image. thanks for reply. Commented Jan 2, 2019 at 9:55
  • @Sarfaraj , clarification , so you want highest to lowest? what's with the two custom option and simple product?
    – fmsthird
    Commented Jan 2, 2019 at 10:01
  • i want lowest to highest if custom option price is lower than product price then it should display first. like that Commented Jan 2, 2019 at 10:03
  • from your illustration, it would be 1 2 3 4?
    – fmsthird
    Commented Jan 2, 2019 at 10:11
  • i think you need custom option price first if its is lower than higher which is minimum than other product price.
    – Rv Singh
    Commented Jan 2, 2019 at 10:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.