Question

I'm trying to compile after upgrade to magento 2.3.5-p1 and I get this error:

public_html$ php bin/magento setup:di:compile
The directory "/public_html/generated/code/Magento" cannot be deleted Warning!rmdir(/public_html/generated/code/Magento): Directory not empty
public_html$ php bin/magento setup:di:compile
Compilation was started.
Interception cache generation... 6/8 [=========>---]  75% 1 min 399.0 MiBErrors during compilation:
        Plumrocket\Amp\Block\Catalog\Product\Widget\Category
                Incompatible argument type: Required type: \Magento\Catalog\Api\CategoryRepositoryInterface. Actual type: array; File:
/public_html/app/code/Plumrocket/Amp/Block/Catalog/Product/Widget/Category.php

        Plumrocket\Amp\Controller\Api\Store\SwitchAction
                Missed required argument storeSwitcher in parent::__construct call. File: /public_html/app/code/Plumrocket/Amp/Controller/Api/Store/SwitchAction.php
Total Errors Count: 2

Category.php file:

<?php
/**
 * Plumrocket Inc.
 * NOTICE OF LICENSE
 * This source file is subject to the End-user License Agreement
 * that is available through the world-wide-web at this URL:
 * http://wiki.plumrocket.net/wiki/EULA
 * If you are unable to obtain it through the world-wide-web, please
 * send an email to support@plumrocket.com so we can send you a copy immediately.
 *
 * @package     Plumrocket Amp v2.x.x
 * @copyright   Copyright (c) 2018 Plumrocket Inc. (http://www.plumrocket.com)
 * @license     http://wiki.plumrocket.net/wiki/EULA  End-user License Agreement
 */

namespace Plumrocket\Amp\Block\Catalog\Product\Widget;

use Magento\Framework\Exception\LocalizedException;

/**
 * Class ListSlider
 *
 * @method \Magento\Catalog\Model\ResourceModel\Product\Collection|null getProductCollection()
 * @method integer|null getProductsCount()
 * @method $this setProductsCount($number)
 */
class Category extends \Magento\CatalogWidget\Block\Product\ProductsList implements \Magento\Widget\Block\BlockInterface
{
    /**
     * Widget identificator
     */
    const WIDGET_TYPE = 'amp_category_list';

    /**
     * This constant does not set the default value for the "Number of Products to Display" option
     */
    const DEFAULT_PRODUCTS_COUNT = 5;

    /**
     * Default sort by for product collection and default value for option
     */
    const DEFAULT_COLLECTION_SORT_BY = 'name';

    /**
     * Default sort order for product collection and default value for option
     */
    const DEFAULT_COLLECTION_ORDER = 'asc';

    /**
     * Default value for option "Display Add To Cart Button"
     */
    const DEFAULT_SHOW_ADD_TO_CART = false;

    /**
     * @var \Magento\Catalog\Api\CategoryRepositoryInterface
     */
    private $categoryRepository;

    /**
     * @var bool|null
     */
    private $canShow;

    /**
     * @return string
     */
    public function getType()
    {
        return self::WIDGET_TYPE;
    }

    /**
     * Category constructor.
     *
     * @param \Magento\Catalog\Block\Product\Context                         $context
     * @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
     * @param \Magento\Catalog\Model\Product\Visibility                      $catalogProductVisibility
     * @param \Magento\Framework\App\Http\Context                            $httpContext
     * @param \Magento\Rule\Model\Condition\Sql\Builder                      $sqlBuilder
     * @param \Magento\CatalogWidget\Model\Rule                              $rule
     * @param \Magento\Widget\Helper\Conditions                              $conditionsHelper
     * @param \Magento\Catalog\Api\CategoryRepositoryInterface               $categoryRepository
     * @param array                                                          $data
     */
    public function __construct(
        \Magento\Catalog\Block\Product\Context $context,
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
        \Magento\Catalog\Model\Product\Visibility $catalogProductVisibility,
        \Magento\Framework\App\Http\Context $httpContext,
        \Magento\Rule\Model\Condition\Sql\Builder $sqlBuilder,
        \Magento\CatalogWidget\Model\Rule $rule,
        \Magento\Widget\Helper\Conditions $conditionsHelper,
        \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository,
        array $data = []
    ) {
        parent::__construct(
            $context,
            $productCollectionFactory,
            $catalogProductVisibility,
            $httpContext,
            $sqlBuilder,
            $rule,
            $conditionsHelper,
            $data
        );
        $this->categoryRepository = $categoryRepository;
    }

    /**
     * Retrieve product collection
     *
     * @return \Magento\Catalog\Model\ResourceModel\Product\Collection|null
     */
    public function createCollection()
    {
        /** @var $collection \Magento\Catalog\Model\ResourceModel\Product\Collection */
        $collection = $this->productCollectionFactory->create();

        $collection = $this->_addProductAttributesAndPrices($collection)
            ->setVisibility($this->catalogProductVisibility->getVisibleInCatalogIds())
            ->addStoreFilter()
            ->setPageSize($this->getPageSize())
            ->setCurPage(1)
            ->setOrder($this->getSortBy(), $this->getSortOrder());

        $collection->addCategoryFilter($this->getCategory());

        return $collection;
    }

    /**
     * @return bool|null
     */
    private function canShow()
    {
        if (null === $this->canShow) {
            $this->canShow = null !== $this->getCategory();
        }

        return $this->canShow;
    }

    /**
     * Set default template for widget in cms page
     *
     * @return $this|\Magento\CatalogWidget\Block\Product\ProductsList
     */
    protected function _beforeToHtml()
    {
        if (! $this->canShow()) {
            return $this;
        }

        if (! $this->getTemplate()) {
            $this->setTemplate('Plumrocket_Amp::catalog/product/widget/items.phtml');
        }

        return parent::_beforeToHtml();
    }

    /**
     * Disable render for invalid widgets
     *
     * @return string
     */
    protected function _toHtml()
    {
        if (! $this->canShow()) {
            return '';
        }

        return parent::_toHtml();
    }

    /**
     * Add cache key info
     *
     * @return array|int
     */
    public function getCacheKeyInfo()
    {
        $baseCacheKeyInfo = parent::getCacheKeyInfo();

        array_push($baseCacheKeyInfo, self::WIDGET_TYPE, $this->_getWidgetParams(true));

        return $baseCacheKeyInfo;
    }

    /**
     * @param bool $toString
     * @return array|string
     */
    protected function _getWidgetParams($toString = false)
    {
        $params = [
            $this->getCategoryId(),
            $this->getSortBy(),
            $this->getSortOrder(),
            $this->showAddToCart()
        ];

        return $toString ? implode('|', $params) : $params;
    }

    /**
     * Get selected category
     *
     * @return null|\Magento\Catalog\Api\Data\CategoryInterface|\Magento\Catalog\Model\Category
     */
    public function getCategory()
    {
        try {
            return $this->categoryRepository->get($this->getCategoryId());
        } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
            return null;
        } catch (LocalizedException $e) {
            return null;
        }
    }

    /**
     * Retrieve count of products
     *
     * @return int
     */
    public function hasItems()
    {
        return $this->getProductCollection()->getSize();
    }

    /**
     * Retrieve number of products to display
     *
     * @return int
     */
    public function getPageSize()
    {
        if ((int)$this->getProductsCount()) {
            return (int)$this->getProductsCount();
        }

        return self::DEFAULT_PRODUCTS_COUNT;
    }

    /**
     * Retrieve category id
     *
     * @return int
     * @throws LocalizedException
     */
    public function getCategoryId()
    {
        $catId = $this->getData('category');
        if ($catId && strpos($catId, '/') !== false) {
            $result = explode('/', $catId);
            $catId = $result[1];
        }

        if (! (int)$catId || ! is_numeric($catId)) {
            throw new LocalizedException(__('Category id is not valid'));
        }

        return (int)$catId;
    }

    /**
     * Retrieve sort by: price, position and etc.
     *
     * @return mixed
     */
    public function getSortBy()
    {
        if (! $this->hasData('sort_by')) {
            $this->setData('sort_by', self::DEFAULT_COLLECTION_SORT_BY);
        }

        return $this->getData('sort_by');
    }

    /**
     * Retrieve sort order: asc/desc
     *
     * @return string
     */
    public function getSortOrder()
    {
        if (! $this->hasData('sort_order')) {
            $this->setData('sort_order', self::DEFAULT_COLLECTION_ORDER);
        }

        return $this->getData('sort_order');
    }

    /**
     * @return bool
     */
    public function showAddToCart()
    {
        if (! $this->hasData('show_add_to_cart')) {
            $this->setData('show_add_to_cart', self::DEFAULT_SHOW_ADD_TO_CART);
        }

        return (bool)$this->getData('show_add_to_cart');
    }
}

SwitchAction.php file:

<?php
/**
 * Plumrocket Inc.
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the End-user License Agreement
 * that is available through the world-wide-web at this URL:
 * http://wiki.plumrocket.net/wiki/EULA
 * If you are unable to obtain it through the world-wide-web, please
 * send an email to support@plumrocket.com so we can send you a copy immediately.
 *
 * @package     Plumrocket_Amp
 * @copyright   Copyright (c) 2019 Plumrocket Inc. (http://www.plumrocket.com)
 * @license     http://wiki.plumrocket.net/wiki/EULA  End-user License Agreement
 */

namespace Plumrocket\Amp\Controller\Api\Store;

use Magento\Framework\App\Action\Context as ActionContext;
use Magento\Framework\App\Http\Context as HttpContext;
use Magento\Framework\App\Request\InvalidRequestException;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Store\Api\StoreCookieManagerInterface;
use Magento\Store\Api\StoreRepositoryInterface;
use Magento\Store\Model\StoreIsInactiveException;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Store\Model\StoreSwitcherInterface;
use Plumrocket\Amp\Model\Result\AmpJson;
use Plumrocket\Amp\Model\Result\AmpJsonFactory;
use Magento\Store\Model\Store;

