0

i am trying to add item to wishlist to a customer using web rest api ,

i am facing a strange error

 INNER JOIN `catalog_category_product_index_store0` AS `cat_index` ON cat_index.product_id = main_table.product_id AND cat_index.category_id = '0' AND cat_index.visibility IN (3, 2, 4) WHERE (`wishlist_id` = '1') AND (`main_table`.`store_id` IN('0')) at /opt/lampp/htdocs/magento2/lib/internal/Magento/Framework/DB/Statement/Pdo/Mysql.php:110, PDOException(code: 42S02): SQLSTATE[42S02]: Base table or view not found: 1146 Table 'magento2.catalog_category_product_index_store0' doesn't exist at /opt/lampp/htdocs/magento2/lib/internal/Magento/Framework/DB/Statement/Pdo/Mysql.php:91)"} []

here is my code ,

<?php
/**
 * A Magento 2 module named YourCompany/WishlistWebService
 *
 */
namespace YourCompany\WishlistWebService\Model;

use YourCompany\WishlistWebService\Api\WishlistManagementInterface;
use Magento\Wishlist\Controller\WishlistProvider;
use Magento\Wishlist\Model\ResourceModel\Item\CollectionFactory;
use Magento\Wishlist\Model\WishlistFactory;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Wishlist\Model\ResourceModel\Item as ItemResource;
use Magento\Framework\Exception\LocalizedException;
use Magento\Catalog\Helper\ImageFactory as ProductImageHelper;
use Magento\Store\Model\App\Emulation as AppEmulation;
/**
 * Defines the implementaiton class of the WishlistManagementInterface
 */
class WishlistManagement implements WishlistManagementInterface
{

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

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

        /**
     * @var ItemResource
     */
    private $itemResource;
    /**
     * @var WishlistRepository
     */
    protected $_wishlistRepository;
    /**
     * @var ProductRepository
     */
    protected $productRepository;

    /**
     * @var WishlistFactory
     */
    protected $_wishlistFactory;

    /**
     * @var Item
     */
    protected $_itemFactory;

    /**
     * @var \Magento\Customer\Model\Customer
     */
    protected $_customer;

    /**
     *@var \Magento\Catalog\Helper\ImageFactory
     */
    protected $productImageHelper;

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

    /**
     *@var \Magento\Store\Model\App\Emulation
     */
    protected $appEmulation;

    /**
     *@var \Magento\Catalog\Model\Product
     */
    protected $_productload;
    protected $_wishlistObj;
    /**
     *@var \Magento\Directory\Model\CountryFactory
     */
    protected $countryfactory;
    protected $_productloader;
    /**
     * @param CollectionFactory $wishlistCollectionFactory
     * @param \Magento\Catalog\Model\ProductFactory $productFactory
     * @param \Magento\Framework\Math\Random $mathRandom
     * @param \Magento\Customer\Model\Customer $customer
     * @param \Magento\Framework\Stdlib\DateTime $dateTime
     * @param ItemResource $itemResource
     * @param ProductRepositoryInterface $productRepository
     */
    public function __construct(
        \Magento\Wishlist\Model\WishlistFactory $wishlistObj,
        CollectionFactory $wishlistCollectionFactory,
        WishlistFactory $wishlistFactory,
        \Magento\Customer\Model\Customer $customer,
        AppEmulation $appEmulation,
        ItemResource $itemResource,
        \Magento\Directory\Model\CountryFactory $countryfactory,
        \Magento\Store\Model\StoreManagerInterface $storemanagerinterface,
        ProductImageHelper $productImageHelper,
        \Magento\Catalog\Model\Product $productload,
        \Magento\Wishlist\Model\WishlistFactory $wishlistRepository,
        ProductRepositoryInterface $productRepository,
        \Magento\Wishlist\Model\ItemFactory $itemFactory,
        \Magento\Catalog\Model\ProductFactory $_productloader,
        \Magento\Wishlist\Model\ResourceModel\Item\CollectionFactory $_wishlistCollectionFactory
    ) {
        $this->productRepository = $productRepository;
        $this->_wishlistFactory = $wishlistFactory;
        $this->_wishlistObj = $wishlistObj;
        $this->countryfactory = $countryfactory;
        $this->storemanagerinterface = $storemanagerinterface;
        $this->_itemFactory = $itemFactory;
        $this->_customer = $customer;
        $this->_productload = $productload;
        $this->appEmulation = $appEmulation;
        $this->productImageHelper = $productImageHelper;
        $this->_customer = $customer;
        $this->itemResource = $itemResource;
        $this->_productloader = $_productloader;
        $this->_wishlistCollectionFactory = $_wishlistCollectionFactory;
        $this->_wishlistRepository= $wishlistRepository;
    }

