如何覆盖magento 2愿望清单模型?

时间:2019-05-08 06:51:11

标签: php magento magento2

我正在开发必须 override wishlist.php模型的模块,但是当我尝试覆盖它时,我无法生成错误。我为您提供了错误,但是您能告诉我为什么它不被覆盖吗?

我要覆盖的模型路径:

vendor/magento/module-wishlist/Model/Wishlist.php

我遇到的错误:

  

致命错误:未捕获的TypeError:参数1传递给   Magento \愿望清单\模型\资源模型\项目\集合:: addWishlistFilter()   必须是Magento \ Wishlist \ Model \ Wishlist的实例,   给定的My \ Multiwishlist \ Model \ Wishlist,称为   /opt/lampp/htdocs/qv3/app/code/My/Multiwishlist/Model/Wishlist.php上   375行并在   /opt/lampp/htdocs/qv3/vendor/magento/module-wishlist/Model/ResourceModel/Item/Collection.php:338   堆栈跟踪:#0   /opt/lampp/htdocs/qv3/app/code/My/Multiwishlist/Model/Wishlist.php(375):   Magento \愿望清单\模型\资源模型\项目\集合-> addWishlistFilter(对象(我的\多重愿望清单\模型\愿望清单))   /opt/lampp/htdocs/qv3/app/code/My/Multiwishlist/Model/Wishlist.php(617):   My \ Multiwishlist \ Model \ Wishlist-> getItemCollection()#2   /opt/lampp/htdocs/qv3/vendor/magento/module-wishlist/Block/AbstractBlock.php(243):   My \ Multiwishlist \ Model \ Wishlist-> getItemsCount()#3   / opt / lampp / htdocs / qv3 / vendor / magento / module-wishlist / Block / AbstractBl   在   /opt/lampp/htdocs/qv3/vendor/magento/module-wishlist/Model/ResourceModel/Item/Collection.php   在第338行

1 个答案:

答案 0 :(得分:0)

如果要在扩展类中注入新的依赖项,则需要调用 parent :: construct 然后传递引用

namespace My\Multiwishlist\Model\Wishlist;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Serialize\Serializer\Json;
use Magento\Wishlist\Model\ResourceModel\Item\CollectionFactory;
use Magento\Wishlist\Model\ResourceModel\Wishlist as ResourceWishlist;
use Magento\Wishlist\Model\ResourceModel\Wishlist\Collection;

class Wishlist extends Magento\Wishlist\Model\Wishlist {
    /**
     * Cache tag
     */
    const CACHE_TAG = 'wishlist';

    /**
     * Prefix of model events names
     *
     * @var string
     */
    protected $_eventPrefix = 'wishlist';

    /**
     * Wishlist item collection
     *
     * @var \Magento\Wishlist\Model\ResourceModel\Item\Collection
     */
    protected $_itemCollection;

    /**
     * Store filter for wishlist
     *
     * @var \Magento\Store\Model\Store
     */
    protected $_store;

    /**
     * Shared store ids (website stores)
     *
     * @var array
     */
    protected $_storeIds;

    /**
     * Wishlist data
     *
     * @var \Magento\Wishlist\Helper\Data
     */
    protected $_wishlistData;

    /**
     * Catalog product
     *
     * @var \Magento\Catalog\Helper\Product
     */
    protected $_catalogProduct;

    /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $_storeManager;

    /**
     * @var \Magento\Framework\Stdlib\DateTime\DateTime
     */
    protected $_date;

    /**
     * @var ItemFactory
     */
    protected $_wishlistItemFactory;

    /**
     * @var CollectionFactory
     */
    protected $_wishlistCollectionFactory;

    /**
     * @var \Magento\Catalog\Model\ProductFactory
     */
    protected $_productFactory;

    /**
     * @var \Magento\Framework\Math\Random
     */
    protected $mathRandom;

    /**
     * @var \Magento\Framework\Stdlib\DateTime
     */
    protected $dateTime;

    /**
     * @var bool
     */
    protected $_useCurrentWebsite;

    /**
     * @var ProductRepositoryInterface
     */
    protected $productRepository;

    /**
     * @var Json
     */
    private $serializer;


    public function __construct(
        \Magento\Framework\Model\Context $context,
        \Magento\Framework\Registry $registry,
        \Magento\Catalog\Helper\Product $catalogProduct,
        \Magento\Wishlist\Helper\Data $wishlistData,
        ResourceWishlist $resource,
        Collection $resourceCollection,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\Stdlib\DateTime\DateTime $date,
        ItemFactory $wishlistItemFactory,
        CollectionFactory $wishlistCollectionFactory,
        \Magento\Catalog\Model\ProductFactory $productFactory,
        \Magento\Framework\Math\Random $mathRandom,
        \Magento\Framework\Stdlib\DateTime $dateTime,
        ProductRepositoryInterface $productRepository,
        $useCurrentWebsite = true,
        /* your injecting class */
        array $data = [],
        Json $serializer = null
    ) {

        parent::__construct($context, $registry, $resource, $resourceCollection, $data, $useCurrentWebsite,$productRepository,$catalogProduct,$wishlistData,$storeManager,$date,$wishlistItemFactory,$wishlistCollectionFactory,$productFactory,$mathRandom,$dateTime,);
        $this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
    /* initialize the parameter */
    }

}