Skip to content

Commit

Permalink
add monkey mark in orders grid
Browse files Browse the repository at this point in the history
add controller to retrieve the cart
  • Loading branch information
gonzaloebiz committed Mar 16, 2017
1 parent 9683b0f commit 49a4509
Show file tree
Hide file tree
Showing 15 changed files with 452 additions and 6 deletions.
21 changes: 21 additions & 0 deletions Block/Loadquote.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* Loadquote
*
* @copyright Copyright © 2017 Ebizmarts Corp.. All rights reserved.
* @author info@ebizmarts.com
*/

namespace Ebizmarts\MailChimp\Block;

use Magento\Framework\View\Element\Template;

class Loadquote extends Template
{
/**
* @var string $_template
*/
protected $_template = "loadquote.phtml";

// write your methods here...
}
154 changes: 154 additions & 0 deletions Controller/Cart/Loadquote.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
<?php
/**
* Index
*
* @copyright Copyright © 2017 Ebizmarts Corp.. All rights reserved.
* @author info@ebizmarts.com
*/

namespace Ebizmarts\MailChimp\Controller\Cart;

use Magento\Framework\App\Action\Action;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\App\Action\Context;

class Loadquote extends Action
{
/**
* @var PageFactory
*/
protected $pageFactory;
/**
* @var \Magento\Quote\Model\QuoteFactory
*/
protected $_quote;
/**
* @var \Magento\Customer\Model\Session
*/
protected $_customerSession;
/**
* @var \Ebizmarts\MailChimp\Helper\Data
*/
protected $_helper;
/**
* @var \Magento\Framework\Url
*/
protected $_urlHelper;
/**
* @var \Magento\Framework\Message\ManagerInterface
*/
protected $_message;
/**
* @var \Magento\Customer\Model\Url
*/
protected $_customerUrl;

/**
* Loadquote constructor.
* @param Context $context
* @param PageFactory $pageFactory
* @param \Magento\Quote\Model\QuoteFactory $quote
* @param \Magento\Customer\Model\Session $customerSession
* @param \Ebizmarts\MailChimp\Helper\Data $helper
* @param \Magento\Framework\Url $urlHelper
* @param \Magento\Framework\Message\ManagerInterface $message
* @param \Magento\Customer\Model\Url $customerUrl
*/
public function __construct(
Context $context,
PageFactory $pageFactory,
\Magento\Quote\Model\QuoteFactory $quote,
\Magento\Customer\Model\Session $customerSession,
\Ebizmarts\MailChimp\Helper\Data $helper,
\Magento\Framework\Url $urlHelper,
\Magento\Framework\Message\ManagerInterface $message,
\Magento\Customer\Model\Url $customerUrl
)
{
$this->pageFactory = $pageFactory;
$this->_quote = $quote;
$this->_customerSession = $customerSession;
$this->_helper = $helper;
$this->_urlHelper = $urlHelper;
$this->_message = $message;
$this->_customerUrl = $customerUrl;
parent::__construct($context);
}

/**
* Index Action
*
* @return \Magento\Framework\View\Result\Page
*/
public function execute()
{
/** @var \Magento\Framework\View\Result\Page $resultPage */
$resultPage = $this->pageFactory->create();
$params = $this->getRequest()->getParams();
if(isset($params['id'])) {
$quote = $this->_quote->create();
$quote->getResource()->load($quote,$params['id']);
$magentoStoreId = $quote->getStoreId();
$mailchimpStoreId = $this->_helper->getConfigValue(
\Ebizmarts\MailChimp\Helper\Data::XML_PATH_STORE,
$magentoStoreId
);
$syncCommerce = $this->_helper->getChimpSyncEcommerce(
$mailchimpStoreId,
$params['id'],
\Ebizmarts\MailChimp\Helper\Data::IS_QUOTE
);
if (!isset($params['token']) || $params['token'] != $syncCommerce->getMailchimpToken()) {
// @error
$this->_message->addErrorMessage(__("You can't access this cart"));
$url = $this->_urlHelper->getUrl(
$this->_helper->getConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_ABANDONEDCART_PAGE,
$magentoStoreId
)
);
$this->_redirect($url);
} else {
if (isset($params['mc_cid'])) {
$url = $this->_urlHelper->getUrl(
$this->_helper->getConfigValue(
\Ebizmarts\MailChimp\Helper\Data::XML_ABANDONEDCART_PAGE,
$magentoStoreId,
array('mc_cid'=> $params['mc_cid'])
)
);
}
else {
$url = $this->_urlHelper->getUrl(
$this->_helper->getConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_ABANDONEDCART_PAGE,
$magentoStoreId
)
);
}
$quote->setData('mailchimp_abandonedcart_flag', true);
$quote->getResource()->save($quote);
if (!$quote->getCustomerId()) {
$this->_customerSession->setQuoteId($quote->getId());
$this->_redirect($url);
} else {
if ($this->_customerSession->isLoggedIn()) {
$this->_redirect($url);
} else {
$this->_message->addNoticeMessage(__("Login to complete your order"));
if (isset($params['mc_cid'])) {
$url = $this->_urlHelper->getUrl(
$this->_customerUrl->getLoginUrl(),
array('mc_cid'=>$params['mc_cid'])
);
}
else {
$url = $this->_customerUrl->getLoginUrl();
}
$this->_redirect($url);
}
}
}
}
return $resultPage;
}
}
1 change: 1 addition & 0 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper
const XML_ECOMMERCE_FIRSTDATE = 'mailchimp/ecommerce/firstdate';
const XML_ABANDONEDCART_ACTIVE = 'mailchimp/abandonedcart/active';
const XML_ABANDONEDCART_FIRSTDATE = 'mailchimp/abandonedcart/firstdate';
const XML_ABANDONEDCART_PAGE = 'mailchimp/abandonedcart/page';


