|
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 / dev2.destoffenstraat.com / app / code / SR / Inspiration / Controller / Index / |
Filename | /home/dev2.destoffenstraat.com/app/code/SR/Inspiration/Controller/Index/Index.php |
Size | 2.71 kb |
Permission | rwxrwxrwx |
Owner | root : root |
Create time | 17-Aug-2025 10:26 |
Last modified | 06-Apr-2021 18:06 |
Last accessed | 23-Aug-2025 08:42 |
Actions | edit | rename | delete | download (gzip) |
View | text | code | image |
<?php
namespace SR\Inspiration\Controller\Index;
class Index 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
)
{
$this->_pageFactory = $pageFactory;
$this->customerSession = $customerSession;
$this->_favouriteFactory = $favouriteFactory;
$this->_favouriteCollection = $favouriteCollection;
return parent::__construct($context);
}
public function execute()
{
$pid = $this->getRequest()->getParam('id');
if(!$pid){
$this->messageManager->addError(
__('Item not found.'));
$this->_redirect('couponnenbak');
return;
}
$favourites = $this->customerSession->getFavourites();
if(!is_array($favourites)){
$favourites = array();
}
if(in_array($pid,$favourites)){
// already exist
$this->messageManager->addError(
__('Item is already added in favourite list.'));
$this->_redirect('couponnenbak');
return;
}
$newfavourites = array_merge($favourites,[$pid]);
try{
if($this->customerSession->isLoggedIn()){
$customer = $this->customerSession->getCustomer();
$collection = $this->_favouriteCollection->create();
$collection->addFieldToFilter('customer_id',$customer->getId());
$collection->addFieldToFilter('product_id',$pid);
$recordid = '';
if($collection->count()){
foreach($collection as $item){
$recordid = $item->getId();
break;
}
}
$favouritemodel = $this->_favouriteFactory->create();
if($recordid){
$favouritemodel->load($recordid);
}
$favouritemodel->setCustomerId($customer->getId());
$favouritemodel->setProductId($pid);
$favouritemodel->save();
}
$this->customerSession->setFavourites($newfavourites);
$this->messageManager->addSuccess(
__('Item is added successfully in favourite list.'));
}catch (\Exception $e) {
echo 'Error ='.$e->getMessage();
exit;
$this->messageManager->addError(
__('We can\'t process your request right now. Sorry, that\'s all we know.'));
}
$this->_redirect('couponnenbak');
return;
//return $this->_pageFactory->create();
}
}
namespace SR\Inspiration\Controller\Index;
class Index 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
)
{
$this->_pageFactory = $pageFactory;
$this->customerSession = $customerSession;
$this->_favouriteFactory = $favouriteFactory;
$this->_favouriteCollection = $favouriteCollection;
return parent::__construct($context);
}
public function execute()
{
$pid = $this->getRequest()->getParam('id');
if(!$pid){
$this->messageManager->addError(
__('Item not found.'));
$this->_redirect('couponnenbak');
return;
}
$favourites = $this->customerSession->getFavourites();
if(!is_array($favourites)){
$favourites = array();
}
if(in_array($pid,$favourites)){
// already exist
$this->messageManager->addError(
__('Item is already added in favourite list.'));
$this->_redirect('couponnenbak');
return;
}
$newfavourites = array_merge($favourites,[$pid]);
try{
if($this->customerSession->isLoggedIn()){
$customer = $this->customerSession->getCustomer();
$collection = $this->_favouriteCollection->create();
$collection->addFieldToFilter('customer_id',$customer->getId());
$collection->addFieldToFilter('product_id',$pid);
$recordid = '';
if($collection->count()){
foreach($collection as $item){
$recordid = $item->getId();
break;
}
}
$favouritemodel = $this->_favouriteFactory->create();
if($recordid){
$favouritemodel->load($recordid);
}
$favouritemodel->setCustomerId($customer->getId());
$favouritemodel->setProductId($pid);
$favouritemodel->save();
}
$this->customerSession->setFavourites($newfavourites);
$this->messageManager->addSuccess(
__('Item is added successfully in favourite list.'));
}catch (\Exception $e) {
echo 'Error ='.$e->getMessage();
exit;
$this->messageManager->addError(
__('We can\'t process your request right now. Sorry, that\'s all we know.'));
}
$this->_redirect('couponnenbak');
return;
//return $this->_pageFactory->create();
}
}