繁体   English   中英

如何覆盖magento 2 wishlist模型?

[英]How to override magento 2 wishlist model?

我正在研究模块,我必须覆盖wishlist.php模型,但是当我尝试覆盖它时,我不能给magento错误。 我正在向你提供错误,但你能否告诉我为什么它不会超越?

我想覆盖的模型路径:

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

我面临的错误:

致命错误:未捕获的TypeError:参数1传递给Magento \\ Wishlist \\ Model \\ ResourceModel \\ Item \\ Collection :: addWishlistFilter()必须是Magento \\ Wishlist \\ Model \\ Wishlist的实例,My \\ Multiwishlist \\ Model \\ Wishlist的实例给出,在第375行的/opt/lampp/htdocs/qv3/app/code/My/Multiwishlist/Model/Wishlist.php中调用,并在/ 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 \\ Wishlist \\ Model \\ ResourceModel \\ Item \\ Collection - > addWishlistFilter(Object(My \\ Multiwishlist \\ Model \\ Wishlist))/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):我的\\ Multiwishlist \\ Model \\ Wishlist-> getItemsCount()#3 / opt / lampp / / opt / lampp / htdocs / qv3 / ven中的htdocs / qv3 / vendor / magento / module-wishlist / Block / AbstractBl 第338行的dor / magento / module-wishlist / Model / ResourceModel / Item / Collection.php

如果要在扩展类中注入新的依赖项,则需要调用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 */
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM