|
| 1 | +<?php |
| 2 | + |
| 3 | +ini_set('display_startup_errors', 1);ini_set('display_errors', 1); error_reporting(-1); |
| 4 | +//// Setup Base |
| 5 | +$folder = ''; //Folder Name |
| 6 | +$file = $folder ? "$folder/app/bootstrap.php" : "app/bootstrap.php"; |
| 7 | + |
| 8 | +if(!file_exists ($file)) $file = "app/bootstrap.php"; |
| 9 | +if(file_exists ($file)){ |
| 10 | + require dirname(__FILE__) .'/' .$file; |
| 11 | + $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER); |
| 12 | +} else {die('Not found bootstrap.php');} |
| 13 | + |
| 14 | +class upToDate extends \Magento\Framework\App\Http |
| 15 | + implements \Magento\Framework\AppInterface { |
| 16 | + |
| 17 | + protected $todayEndOfDayDate; |
| 18 | + protected $todayStartOfDayDate; |
| 19 | + protected $setTime = '2021-06-20 00:00:00'; |
| 20 | + |
| 21 | + public function launch() |
| 22 | + { |
| 23 | + |
| 24 | + $setTime = $this->setTime; |
| 25 | + $dt = new DateTime(); |
| 26 | + // $dt = new DateTime('2017-01-01'); |
| 27 | + $this->todayStartOfDayDate = $dt->setTime(0, 0, 0)->format('Y-m-d H:i:s'); |
| 28 | + $this->todayEndOfDayDate = $dt->setTime(23, 59, 59)->format('Y-m-d H:i:s'); |
| 29 | + $discount=0.9; |
| 30 | + $attributeSet = 'Service'; |
| 31 | + $attributeSetId = 9; |
| 32 | + // echo $this->todayStartOfDayDate; |
| 33 | + // echo '<br/>'; |
| 34 | + // echo $this->todayEndOfDayDate; |
| 35 | + // echo '<br/>'; |
| 36 | + // die; |
| 37 | + |
| 38 | + $this->_state->setAreaCode('adminhtml'); |
| 39 | + // $_storeManager = $this->_objectManager->create('\Magento\Store\Model\StoreManagerInterface'); |
| 40 | + // $_storeManager->setCurrentStore(0); |
| 41 | + $model = $this->_objectManager->get('Magento\Catalog\Model\Product\Action'); |
| 42 | + // $productRepository = $this->_objectManager->create('Magento\Catalog\Model\ProductRepository'); |
| 43 | + // $attributeSetRepository = $this->_objectManager->get('Magento\Eav\Api\AttributeSetRepositoryInterface'); |
| 44 | + // $attributeSetCollection = $this->_objectManager->get('\Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory'); |
| 45 | + $newProducts = $this->getNewProducts(); |
| 46 | + $num = 0; |
| 47 | + if( count($newProducts)){ |
| 48 | + foreach ($newProducts as $product) { |
| 49 | + // $product->setStoreId(0)->setData('news_to_date', $setTime)->save(); |
| 50 | + // $model->updateAttributes([$product->getId()], ['news_to_date' => $setTime], 0); |
| 51 | + $num++; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + echo "$num New products changed !<br/>"; |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | + $saleProducts = $this->getSaleProducts(); |
| 60 | + $num = 0; |
| 61 | + foreach ($saleProducts as $product) { |
| 62 | + // echo $product->getName(); |
| 63 | + if( $product->getAttributeSetId() != $attributeSetId) continue; |
| 64 | + echo $product->getName(); |
| 65 | + // $product->setStoreId(0)->setData('special_to_date', $setTime)->save(); |
| 66 | + $price = (int) $product->getData('price'); |
| 67 | + $specialPrice = (int) ($price*$discount); |
| 68 | + echo $specialPrice; |
| 69 | + $model->updateAttributes([$product->getId()], ['special_to_date' => $setTime], 0); |
| 70 | + $model->updateAttributes([$product->getId()], ['special_price' => $specialPrice], 0); |
| 71 | + $num++; |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | + echo "$num Sale products changed !<br/>"; |
| 76 | + |
| 77 | + return $this->_response; |
| 78 | + } |
| 79 | + |
| 80 | + public function getNewProducts() { |
| 81 | + |
| 82 | + // $todayStartOfDayDate = $this->_localeDate->date()->setTime(0, 0, 0)->format('Y-m-d H:i:s'); |
| 83 | + // $todayEndOfDayDate = $this->_localeDate->date()->setTime(23, 59, 59)->format('Y-m-d H:i:s'); |
| 84 | + $todayEndOfDayDate = $this->todayEndOfDayDate; |
| 85 | + $todayStartOfDayDate = $this->todayStartOfDayDate; |
| 86 | + $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); |
| 87 | + /** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $manager */ |
| 88 | + $manager = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory'); |
| 89 | + $collection = $manager->create(); |
| 90 | + |
| 91 | + $collection->addStoreFilter() |
| 92 | + ->addAttributeToFilter( |
| 93 | + 'news_from_date', |
| 94 | + [ |
| 95 | + 'or' => [ |
| 96 | + 0 => ['date' => true, 'to' => $todayEndOfDayDate], |
| 97 | + 1 => ['is' => new \Zend_Db_Expr('null')], |
| 98 | + ] |
| 99 | + ], |
| 100 | + 'left' |
| 101 | + ) |
| 102 | + // ->addAttributeToFilter( |
| 103 | + // 'news_to_date', |
| 104 | + // [ |
| 105 | + // 'or' => [ |
| 106 | + // 0 => ['date' => true, 'from' => $todayStartOfDayDate], |
| 107 | + // 1 => ['is' => new \Zend_Db_Expr('null')], |
| 108 | + // ] |
| 109 | + // ], |
| 110 | + // 'left' |
| 111 | + // ) |
| 112 | + ->addAttributeToFilter( |
| 113 | + [ |
| 114 | + ['attribute' => 'news_from_date', 'is' => new \Zend_Db_Expr('not null')], |
| 115 | + ['attribute' => 'news_to_date', 'is' => new \Zend_Db_Expr('not null')], |
| 116 | + ] |
| 117 | + )->addAttributeToSort('news_from_date', 'desc'); |
| 118 | + |
| 119 | + return $collection; |
| 120 | + } |
| 121 | + |
| 122 | + public function getSaleProducts(){ |
| 123 | + |
| 124 | + // $todayStartOfDayDate = $this->_localeDate->date()->setTime(0, 0, 0)->format('Y-m-d H:i:s'); |
| 125 | + // $todayEndOfDayDate = $this->_localeDate->date()->setTime(23, 59, 59)->format('Y-m-d H:i:s'); |
| 126 | + $todayEndOfDayDate = $this->todayEndOfDayDate; |
| 127 | + $todayStartOfDayDate = $this->todayStartOfDayDate; |
| 128 | + $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); |
| 129 | + /** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $manager */ |
| 130 | + $manager = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory'); |
| 131 | + $collection = $manager->create(); |
| 132 | + |
| 133 | + $collection->addStoreFilter() |
| 134 | + ->addAttributeToSelect('*') |
| 135 | + // ->addAttributeToFilter( |
| 136 | + // 'special_from_date', |
| 137 | + // [ |
| 138 | + // 'or' => [ |
| 139 | + // 0 => ['date' => true, 'to' => $todayEndOfDayDate], |
| 140 | + // 1 => ['is' => new \Zend_Db_Expr('null')], |
| 141 | + // ] |
| 142 | + // ], |
| 143 | + // 'left' |
| 144 | + // ) |
| 145 | + // // ->addAttributeToFilter( |
| 146 | + // // 'special_to_date', |
| 147 | + // // [ |
| 148 | + // // 'or' => [ |
| 149 | + // // 0 => ['date' => true, 'from' => $todayStartOfDayDate], |
| 150 | + // // 1 => ['is' => new \Zend_Db_Expr('null')], |
| 151 | + // // ] |
| 152 | + // // ], |
| 153 | + // // 'left' |
| 154 | + // // ) |
| 155 | + // ->addAttributeToFilter( |
| 156 | + // [ |
| 157 | + // ['attribute' => 'special_from_date', 'is' => new \Zend_Db_Expr('not null')], |
| 158 | + // ['attribute' => 'special_to_date', 'is' => new \Zend_Db_Expr('not null')], |
| 159 | + // ] |
| 160 | + // ) |
| 161 | + ->addAttributeToSort('special_to_date', 'desc'); |
| 162 | + |
| 163 | + return $collection; |
| 164 | + |
| 165 | + } |
| 166 | + |
| 167 | +} |
| 168 | + |
| 169 | +/** @var \Magento\Framework\App\Http $app */ |
| 170 | +$app = $bootstrap->createApplication('upToDate'); |
| 171 | +$bootstrap->run($app); |
0 commit comments