Skip to content

Instantly share code, notes, and snippets.

@nttan
Last active April 4, 2017 10:30
Show Gist options
  • Save nttan/711360bb446dce712d251dffec5832c8 to your computer and use it in GitHub Desktop.
Save nttan/711360bb446dce712d251dffec5832c8 to your computer and use it in GitHub Desktop.
a.php
SELECT `main_table`.* FROM `quote` AS `main_table` WHERE (`is_active` = '1') AND (`items_count` > 0) ORDER BY entity_id DESC
############################################
$this->_logger->info(sprintf("Start cron update Quote - Total: %s", implode(',',$quotes->getColumnValues('entity_id'))));
// echo 'SM\Quote\Model\Observer';die;
// die;
foreach ($quotes as $quote) {
$items = $quote->getAllVisibleItems();
// echo get_class($items);die;
if (sizeof($items) > 0) {
$flagChangeQuote = false;
/** @var \Magento\Quote\Model\Quote\Item $item */
foreach ($items as $item) {
$item = ($item->getParentItem() ? $item->getParentItem() : $item);
try {
$price = $item->getProduct()->getFinalPrice();
if ($item->getPrice() != $price) {
$item->setOriginalCustomPrice($price);
$item->setCustomPrice($price);
$item->getProduct()->setIsSuperMode(true);
/** flag check to need update quote ? */
<?php
namespace SM\Quote\Model;
/**
* Class Observer
* @package SM\Quote\Model
*/
class Observer
{
const XML_PATH_ALLOW_RUN_CRON = 'quote/update_quote_item_price/allow_run';
/**
* Core store config
*
* @var \Magento\Framework\App\Config\ScopeConfigInterface
*/
protected $_scopeConfig;
/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected $_storeManager;
/**
* @var \Magento\Quote\Model\Quote
*/
protected $_quote;
protected $_logger;
/**
* Observer constructor.
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\Quote\Model\Quote $quote
* @param \Psr\Log\LoggerInterface $logger
*/
public function __construct(
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Quote\Model\Quote $quote,
\Psr\Log\LoggerInterface $logger
)
{
$this->_scopeConfig = $scopeConfig;
$this->_storeManager = $storeManager;
$this->_quote = $quote;
$this->_logger = $logger;
$this->_logger->pushHandler( new \Monolog\Handler\StreamHandler( 'var/log/update_quote.log'));
}
public function process()
{
if (!$this->_scopeConfig->getValue(
self::XML_PATH_ALLOW_RUN_CRON,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
)
) {
return $this;
}
$quotes = $this->_quote->getCollection()
->addFieldToFilter('is_active', 1)
->addFieldToFilter('items_count', ['gt' => 0])
->setOrder('entity_id');
$pageSize = 1000;
$totalPage = (int) ceil($quotes->getSize() / $pageSize);
$quotes = $quotes->setPageSize($pageSize);
if ($totalPage) {
/** @var \Magento\Quote\Model\Quote $quote */
$this->_logger->info(sprintf("Start cron update Quote - Total: %s", $quotes->getSize()));
for($currentPage = 1; $currentPage <= $totalPage; $currentPage++){
$quotes->getSelect()->limitPage($currentPage, $pageSize);
foreach ($quotes as $quote) {
$items = $quote->getAllVisibleItems();
if (count($items) > 0) {
$flagChangeQuote = false;
/** @var \Magento\Quote\Model\Quote\Item $item */
foreach ($items as $item) {
try {
$price = $item->getProduct()->getFinalPrice();
if ($item->getPrice() != $price) {
$item->setOriginalCustomPrice($price);
$item->setCustomPrice($price);
$item->getProduct()->setIsSuperMode(true);
/** flag check to need update quote ? */
$flagChangeQuote = true;
}
} catch (\Exception $exception) {
$error = sprintf('Quote ID: %s has error when update %s', $quote->getId(), $exception->getMessage());
$this->_logger->error($error);
}
}
if ($flagChangeQuote) {
$quote->collectTotals()->save();
$this->_logger->info(sprintf("Updated Quote - ID: %s", $quote->getId()));
} else {
$this->_logger->info(sprintf("Quote - ID: %s No need update", $quote->getId()));
}
}
}
}
}
return $this;
}
}
           
#####################################
<?php
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
/** @var SM\Quote\Model\Observer $quoteObserver */
$quoteObserver = $obj->create('SM\Quote\Model\Observer');
$quoteObserver->process();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment