SwDealProduct.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace Smartwave\Dailydeals\Model\Dailydeal\Source;
  3. class SwDealProduct implements \Magento\Framework\Option\ArrayInterface
  4. {
  5. const FIXED = 1;
  6. const PERCENTAGE = 2;
  7. /**
  8. * to option array
  9. *
  10. * @return array
  11. */
  12. protected $productFactory;
  13. public function __construct(
  14. \Magento\Catalog\Model\ProductFactory $productFactory
  15. ) {
  16. $this->productFactory=$productFactory;
  17. }
  18. public function toOptionArray()
  19. {
  20. $childArray=[];
  21. $productcollection=$this->productFactory->create()->getCollection();
  22. $productcollection->addAttributeToSelect('*');
  23. foreach ($productcollection as $_product) {
  24. if ($_product->getTypeId() == "bundle") {
  25. $product = $this->productFactory->create()->load($_product->getId());
  26. //get all the selection products used in bundle product.
  27. $selectionCollection = $product->getTypeInstance(true)
  28. ->getSelectionsCollection(
  29. $product->getTypeInstance(true)->getOptionsIds($product),
  30. $product
  31. );
  32. foreach ($selectionCollection as $proselection) {
  33. array_push($childArray, $proselection->getProductId());
  34. }
  35. }
  36. }
  37. $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
  38. $currencySymbol=$objectManager->create('Magento\Store\Model\StoreManagerInterface');
  39. $currencysymbol=$currencySymbol->getStore()->getCurrentCurrency()->getCurrencySymbol();
  40. $productcollection=$this->productFactory->create()->getCollection();
  41. $productcollection->addAttributeToSelect('*');
  42. $productcollection->addAttributeToFilter('entity_id', ['nin'=>$childArray]);
  43. $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
  44. $options = ['value'=>'','label'=>'-- Select Product --'];
  45. foreach ($productcollection as $product) {
  46. $productId = $product->getId(); //this is child product id
  47. $getproduct = $objectManager->create('Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable')->getParentIdsByChild($productId);
  48. if (isset($getproduct[0])) {
  49. $productcollection1=$this->productFactory->create()->getCollection();
  50. $productcollection1->addFieldToSelect('*');
  51. $productcollection1->addFieldToFilter('entity_id', ['eq'=>$getproduct[0]]);
  52. $sku=$productcollection1->getFirstItem()->getSku();
  53. $name=$productcollection1->getFirstItem()->getName() ;
  54. $price=$product->getFinalPrice();
  55. $id=$productcollection1->getFirstItem()->getId();
  56. } else {
  57. if ($product->getTypeId() == "bundle") {
  58. $bundleprice=[];
  59. $sku=$product->getSku();
  60. $name=$product->getName();
  61. $id=$product->getId();
  62. $bundleproduct = $this->productFactory->create()->load($product->getId());
  63. //get all the selection products used in bundle product.
  64. $selectionCollection = $bundleproduct->getTypeInstance(true)
  65. ->getSelectionsCollection(
  66. $bundleproduct->getTypeInstance(true)->getOptionsIds($bundleproduct),
  67. $bundleproduct
  68. );
  69. foreach ($selectionCollection as $proselection) {
  70. array_push($bundleprice, $proselection->getFinalPrice());
  71. }
  72. $price=min($bundleprice);
  73. } elseif ($product->getTypeId() == "grouped") {
  74. $groupedprice=[];
  75. $groupedproduct = $this->productFactory->create()->load($product->getId());
  76. $associatedProducts =$groupedproduct->getTypeInstance()->getAssociatedProducts($groupedproduct);
  77. foreach ($associatedProducts as $_item) {
  78. array_push($groupedprice, $_item->getFinalPrice());
  79. }
  80. $sku=$product->getSku();
  81. $name=$product->getName();
  82. $id=$product->getId();
  83. $price=min($groupedprice);
  84. } elseif ($product->getvisibility() !=1) {
  85. $sku=$product->getSku();
  86. $name=$product->getName();
  87. $id=$product->getId();
  88. $price=$product->getFinalPrice();
  89. }
  90. }
  91. if ($price != 0) {
  92. $options[] =
  93. [ 'value'=>$sku,
  94. 'label'=>"ID:".$id." ".$name."- ".$currencysymbol."".round($price, 2)." "
  95. ];
  96. }
  97. }
  98. $unique = array_map("unserialize", array_unique(array_map("serialize", $options)));
  99. return $unique;
  100. }
  101. }