const ORDER_STATE_OK = 'complete';
Expand Down
5 changes: 1 addition & 4 deletions Model/Api/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,11 +227,8 @@ protected function _getModifiedQuotes($mailchimpStoreId, $magentoStoreId)
/**
* @var $customer \Magento\Customer\Model\Customer
*/
// $customer = Mage::getModel("customer/customer");
// $customer->setWebsiteId(Mage::getModel('core/store')->load($magentoStoreId)->getWebsiteId());
// $customer->loadByEmail($cart->getCustomerEmail());
$customer = $this->_customerFactory->create();
$customer->setWebsiteId();
$customer->setWebsiteId($magentoStoreId);
$customer->loadByEmail($cart->getCustomerEmail());

if ($customer->getEmail() != $cart->getCustomerEmail()) {
Expand Down
8 changes: 6 additions & 2 deletions Model/Api/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,12 @@ protected function _buildProductData(\Magento\Catalog\Model\Product $product, $
$stock =$this->_stockItemRepository->get($product->getId());
$data["inventory_quantity"] = (int)$stock->getQty();
$data["backorders"] = (string)$stock->getBackorders();

$data["visibility"] = $product->getVisibility();
if ($product->getVisibility() != \Magento\Catalog\Model\Product\Visibility::VISIBILITY_NOT_VISIBLE) {
$data["visibility"] = 'true';
}
else {
$data["visibility"] = false;
}

} else {
//this is for a root product
Expand Down
37 changes: 37 additions & 0 deletions Observer/Sales/Order/SubmitBefore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/**
* mc-magento2 Magento Component
*
* @category Ebizmarts
* @package mc-magento2
* @author Ebizmarts Team <info@ebizmarts.com>
* @copyright Ebizmarts (http://ebizmarts.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @date: 3/14/17 11:26 PM
* @file: SaveBefore.php
*/
namespace Ebizmarts\MailChimp\Observer\Sales\Order;

use Magento\Framework\Event\Observer;

class SubmitBefore implements \Magento\Framework\Event\ObserverInterface
{
private $attributes = [
'mailchimp_abandonedcart_flag'
];
public function execute(\Magento\Framework\Event\Observer $observer)
{
/* @var \Magento\Sales\Model\Order $order */
$order = $observer->getEvent()->getData('order');
/* @var \Magento\Quote\Model\Quote $quote */
$quote = $observer->getEvent()->getData('quote');

foreach ($this->attributes as $attribute) {
if ($quote->hasData($attribute)) {
$order->setData($attribute, $quote->getData($attribute));
}
}

return $this;
}
}
20 changes: 20 additions & 0 deletions Setup/InstallSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,26 @@ public function install(SchemaSetupInterface $setup, ModuleContextInterface $con
]
);

$connection->addColumn(
$installer->getTable('quote'),
'mailchimp_abandonedcart_flag',
[
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_BOOLEAN,
'default' => 0,
'comment' => 'Retrieved from Mailchimp'
]
);

$connection->addColumn(
$installer->getTable('sales_order'),
'mailchimp_abandonedcart_flag',
[
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_BOOLEAN,
'default' => 0,
'comment' => 'Retrieved from Mailchimp'
]
);

$path = $this->_helper->getBaseDir() . DIRECTORY_SEPARATOR . 'var' . DIRECTORY_SEPARATOR . 'Mailchimp';
try {
if (!is_dir($path)) {
Expand Down
102 changes: 102 additions & 0 deletions Ui/Component/Listing/Column/Monkey.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php
/**
* mc-magento2 Magento Component
*
* @category Ebizmarts
* @package mc-magento2
* @author Ebizmarts Team <info@ebizmarts.com>
* @copyright Ebizmarts (http://ebizmarts.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @date: 3/15/17 1:23 AM
* @file: Monkey.php
*/
namespace Ebizmarts\MailChimp\Ui\Component\Listing\Column;

use \Magento\Sales\Api\OrderRepositoryInterface;
use \Magento\Framework\View\Element\UiComponent\ContextInterface;
use \Magento\Framework\View\Element\UiComponentFactory;
use \Magento\Ui\Component\Listing\Columns\Column;
use \Magento\Framework\Api\SearchCriteriaBuilder;
use \Magento\Store\Model\StoreManagerInterface;

class Monkey extends Column
{
/**
* @var OrderRepositoryInterface
*/
protected $_orderRepository;
/**
* @var SearchCriteriaBuilder
*/
protected $_searchCriteria;
/**
* @var \Magento\Framework\View\Asset\Repository
*/
protected $_assetRepository;
/**
* @var \Magento\Framework\App\RequestInterface
*/
protected $_requestInterfase;

/**
* Monkey constructor.
* @param ContextInterface $context
* @param UiComponentFactory $uiComponentFactory
* @param OrderRepositoryInterface $orderRepository
* @param \Magento\Framework\View\Asset\Repository $assetRepository
* @param \Magento\Framework\App\RequestInterface $requestInterface
* @param SearchCriteriaBuilder $criteria
* @param array $components
* @param array $data
*/
public function __construct(
ContextInterface $context,
UiComponentFactory $uiComponentFactory,
OrderRepositoryInterface $orderRepository,
\Magento\Framework\View\Asset\Repository $assetRepository,
\Magento\Framework\App\RequestInterface $requestInterface,
SearchCriteriaBuilder $criteria,
array $components = [],
array $data = []
)
{
$this->_orderRepository = $orderRepository;
$this->_searchCriteria = $criteria;
$this->_assetRepository = $assetRepository;
$this->_requestInterfase= $requestInterface;
parent::__construct($context, $uiComponentFactory, $components, $data);
}

public function prepareDataSource(array $dataSource)
{
if (isset($dataSource['data']['items'])) {
foreach ($dataSource['data']['items'] as & $item) {

$order = $this->_orderRepository->get($item["entity_id"]);
$status = $order->getData("mailchimp_abandonedcart_flag");
$fieldName = $this->getData('name');

switch ($status) {
case "0":
$item[$fieldName . '_src'] = '';
$item[$fieldName . '_alt'] = '';
$item[$fieldName . '_link'] = '';
$item[$fieldName . '_orig_src'] = '';
$item[$fieldName . '_class'] = '';
break;
case "1":
$params = array('_secure' => $this->_requestInterfase->isSecure());
$url = $this->_assetRepository->getUrlWithParams('Ebizmarts_MailChimp::images/logo-freddie-monocolor-200.png', $params);
$item[$fieldName . '_src'] = $url;
$item[$fieldName . '_alt'] = 'hep hep thanks MailChimp';
$item[$fieldName . '_link'] = '';
// $item[$fieldName . '_orig_src'] = $url;
break;

}
}
}

return $dataSource;
}
}
Loading

0 comments on commit 49a4509

Please sign in to comment.