|
Apache/2.4.41 (Ubuntu) Linux vmi616275.contaboserver.net 5.4.0-84-generic #94-Ubuntu SMP Thu Aug 26 20:27:37 UTC 2021 x86_64 uid=33(www-data) gid=33(www-data) groups=33(www-data) server ip : 62.171.164.128 | your ip : 127.0.0.1 safemode OFF > / home / a / home / dev2.destoffenstraat.com / app / code / SR / Inspiration / Controller / Index / |
Filename | /home/a/home/dev2.destoffenstraat.com/app/code/SR/Inspiration/Controller/Index/Addtocart.php |
Size | 2.63 kb |
Permission | rwxrwxrwx |
Owner | root : root |
Create time | 21-Aug-2025 12:26 |
Last modified | 06-Apr-2021 18:06 |
Last accessed | 22-Aug-2025 21:45 |
Actions | edit | rename | delete | download (gzip) |
View | text | code | image |
<?php
namespace SR\Inspiration\Controller\Index;
class Addtocart extends \Magento\Framework\App\Action\Action
{
protected $_pageFactory;
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $pageFactory,
\Magento\Customer\Model\Session $customerSession,
\SR\Inspiration\Model\FavouriteFactory $favouriteFactory,
\SR\Inspiration\Model\ResourceModel\Favourite\CollectionFactory $favouriteCollection,
\Magento\Framework\Data\Form\FormKey $formKey,
\Magento\Checkout\Model\Cart $cart,
\Magento\Catalog\Model\Product $product
)
{
$this->_pageFactory = $pageFactory;
$this->customerSession = $customerSession;
$this->_favouriteFactory = $favouriteFactory;
$this->_favouriteCollection = $favouriteCollection;
$this->formKey = $formKey;
$this->cart = $cart;
$this->product = $product;
return parent::__construct($context);
}
public function execute()
{
$pids = $this->getRequest()->getParam('ids');
if(!$pids){
$this->messageManager->addError(
__('Item not found.'));
$this->_redirect('couponnenbak');
return;
}
$pids = explode(",",$pids);
try{
foreach($pids as $k => $item){
$productId =$item; // Your Product Id
$params = array(
'form_key' => $this->formKey->getFormKey(),
'product' => $productId,
'qty' => 1
);
//Load the product based on productID
$_product = $this->product->load($productId);
$this->cart->addProduct($_product, $params);
$this->cart->save();
}
if($this->customerSession->isLoggedIn()){
$customer = $this->customerSession->getCustomer();
$collection = $this->_favouriteCollection->create();
$collection->addFieldToFilter('customer_id',$customer->getId());
if($collection->count()){
foreach($collection as $k => $item){
if(in_array($item->getProductId(),$pids)){
$item->delete();
}
}
}
}
$favourites = $this->customerSession->getFavourites();
$newarray = array_diff($favourites,$pids);
$this->customerSession->setFavourites($newarray);
//$this->customerSession->setFavourites($newfavourites);
$this->messageManager->addSuccess(
__('Item is added successfully in favourite list.'));
}catch (\Exception $e) {
echo $e->getMessage();
$this->messageManager->addError(
__($e->getMessage()));
}
$this->_redirect('couponnenbak');
return;
//return $this->_pageFactory->create();
}
}
namespace SR\Inspiration\Controller\Index;
class Addtocart extends \Magento\Framework\App\Action\Action
{
protected $_pageFactory;
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $pageFactory,
\Magento\Customer\Model\Session $customerSession,
\SR\Inspiration\Model\FavouriteFactory $favouriteFactory,
\SR\Inspiration\Model\ResourceModel\Favourite\CollectionFactory $favouriteCollection,
\Magento\Framework\Data\Form\FormKey $formKey,
\Magento\Checkout\Model\Cart $cart,
\Magento\Catalog\Model\Product $product
)
{
$this->_pageFactory = $pageFactory;
$this->customerSession = $customerSession;
$this->_favouriteFactory = $favouriteFactory;
$this->_favouriteCollection = $favouriteCollection;
$this->formKey = $formKey;
$this->cart = $cart;
$this->product = $product;
return parent::__construct($context);
}
public function execute()
{
$pids = $this->getRequest()->getParam('ids');
if(!$pids){
$this->messageManager->addError(
__('Item not found.'));
$this->_redirect('couponnenbak');
return;
}
$pids = explode(",",$pids);
try{
foreach($pids as $k => $item){
$productId =$item; // Your Product Id
$params = array(
'form_key' => $this->formKey->getFormKey(),
'product' => $productId,
'qty' => 1
);
//Load the product based on productID
$_product = $this->product->load($productId);
$this->cart->addProduct($_product, $params);
$this->cart->save();
}
if($this->customerSession->isLoggedIn()){
$customer = $this->customerSession->getCustomer();
$collection = $this->_favouriteCollection->create();
$collection->addFieldToFilter('customer_id',$customer->getId());
if($collection->count()){
foreach($collection as $k => $item){
if(in_array($item->getProductId(),$pids)){
$item->delete();
}
}
}
}
$favourites = $this->customerSession->getFavourites();
$newarray = array_diff($favourites,$pids);
$this->customerSession->setFavourites($newarray);
//$this->customerSession->setFavourites($newfavourites);
$this->messageManager->addSuccess(
__('Item is added successfully in favourite list.'));
}catch (\Exception $e) {
echo $e->getMessage();
$this->messageManager->addError(
__($e->getMessage()));
}
$this->_redirect('couponnenbak');
return;
//return $this->_pageFactory->create();
}
}