class SwitchAction extends \Magento\Store\Controller\Store\SwitchAction implements
    \Plumrocket\Amp\Model\MagentoTwoTwo\CsrfAwareActionInterface
{
    use \Plumrocket\Amp\Controller\ValidateForCsrfTrait;

    /**
     * @var AmpJsonFactory
     */
    private $ampResultFactory;

    /**
     * SwitchAction constructor.
     *
     * @param ActionContext                               $context
     * @param StoreCookieManagerInterface                 $storeCookieManager
     * @param HttpContext                                 $httpContext
     * @param StoreRepositoryInterface                    $storeRepository
     * @param StoreManagerInterface                       $storeManager
     * @param AmpJsonFactory $ampResultFactory
     */
    public function __construct(
        ActionContext $context,
        StoreCookieManagerInterface $storeCookieManager,
        HttpContext $httpContext,
        StoreRepositoryInterface $storeRepository,
        StoreManagerInterface $storeManager,
        AmpJsonFactory $ampResultFactory
    ) {
        parent::__construct(
            $context,
            $storeCookieManager,
            $httpContext,
            $storeRepository,
            $storeManager
        );
        $this->ampResultFactory = $ampResultFactory;
    }

    /**
     * @return AmpJson
     */
    public function execute() : AmpJson
    {
        $ampJsonResult = $this->ampResultFactory->create();

        $currentActiveStore = $this->storeManager->getStore();

        $targetStoreCode = $this->_request->getParam(
            '___store',
            $this->storeCookieManager->getStoreCodeFromCookie()
        );
        $fromStoreCode = $this->_request->getParam('___from_store');

        $error = null;
        try {
            $fromStore = $this->storeRepository->get($fromStoreCode);
            $targetStore = $this->storeRepository->getActiveStoreByCode($targetStoreCode);
        } catch (StoreIsInactiveException $e) {
            $error = __('Requested store is inactive');
        } catch (NoSuchEntityException $e) {
            $error = __("The store that was requested wasn't found. Verify the store and try again.");
        }
        if ($error !== null) {
            $ampJsonResult->addErrorMessage($error);
        } else {
            $defaultStoreView = $this->storeManager->getDefaultStoreView();
            if ($defaultStoreView->getId() == $targetStore->getId()) {
                $this->storeCookieManager->deleteStoreCookie($targetStore);
            } else {
                $this->httpContext->setValue(Store::ENTITY, $targetStore->getCode(), $defaultStoreView->getCode());
                $this->storeCookieManager->setStoreCookie($targetStore);
            }
            if ($targetStore->isUseStoreInUrl()) {
                // Change store code in redirect url
                if (strpos($this->_redirect->getRedirectUrl(), $currentActiveStore->getBaseUrl()) !== false) {
                    $redirectUrl = str_replace(
                        $currentActiveStore->getBaseUrl(),
                        $targetStore->getBaseUrl(),
                        $this->_redirect->getRedirectUrl()
                    );
                } else {
                    $redirectUrl = $targetStore->getBaseUrl();
                }
            } else {
                $redirectUrl = $this->_redirect->getRedirectUrl();
            }

            $ampJsonResult->setFormRedirect($redirectUrl);
        }

        return $ampJsonResult;
    }
}

any idea how to slove the issue? Thanks, Kobi

Was it helpful?

Solution

check below code

Category.php (just replace below the constructor)

  public function __construct(
        \Magento\Catalog\Block\Product\Context $context,
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
        \Magento\Catalog\Model\Product\Visibility $catalogProductVisibility,
        \Magento\Framework\App\Http\Context $httpContext,
        \Magento\Rule\Model\Condition\Sql\Builder $sqlBuilder,
        \Magento\CatalogWidget\Model\Rule $rule,
        \Magento\Widget\Helper\Conditions $conditionsHelper,
        \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository,
        array $data = []
    ) {
        $this->categoryRepository = $categoryRepository;
        parent::__construct(
            $context,
            $productCollectionFactory,
            $catalogProductVisibility,
            $httpContext,
            $sqlBuilder,
            $rule,
            $conditionsHelper,
            $categoryRepository,
            $data
        );
    }

SwitchAction.php (just replace below the constructor)

public function __construct(
    ActionContext $context,
    StoreCookieManagerInterface $storeCookieManager,
    HttpContext $httpContext,
    StoreRepositoryInterface $storeRepository,
    StoreManagerInterface $storeManager,
    StoreSwitcherInterface $StoreSwitcher,
    Store $store,
    AmpJsonFactory $ampResultFactory
) {
    $this->ampResultFactory = $ampResultFactory;
    parent::__construct(
        $context,
        $storeCookieManager,
        $httpContext,
        $storeRepository,
        $storeManager,
        $StoreSwitcher,
        $store
    );
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top