    /**
     * Get wishlist collection
     * @deprecated
     * @param $customerId
     * @return string
     */
    public function getWishlistForCustomer($customerId)
    {
       
        if (empty($customerId) || !isset($customerId) || $customerId == "") {
            throw new InputException(__('Id required'));
        } else {
                
            $collection =
            $this->_wishlistCollectionFactory->create()
            ->addCustomerIdFilter($customerId)->getData();
            
            $baseurl = $this->storemanagerinterface->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA).'catalog/product';
           
            $wishlistData = [];
            foreach ($collection as $item) {
                $productInfo = $this->_productloader->create()->load($item['product_id']);
                $imageURL = $this->getImageUrl($productInfo, 'product_base_image');
                if ($productInfo['small_image'] == 'no_selection') {
                    $currentproduct = $this->_productload->load($productInfo['entity_id']);
                    $imageURL = $this->getImageUrl($productInfo, 'product_base_image');
                    $productInfo['small_image'] = $imageURL;
                    $productInfo['thumbnail'] = $imageURL;
                }else{
                    $imageURL = $baseurl.$productInfo['small_image'];
                    $productInfo['small_image'] = $imageURL;
                    $productInfo['thumbnail'] = $imageURL;
                }
                $data = [
                    "wishlist_item_id" => $item['wishlist_item_id'],
                    "wishlist_id"      => $item['wishlist_id'],
                    "product_id"       => $item['product_id'],
                    "store_id"         => $item['store_id'],
                    "added_at"         => $item['added_at'],
                    "description"      => $item['description'],
                    "qty"              => round($item['qty']),
                ];

                $wishlistData[] = $data;
            }
            return $wishlistData;
        }
    }
    /**
     * Add wishlist item for the customer
     * @param int $customerId
     * @param int $productId
     * @return bool
     * @throws \Magento\Framework\Exception\LocalizedException
     */
    public function addWishlistForCustomer($customerId, $productId)
    {
        if ($productId == null) {
            throw new LocalizedException(__
            ('Invalid product, Please select a valid product'));
        }
        try {
            $product = $this->_productloader->create()->load($productId);
        } catch (NoSuchEntityException $e) {
            $product = null;
        }
        try {
          
          $wishlist = $this->_wishlistObj->create()->loadByCustomerId($customerId, true);
            $wishlist->addNewItem($product);
            $wishlist->save();
            return $wishlist->getData();
        } catch (NoSuchEntityException $e) {

        }
        return true;
    }

    /**
     * Helper function that provides full cache image url
     * @param \Magento\Catalog\Model\Product
     * @return string
     */
    public function getImageUrl($product, string $imageType = ''){
        $storeId = $this->storemanagerinterface->getStore()->getId();
        $this->appEmulation->startEnvironmentEmulation($storeId, \Magento\Framework\App\Area::AREA_FRONTEND, true);
        $imageUrl = $this->productImageHelper->create()->init($product, $imageType)->getUrl();
        $this->appEmulation->stopEnvironmentEmulation();

        return $imageUrl;
    }
    public function removeItem($customerId, $productId)
    {
        $wishlist = $this->_wishlistObj->create();
        $wishlist->loadByCustomerId($customerId);

        if (!$wishlist->getId()) {
            $wishlist->setCustomerId($customerId);
            $wishlist->getResource()->save($wishlist);
        }

        
        $item = $wishlist->getItem($productId);
        if (!$item) {
            return false;
        }

        $this->itemResource->delete($item);

        return true;
    }
}

Please let me know what is wrong with it , thank you feel free to share opinions

Your Answer

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

Browse other questions tagged or ask your own question.