1

TypeError: Mstore\ProductsList\Plugin\TotalsPlugin::afterGet(): Argument #2 ($result) must be of type Mstore\ProductsList\Plugin\Magento\Quote\Api\Data\TotalsInterface, Magento\Quote\Model\Quote\Interceptor given, called in /var/www/html/vendor/magento/framework/Interception/Interceptor.php on line 146 and defined in /var/www/html/app/code/Mstore/ProductsList/Plugin/TotalsPlugin.php:76

<?php namespace Mstore\ProductsList\Plugin; use Magento\Catalog\Model\Product; use Magento\Catalog\Model\ProductFactory; use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Framework\Stdlib\DateTime\TimezoneInterface; use Magento\Quote\Api\Data\TotalsInterface; use Magento\Quote\Api\Data\TotalsItemExtensionInterfaceFactory; use Magento\Quote\Api\Data\TotalsItemInterface; use Magento\Quote\Api\GuestCartRepositoryInterface; use Magento\Quote\Model\Quote\ItemFactory; use Magento\Store\Model\ScopeInterface; class TotalsPlugin { /** * @var TotalsItemExtensionInterfaceFactory */ protected $totalsItemExtensionInterfaceFactory; /** * @var ItemFactory */ private $itemFactory; /** * @var ProductFactory */ private $productFactory; /** * @var ScopeConfigInterface */ private $scopeConfig; /** * @var TimezoneInterface */ private $date; /** * @var mixed */ private mixed $discountPercent; /** * TotalsPlugin constructor. * * @param TotalsItemExtensionInterfaceFactory $totalsItemExtensionInterfaceFactory * @param ItemFactory $itemFactory * @param ProductFactory $productFactory * @param ScopeConfigInterface $scopeConfig * @param TimezoneInterface $date */ public function __construct( TotalsItemExtensionInterfaceFactory $totalsItemExtensionInterfaceFactory, ItemFactory $itemFactory, ProductFactory $productFactory, ScopeConfigInterface $scopeConfig, \Magento\Catalog\Model\ProductRepository $productRepository, TimezoneInterface $date ) { $this->totalsItemExtensionInterfaceFactory = $totalsItemExtensionInterfaceFactory; $this->itemFactory = $itemFactory; $this->productFactory = $productFactory; $this->scopeConfig = $scopeConfig; $this->_productRepository = $productRepository; $this->date = $date; } /** * After get items. * * @param GuestCartTotalRepositoryInterface $subject * @param TotalsInterface $result * @return TotalsInterface */ public function afterGet( GuestCartRepositoryInterface $subject, TotalsInterface $result ) { /** @var TotalsItemInterface $item */ foreach ($result->getItems() as $item) { $id = $item->getItemId(); $product = $this->getProductById($id); $sku = $_product->getSku(); $image = $product ? $product->getImage() : ""; $specialPrice = $product->getSpecialPrice() ?? 0; $specialFromDate = $product->getSpecialFromDate() ?? ""; $specialToDate = $product->getSpecialToDate() ?? ""; $originalPrice = $product->getPrice() ?? 0; $brandName = $product->getTestingBrandName() ?? ""; $productLinks = $product->getProductLinks()??[]; $extensionAttributes = $item->getExtensionAttributes(); if (!$extensionAttributes) { $extensionAttributes = $this->totalsItemExtensionInterfaceFactory->create(); } $extensionAttributes->setProductSku($sku); $extensionAttributes->setProductImage($image); $extensionAttributes->setOriginalPrice($originalPrice); $extensionAttributes->setSpecialPrice($specialPrice); $extensionAttributes->setSpecialPriceFrom($specialFromDate); $extensionAttributes->setSpecialPriceTo($specialToDate); $extensionAttributes->setTestingBrandName($brandName); $extensionAttributes->setProductLinks($productLinks); $item->setExtensionAttributes($extensionAttributes); } return $result; } public function getProductById($id) { return $this->_productRepository->getById($id); } } <type name="Magento\Quote\Api\GuestCartRepositoryInterface"> <plugin name="product_skus" type="Mstore\ProductsList\Plugin\TotalsPlugin"/> </type> 
2
  • Add your whole code for plugin and di.xml Commented Mar 22, 2023 at 6:59
  • updated please check Commented Mar 22, 2023 at 7:04

1 Answer 1

1

You can try below code: First check, if simple log is working for you.

<?php namespace Mstore\ProductsList\Plugin; use Magento\Quote\Api\Data\CartInterface; use Magento\Quote\Api\GuestCartRepositoryInterface; class TotalsPlugin { /** * After get items. * * @param GuestCartTotalRepositoryInterface $subject * @param CartInterface $result * @param string $cartId * @return CartInterface */ public function afterGet(GuestCartRepositoryInterface $subject, CartInterface $result, $cartId):CartInterface { // Add log here to test, if it is working return $result; } } 
2
  • Error: Call to undefined method Magento\Quote\Api\Data\CartItemExtension::setProductSku() in /var/www/html/app/code/Mstore/ProductsList/Plugin/TotalsPlugin.php:99 Commented Mar 22, 2023 at 7:26
  • is it working now ? Commented Mar 22, 2023 at 12:19

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.