Main.php 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace Smartwave\Dailydeals\Block;
  3. //use Magento\Framework\View\Element\Template;
  4. use Magento\Catalog\Api\CategoryRepositoryInterface;
  5. class Main extends \Magento\Catalog\Block\Product\ListProduct
  6. {
  7. protected $productFactory;
  8. protected $dailydealFactory;
  9. protected $scopeConfig;
  10. public function __construct(
  11. \Magento\Catalog\Block\Product\Context $context,
  12. \Magento\Catalog\Model\ProductFactory $productFactory,
  13. \Magento\Framework\Data\Helper\PostHelper $postDataHelper,
  14. \Magento\Catalog\Model\Layer\Resolver $layerResolver,
  15. CategoryRepositoryInterface $categoryRepository,
  16. \Magento\Framework\Url\Helper\Data $urlHelper,
  17. \Smartwave\Dailydeals\Model\DailydealFactory $dailydealFactory,
  18. array $data = []
  19. ) {
  20. $this->productFactory= $productFactory;
  21. $this->dailydealFactory=$dailydealFactory;
  22. $this->scopeConfig=$context->getScopeConfig();
  23. return parent::__construct($context, $postDataHelper, $layerResolver, $categoryRepository, $urlHelper, $data);
  24. }
  25. // @return Productcollection whose status is enabled
  26. public function getDailydealEnableProduct()
  27. {
  28. $collection=$this->getDailydealCollection();
  29. $collection->addFieldToSelect('*');
  30. $collection->addFieldToFilter('sw_deal_enable', ['eq' => 1]);
  31. return $collection;
  32. }
  33. public function getDailydealCollection()
  34. {
  35. $collection=$this->dailydealFactory->create()->getCollection();
  36. return $collection;
  37. }
  38. // Get Product Data which is common in DailydealCollection
  39. public function getDailyDealProduct($productSku)
  40. {
  41. $productCollection=$this->productFactory->create()->getCollection();
  42. $productCollection->addAttributeToSelect('*');
  43. $productCollection->addAttributeToFilter('sku', ['eq'=>$productSku]);
  44. return $productCollection;
  45. }
  46. //Retrun Recently dailydeal offer Collection ( duration is 2 days before expired and 2 days ago comming soon offer)
  47. public function recentlyDailydeal($productSku)
  48. {
  49. $dailydealcollection=$this->getDailydealCollection();
  50. $dailydealcollection->addFieldToSelect('*');
  51. $dailydealcollection->addFieldToFilter('sw_product_sku', ['eq'=>$productSku]);
  52. if ($dailydealcollection->getSize() ==1) {
  53. $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
  54. $objDate = $objectManager->create('Magento\Framework\Stdlib\DateTime\DateTime');
  55. $curdate=strtotime($objDate->gmtDate("Y-m-d H:i:s"));
  56. $Todate=strtotime($dailydealcollection->getFirstItem()->getSwDateTo());
  57. $fromdate=strtotime($dailydealcollection->getFirstItem()->getSwDateFrom());
  58. // calculate two days time
  59. $twodays_duration=172800;
  60. $expiredduration=$curdate-$Todate; // It returns positive value
  61. $comingsoonduration=$curdate-$fromdate; // It returns Nagative value
  62. // Check datetime duration before two days and ago twodays
  63. if ($expiredduration > $twodays_duration || $comingsoonduration < -$twodays_duration) {
  64. return false;
  65. } else {
  66. return true; // Return True if collection of product which are expired and comming Duration is two days
  67. }
  68. } else {
  69. return false;
  70. }
  71. }
  72. }