DealProductDiscountPrice.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace Smartwave\Dailydeals\Observer;
  3. use Magento\Framework\Event\ObserverInterface;
  4. use Magento\Framework\App\RequestInterface;
  5. class DealProductDiscountPrice implements ObserverInterface
  6. {
  7. protected $dailydealFactory;
  8. protected $helper;
  9. protected $scopeConfig;
  10. public function __construct(
  11. \Smartwave\Dailydeals\Model\DailydealFactory $dailydealFactory,
  12. \Smartwave\Dailydeals\Helper\Data $helper,
  13. \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  14. ) {
  15. $this->dailydealFactory = $dailydealFactory;
  16. $this->scopeConfig=$scopeConfig;
  17. $this->helper=$helper;
  18. }
  19. public function execute(\Magento\Framework\Event\Observer $observer)
  20. {
  21. $item = $observer->getEvent()->getData('quote_item');
  22. $item = ( $item->getParentItem() ? $item->getParentItem() : $item );
  23. $cartproduct_id=$item->getProductId();
  24. $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
  25. // Check For Grouped Product
  26. $product = $objectManager->create('Magento\GroupedProduct\Model\Product\Type\Grouped')->getParentIdsByChild($item->getProduct()->getId());
  27. if (isset($product[0])) {
  28. $product = $objectManager->create('Magento\Catalog\Model\Product')->load($product[0]);
  29. $groupedProductFalg=1;
  30. $groupedProduct=$product->getId();
  31. } else {
  32. $product = $objectManager->create('Magento\Catalog\Model\Product')->load($item->getProduct()->getId());
  33. }
  34. $dailydealcollection=$this->dailydealFactory->create()->getCollection();
  35. $dailydealcollection->addFieldToSelect('*');
  36. $dailydealcollection->addFieldToFilter('sw_product_sku', ['eq' => $product->getSku()]);
  37. $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
  38. $objDate =$objectManager ->create('Magento\Framework\Stdlib\DateTime\DateTime');
  39. $curdate=strtotime($objDate->gmtDate("Y-m-d H:i:s"));
  40. $Todate=strtotime($dailydealcollection->getFirstItem()->getSwDateTo());
  41. $fromdate=strtotime($dailydealcollection->getFirstItem()->getSwDateFrom());
  42. $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
  43. $configPath = "sw_dailydeal/general/dailydeal_enabled";
  44. $enabledvalue = $this->scopeConfig->getValue($configPath, $storeScope);
  45. if ($enabledvalue==1 && $curdate <= $Todate && $curdate >= $fromdate) {
  46. if ($item->getProduct()->getTypeId() == "bundle") {
  47. $bundleItemPrice=[];
  48. $options=$item->getProduct()->getTypeInstance(true)->getOrderOptions($item->getProduct());
  49. foreach ($options['bundle_options'] as $bundleitems) {
  50. foreach ($bundleitems['value'] as $sub) {
  51. array_push($bundleItemPrice, $sub['price']);
  52. }
  53. }
  54. foreach ($item->getQuote()->getAllItems() as $bundleitems) {
  55. /** @var $bundleitems\Magento\Quote\Model\Quote\Item */
  56. if (max($bundleItemPrice) == $bundleitems->getProduct()->getPrice()) {
  57. if ($dailydealcollection->getFirstItem()->getSwDiscountType() == 1) {
  58. $finalprice=$bundleitems->getProduct()->getFinalPrice()-$dailydealcollection->getFirstItem()->getSwDiscountAmount();
  59. } elseif ($dailydealcollection->getFirstItem()->getSwDiscountType() == 2) {
  60. $finalprice=$bundleitems->getProduct()->getFinalPrice()-(($item->getProduct()->getFinalPrice()*$dailydealcollection->getFirstItem()->getSwDiscountAmount())/100);
  61. }
  62. $bundleitems->setCustomPrice($finalprice);
  63. $bundleitems->setOriginalCustomPrice($finalprice);
  64. $item->getProduct()->setIsSuperMode(true);
  65. break;
  66. }
  67. }
  68. $item->getProduct()->setIsSuperMode(true);
  69. } else {
  70. if (isset($groupedProductFalg)) {
  71. $groupedItemPrice=[];
  72. foreach ($item->getQuote()->getAllItems() as $groupedItem) {
  73. $grouped_product = $objectManager->create('Magento\GroupedProduct\Model\Product\Type\Grouped')->getParentIdsByChild($groupedItem->getProduct()->getId());
  74. if (isset($grouped_product[0])) {
  75. if ($groupedProduct==$grouped_product[0]) {
  76. array_push($groupedItemPrice, $groupedItem->getProduct()->getPrice());
  77. }
  78. }
  79. }
  80. foreach ($item->getQuote()->getAllItems() as $groupedItem) {
  81. if (min($groupedItemPrice) == $groupedItem->getProduct()->getPrice()) {
  82. if ($dailydealcollection->getFirstItem()->getSwDiscountType() == 1) {
  83. $price=$groupedItem->getProduct()->getPrice()-$dailydealcollection->getFirstItem()->getSwDiscountAmount();
  84. } elseif ($dailydealcollection->getFirstItem()->getSwDiscountType() == 2) {
  85. $price=$groupedItem->getProduct()->getPrice()-(($groupedItem->getProduct()->getPrice()*$dailydealcollection->getFirstItem()->getSwDiscountAmount())/100);
  86. }
  87. break;
  88. }
  89. }
  90. } else {
  91. $price = $dailydealcollection->getFirstItem()->getSwProductPrice();
  92. }
  93. $item->setCustomPrice($price);
  94. $item->setOriginalCustomPrice($price);
  95. $item->getProduct()->setIsSuperMode(true);
  96. }
  97. }
  98. }
  99. }