Data.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. namespace Smartwave\Dailydeals\Helper;
  3. class Data extends \Magento\Framework\App\Helper\AbstractHelper
  4. {
  5. protected $dailydealFactory;
  6. protected $scopeConfig;
  7. protected $productFactory;
  8. protected $loadedTimer;
  9. public function __construct(
  10. \Smartwave\Dailydeals\Model\DailydealFactory $dailydealFactory,
  11. \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
  12. \Magento\Catalog\Model\ProductFactory $productFactory
  13. ) {
  14. $this->dailydealFactory = $dailydealFactory;
  15. $this->scopeConfig=$scopeConfig;
  16. $this->productFactory= $productFactory;
  17. $this->loadedTimer = 0;
  18. }
  19. public function chkEnableDailydeals()
  20. {
  21. $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
  22. $configPath = "sw_dailydeal/general/dailydeal_enabled";
  23. $chkEnableDailydeals = $this->scopeConfig->getValue($configPath, $storeScope);
  24. return $chkEnableDailydeals;
  25. }
  26. // Get ObjectManager Instance
  27. public function getObjectManagerInstance()
  28. {
  29. $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
  30. return $objectManager;
  31. }
  32. //Check Dailydeal Product
  33. public function isDealProduct($productId)
  34. {
  35. if(!$this->chkEnableDailydeals())
  36. return false;
  37. $productcollection=$this->productFactory->create()->getCollection();
  38. $productcollection->addAttributeToSelect('*');
  39. $productcollection->addAttributeToFilter('entity_id', ['eq'=>$productId]);
  40. $sku=$productcollection->getFirstItem()->getSku();
  41. $dailydealcollection=$this->getDailydealcollection();
  42. $dailydealcollection->addFieldToSelect('*');
  43. $dailydealcollection->addFieldToFilter('sw_product_sku', ['eq'=>$sku]);
  44. if ($dailydealcollection->getSize() ==1) {
  45. $objDate = $this->getObjectManagerInstance()->create('Magento\Framework\Stdlib\DateTime\DateTime');
  46. $curdate=strtotime($this->getcurrentDate());
  47. $Todate=strtotime($this->getDailydealToDate($sku));
  48. $fromdate=strtotime($this->getDailydealFromDate($sku));
  49. if (( $curdate <= $Todate ) && ($curdate >= $fromdate)) {
  50. return true;
  51. } else {
  52. return false;
  53. }
  54. } else {
  55. return false;
  56. }
  57. }
  58. // Get Product Price
  59. public function getProductPrice($sku)
  60. {
  61. $productcollection=$this->productFactory->create()->getCollection();
  62. $productcollection->addAttributeToSelect('*');
  63. $productcollection->addAttributeToFilter('sku', ['eq'=>$sku]);
  64. $productcollection->addAttributeToFilter('type_id', ['neq'=>'bundle']);
  65. if ($productcollection->getSize() ==1 && $productcollection->getFirstItem()->getTypeId() !="grouped") {
  66. return $productcollection->getFirstItem()->getFinalPrice();
  67. } else {
  68. return 1;
  69. }
  70. }
  71. // Get Bundle Discount Value
  72. public function getbundleProductDiscount($sku)
  73. {
  74. $dailydealcollection=$this->getDailydealcollection();
  75. $dailydealcollection->addFieldToSelect('*');
  76. $dailydealcollection->addFieldToFilter('sw_product_sku', ['eq'=>$sku]);
  77. if ($dailydealcollection->getFirstItem()->getSwDiscountType() ==1) {
  78. return '<div style=" margin-top:20px; "><strong>Save:'.$this->getcurrencySymbol().''.number_format($dailydealcollection->getFirstItem()->getSwDiscountAmount(), 2).'</strong></div>';
  79. } elseif ($dailydealcollection->getFirstItem()->getSwDiscountType() ==2) {
  80. return '<div style="margin-top:20px;"><strong>OFF:'.number_format($dailydealcollection->getFirstItem()->getSwDiscountAmount(), 2).'%</strong></div>';
  81. }
  82. }
  83. //Get "Product price" by ProductId
  84. public function getDealproductbyId($productId)
  85. {
  86. $productcollection=$this->productFactory->create()->getCollection();
  87. $productcollection->addAttributeToSelect('*');
  88. $productcollection->addAttributeToFilter('entity_id', ['eq'=>$productId]);
  89. $sku=$productcollection->getFirstItem()->getSku();
  90. return $this->getDealProductPrice($sku);
  91. }
  92. // Get Current Currency Symbol
  93. public function getcurrencySymbol()
  94. {
  95. $currencySymbol=$this->getObjectManagerInstance()->create('Magento\Store\Model\StoreManagerInterface');
  96. return $currencySymbol->getStore()->getCurrentCurrency()->getCurrencySymbol();
  97. }
  98. // Get Current Date
  99. public function getcurrentDate()
  100. {
  101. $objDate = $this->getObjectManagerInstance()->create('Magento\Framework\Stdlib\DateTime\DateTime');
  102. return $objDate->gmtDate("Y-m-d H:i:s");
  103. }
  104. // Get Collection of dailydeal
  105. public function getDailydealcollection()
  106. {
  107. $dailydealcollection=$this->dailydealFactory->create()->getCollection();
  108. return $dailydealcollection;
  109. }
  110. // Get Discount Value of Dailydeal Product
  111. public function getDealProductDiscountValue($dealproductsku)
  112. {
  113. $dailydealcollection=$this->getDailydealcollection();
  114. $dailydealcollection->addFieldToSelect('*');
  115. $dailydealcollection->addFieldToFilter('sw_product_sku', ['eq'=>$dealproductsku]);
  116. return $dailydealcollection->getFirstItem()->getSwDiscountAmount();
  117. }
  118. // Get Dailydeal Product with Discount Price
  119. public function getDealProductPrice($dealproductsku)
  120. {
  121. $dailydealcollection=$this->getDailydealcollection();
  122. $dailydealcollection->addFieldToSelect('*');
  123. $dailydealcollection->addFieldToFilter('sw_product_sku', ['eq'=>$dealproductsku]);
  124. return $dailydealcollection->getFirstItem()->getSwProductPrice();
  125. }
  126. // Get Dailydeal Product TO date
  127. public function getDailydealToDate($dealproductsku)
  128. {
  129. $dailydealcollection=$this->getDailydealcollection();
  130. $dailydealcollection->addFieldToSelect('*');
  131. $dailydealcollection->addFieldToFilter('sw_product_sku', ['eq'=>$dealproductsku]);
  132. return $dailydealcollection->getFirstItem()->getSwDateTo();
  133. }
  134. // Get Dailydeal Product FROM Date
  135. public function getDailydealFromDate($dealproductsku)
  136. {
  137. $dailydealcollection=$this->getDailydealcollection();
  138. $dailydealcollection->addFieldToSelect('*');
  139. $dailydealcollection->addFieldToFilter('sw_product_sku', ['eq'=>$dealproductsku]);
  140. return $dailydealcollection->getFirstItem()->getSwDateFrom();
  141. }
  142. // Get "OFF value" (in percentage) of Dailydeal Product
  143. public function getDealOffValue($dealproductsku)
  144. {
  145. $dailydealcollection=$this->getDailydealcollection();
  146. $dailydealcollection->addFieldToSelect('*');
  147. $dailydealcollection->addFieldToFilter('sw_product_sku', ['eq'=>$dealproductsku]);
  148. $discountType=$dailydealcollection->getFirstItem()->getSwDiscountType();
  149. if ($discountType ==1) {
  150. $off=(($this->getProductPrice($dealproductsku)-$this->getDealProductPrice($dealproductsku))* 100)/ $this->getProductPrice($dealproductsku) ;
  151. return $off;
  152. } elseif ($discountType ==2) {
  153. return $dailydealcollection->getFirstItem()->getSwDiscountAmount();
  154. }
  155. }
  156. // Get "Save value" (In price) of dailydeal Product
  157. public function getDealSaveValue($dealproductsku)
  158. {
  159. $dailydealcollection=$this->getDailydealcollection();
  160. $dailydealcollection->addFieldToSelect('*');
  161. $dailydealcollection->addFieldToFilter('sw_product_sku', ['eq'=>$dealproductsku]);
  162. $discountType=$dailydealcollection->getFirstItem()->getSwDiscountType();
  163. if ($discountType ==1) {
  164. return $dailydealcollection->getFirstItem()->getSwDiscountAmount();
  165. } elseif ($discountType ==2) {
  166. $save=$this->getProductPrice($dealproductsku) - $this->getDealProductPrice($dealproductsku);
  167. return $save;
  168. }
  169. }
  170. public function isLoadedTimer() {
  171. return $this->loadedTimer;
  172. }
  173. public function setLoadedTimer() {
  174. $this->loadedTimer = 1;
  175. }
  176. }