0

I try to add this new module in Magento 2.2.2, but after php bin/magento setup:upgrade nothing happens. I try to enable this module in the app/etc/config.php too but is the same. I can't install it. I do something wrong? Or what is not okay here?

Thank you

app/code/Vendor/Module/etc/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\Block\Product\ProductList\Toolbar">
        <plugin name="custom_custom_block_toolbar" type="Vendor\Module\Plugin\Catalog\Block\Toolbar" />
    </type>

    <type name="Magento\Catalog\Model\Config">
        <plugin name="custom_catalog_model_config" type="Vendor\Module\Plugin\Catalog\Model\Config" />
    </type>

</config>

app/code/Vendor/Module/Plugin/Catalog/Model/Config.php

<?php

namespace Vendor\Module\Plugin\Catalog\Model;

class Config
{
    public function afterGetAttributeUsedForSortByArray(
    \Magento\Catalog\Model\Config $catalogConfig,
    $options
    ) {

        $options['low_to_high'] = __('Price - Low To High');
        $options['high_to_low'] = __('Price - High To Low');
        return $options;

    }

}

app/code/Vendor/Module/Plugin/Catalog/Block/Toolbar.php

<?php
namespace Vendor\Module\Plugin\Catalog\Block;

class Toolbar
{

    /**
    * Plugin
    *
    * @param \Magento\Catalog\Block\Product\ProductList\Toolbar $subject
    * @param \Closure $proceed
    * @param \Magento\Framework\Data\Collection $collection
    * @return \Magento\Catalog\Block\Product\ProductList\Toolbar
    */
    public function aroundSetCollection(
    \Magento\Catalog\Block\Product\ProductList\Toolbar $subject,
    \Closure $proceed,
    $collection
    ) {
    $currentOrder = $subject->getCurrentOrder();
    $result = $proceed($collection);

    if ($currentOrder) {
        if ($currentOrder == 'high_to_low') {
            $subject->getCollection()->setOrder('price', 'desc');
        } elseif ($currentOrder == 'low_to_high') {
            $subject->getCollection()->setOrder('price', 'asc');
        }
    }

    return $result;
    }

}
1

First, you need to create registration.php and module.xml like,

app/code/Vendor/Module/etc/module.xml

<?xml version="1.0"?>
   <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="Vendor_Module" setup_version="1.0.0"></module>
    <sequence>
        <module name="Magento_Catalog"/>
    </sequence>
  </config>

app/code/Vendor/Module/registration.php

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
      \Magento\Framework\Component\ComponentRegistrar::MODULE,
        'Vendor_Module',
         __DIR__
 );

If you want to Override Toolbar.php to add price asc and desc, You must have to override the getAvailableOrder() and setCollection() function of the Magento

vendor/magento/module-catalog/Block/Product/ProductList/Toolbar.php

Override this file in di.xml

app/code/Vendor/Module/etc/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\Block\Product\ProductList\Toolbar">
        <plugin name="custom_custom_block_toolbar" type="Vendor\Module\Plugin\Catalog\Block\Toolbar" />
    </type>
  </config>

Below is the code which may help you.

app/code/Vendor/Module/Plugin/Catalog/Block/Toolbar.php

<?php
namespace Vendor\Module\Plugin\Catalog\Block;

class Toolbar
{
    public function aroundGetAvailableOrders(\Magento\Catalog\Block\Product\ProductList\Toolbar $subject, \Closure $proceed)
    {
       $returnValue = $proceed();
       unset($returnValue['price']);
       $returnValue['priceDesc'] = 'price - high to low';
       $returnValue['priceAsc'] = 'price - low to high';
       return $returnValue;
    }

    public function aroundSetCollection(\Magento\Catalog\Block\Product\ProductList\Toolbar $subject, \Closure $proceed, $collection)
    {
       $returnValue = $proceed($collection);
       if ($subject->getCurrentOrder() == 'priceHighToLow') {
       $collection->addAttributeToSelect('*')->setOrder('price', 'ASC');
       $collection->load();
    }
    return $collection;
}
?>
0

Does your module appear as an Enabled/Disabled module when you run this command?

magento module:status

All of my modules include the following starting point files. Perhaps add those missing files to your module and then run this from Magento root directory:

composer update && php bin/magento setup:upgrade

Starting Point Files

app/code/Vendor/Module/etc/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">
</config>

app/code/Vendor/Module/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Vendor_Module" setup_version="1.0.0"></module>
        <sequence>
            <module name="Magento_Backend"/>
             <module name="Magento_Sales"/>
            <module name="Magento_Quote"/>
            <module name="Magento_Checkout"/>
        </sequence>
</config>

app/code/Vendor/Module/composer.json

{
    "name": "vendor/module",
    "description": "Vendor Module",
    "require": {
      "php": "~5.6.0|7.0.2|~7.0.6",
      "magento/module-store": "0.74.0-beta4",
      "magento/module-theme": "0.74.0-beta4",
      "magento/module-widget": "0.74.0-beta4",
      "magento/module-backend": "0.74.0-beta4",
      "magento/module-catalog": "0.74.0-beta4",
      "magento/module-email": "0.74.0-beta4",
      "magento/module-ui": "0.74.0-beta4",
      "magento/module-variable": "0.74.0-beta4",
      "magento/module-media-storage": "0.74.0-beta4",
      "magento/framework": "0.74.0-beta4",
      "magento/magento-composer-installer": "*"
    },
    "type": "magento2-module",
    "version": "0.74.0-beta4",
    "license": [
        "OSL-3.0",
        "AFL-3.0"
    ],
    "extra": {
        "map": [
            [
                "*",
                "Vendor/Module"
            ]
        ]
    }
}

app/code/Vendor/Module/registration.php

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendor_Module',
    __DIR__
);
  • I see thank you, I try all of this believe me before to ask here, my problem is with this module, what is wrong? for what can't be installed? you can install it? is work on your side? – Robert Jan 18 '18 at 23:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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