3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+ declare (strict_types=1 );
6
7
7
8
namespace Magento \Catalog \Model \Product ;
8
9
11
12
use Magento \Catalog \Api \Data \ProductCustomOptionValuesInterfaceFactory ;
12
13
use Magento \Catalog \Api \Data \ProductInterface ;
13
14
use Magento \Catalog \Model \Product ;
15
+ use Magento \Catalog \Model \Product \Option \Value ;
14
16
use Magento \Catalog \Model \ResourceModel \Product \Option \Value \Collection ;
15
- use Magento \Catalog \Pricing \Price \BasePrice ;
17
+ use Magento \Catalog \Pricing \Price \CalculateCustomOptionCatalogRule ;
18
+ use Magento \Framework \App \ObjectManager ;
16
19
use Magento \Framework \EntityManager \MetadataPool ;
17
20
use Magento \Framework \Exception \LocalizedException ;
18
21
use Magento \Framework \Model \AbstractExtensibleModel ;
@@ -108,6 +111,11 @@ class Option extends AbstractExtensibleModel implements ProductCustomOptionInter
108
111
*/
109
112
private $ customOptionValuesFactory ;
110
113
114
+ /**
115
+ * @var CalculateCustomOptionCatalogRule
116
+ */
117
+ private $ calculateCustomOptionCatalogRule ;
118
+
111
119
/**
112
120
* @param \Magento\Framework\Model\Context $context
113
121
* @param \Magento\Framework\Registry $registry
@@ -121,6 +129,7 @@ class Option extends AbstractExtensibleModel implements ProductCustomOptionInter
121
129
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
122
130
* @param array $data
123
131
* @param ProductCustomOptionValuesInterfaceFactory|null $customOptionValuesFactory
132
+ * @param CalculateCustomOptionCatalogRule $calculateCustomOptionCatalogRule
124
133
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
125
134
*/
126
135
public function __construct (
@@ -135,14 +144,17 @@ public function __construct(
135
144
\Magento \Framework \Model \ResourceModel \AbstractResource $ resource = null ,
136
145
\Magento \Framework \Data \Collection \AbstractDb $ resourceCollection = null ,
137
146
array $ data = [],
138
- ProductCustomOptionValuesInterfaceFactory $ customOptionValuesFactory = null
147
+ ProductCustomOptionValuesInterfaceFactory $ customOptionValuesFactory = null ,
148
+ CalculateCustomOptionCatalogRule $ calculateCustomOptionCatalogRule = null
139
149
) {
140
150
$ this ->productOptionValue = $ productOptionValue ;
141
151
$ this ->optionTypeFactory = $ optionFactory ;
142
152
$ this ->validatorPool = $ validatorPool ;
143
153
$ this ->string = $ string ;
144
154
$ this ->customOptionValuesFactory = $ customOptionValuesFactory ?:
145
- \Magento \Framework \App \ObjectManager::getInstance ()->get (ProductCustomOptionValuesInterfaceFactory::class);
155
+ ObjectManager::getInstance ()->get (ProductCustomOptionValuesInterfaceFactory::class);
156
+ $ this ->calculateCustomOptionCatalogRule = $ calculateCustomOptionCatalogRule ??
157
+ ObjectManager::getInstance ()->get (CalculateCustomOptionCatalogRule::class);
146
158
147
159
parent ::__construct (
148
160
$ context ,
@@ -156,10 +168,7 @@ public function __construct(
156
168
}
157
169
158
170
/**
159
- * Get resource instance
160
- *
161
- * @return \Magento\Framework\Model\ResourceModel\Db\AbstractDb
162
- * @deprecated 101.1.0 because resource models should be used directly
171
+ * @inheritdoc
163
172
*/
164
173
protected function _getResource ()
165
174
{
@@ -439,10 +448,12 @@ public function afterSave()
439
448
*/
440
449
public function getPrice ($ flag = false )
441
450
{
442
- if ($ flag && $ this ->getPriceType () == self ::$ typePercent ) {
443
- $ basePrice = $ this ->getProduct ()->getPriceInfo ()->getPrice (BasePrice::PRICE_CODE )->getValue ();
444
- $ price = $ basePrice * ($ this ->_getData (self ::KEY_PRICE ) / 100 );
445
- return $ price ;
451
+ if ($ flag ) {
452
+ return $ this ->calculateCustomOptionCatalogRule ->execute (
453
+ $ this ->getProduct (),
454
+ (float )$ this ->getData (self ::KEY_PRICE ),
455
+ $ this ->getPriceType () === Value::TYPE_PERCENT
456
+ );
446
457
}
447
458
return $ this ->_getData (self ::KEY_PRICE );
448
459
}
@@ -536,9 +547,7 @@ public function getSearchableData($productId, $storeId)
536
547
}
537
548
538
549
/**
539
- * Clearing object's data
540
- *
541
- * @return $this
550
+ * @inheritdoc
542
551
*/
543
552
protected function _clearData ()
544
553
{
@@ -548,9 +557,7 @@ protected function _clearData()
548
557
}
549
558
550
559
/**
551
- * Clearing cyclic references
552
- *
553
- * @return $this
560
+ * @inheritdoc
554
561
*/
555
562
protected function _clearReferences ()
556
563
{
@@ -571,9 +578,7 @@ protected function _getValidationRulesBeforeSave()
571
578
}
572
579
573
580
/**
574
- * Get product SKU
575
- *
576
- * @return string
581
+ * @inheritdoc
577
582
*/
578
583
public function getProductSku ()
579
584
{
@@ -585,9 +590,7 @@ public function getProductSku()
585
590
}
586
591
587
592
/**
588
- * Get option id
589
- *
590
- * @return int|null
593
+ * @inheritdoc
591
594
* @codeCoverageIgnoreStart
592
595
*/
593
596
public function getOptionId ()
@@ -596,60 +599,47 @@ public function getOptionId()
596
599
}
597
600
598
601
/**
599
- * Get option title
600
- *
601
- * @return string
602
+ * @inheritdoc
602
603
*/
603
604
public function getTitle ()
604
605
{
605
606
return $ this ->_getData (self ::KEY_TITLE );
606
607
}
607
608
608
609
/**
609
- * Get option type
610
- *
611
- * @return string
610
+ * @inheritdoc
612
611
*/
613
612
public function getType ()
614
613
{
615
614
return $ this ->_getData (self ::KEY_TYPE );
616
615
}
617
616
618
617
/**
619
- * Get sort order
620
- *
621
- * @return int
618
+ * @inheritdoc
622
619
*/
623
620
public function getSortOrder ()
624
621
{
625
622
return $ this ->_getData (self ::KEY_SORT_ORDER );
626
623
}
627
624
628
625
/**
629
- * Get is require
630
- *
631
- * @return bool
632
- * @SuppressWarnings(PHPMD.BooleanGetMethodName)
626
+ * @inheritdoc
633
627
*/
634
628
public function getIsRequire ()
635
629
{
636
630
return $ this ->_getData (self ::KEY_IS_REQUIRE );
637
631
}
638
632
639
633
/**
640
- * Get price type
641
- *
642
- * @return string|null
634
+ * @inheritdoc
643
635
*/
644
636
public function getPriceType ()
645
637
{
646
638
return $ this ->_getData (self ::KEY_PRICE_TYPE );
647
639
}
648
640
649
641
/**
650
- * Get Sku
651
- *
652
- * @return string|null
642
+ * @inheritdoc
653
643
*/
654
644
public function getSku ()
655
645
{
@@ -697,98 +687,71 @@ public function getImageSizeY()
697
687
}
698
688
699
689
/**
700
- * Set product SKU
701
- *
702
- * @param string $productSku
703
- * @return $this
690
+ * @inheritdoc
704
691
*/
705
692
public function setProductSku ($ productSku )
706
693
{
707
694
return $ this ->setData (self ::KEY_PRODUCT_SKU , $ productSku );
708
695
}
709
696
710
697
/**
711
- * Set option id
712
- *
713
- * @param int $optionId
714
- * @return $this
698
+ * @inheritdoc
715
699
*/
716
700
public function setOptionId ($ optionId )
717
701
{
718
702
return $ this ->setData (self ::KEY_OPTION_ID , $ optionId );
719
703
}
720
704
721
705
/**
722
- * Set option title
723
- *
724
- * @param string $title
725
- * @return $this
706
+ * @inheritdoc
726
707
*/
727
708
public function setTitle ($ title )
728
709
{
729
710
return $ this ->setData (self ::KEY_TITLE , $ title );
730
711
}
731
712
732
713
/**
733
- * Set option type
734
- *
735
- * @param string $type
736
- * @return $this
714
+ * @inheritdoc
737
715
*/
738
716
public function setType ($ type )
739
717
{
740
718
return $ this ->setData (self ::KEY_TYPE , $ type );
741
719
}
742
720
743
721
/**
744
- * Set sort order
745
- *
746
- * @param int $sortOrder
747
- * @return $this
722
+ * @inheritdoc
748
723
*/
749
724
public function setSortOrder ($ sortOrder )
750
725
{
751
726
return $ this ->setData (self ::KEY_SORT_ORDER , $ sortOrder );
752
727
}
753
728
754
729
/**
755
- * Set is require
756
- *
757
- * @param bool $isRequired
758
- * @return $this
730
+ * @inheritdoc
759
731
*/
760
732
public function setIsRequire ($ isRequired )
761
733
{
762
734
return $ this ->setData (self ::KEY_IS_REQUIRE , $ isRequired );
763
735
}
764
736
765
737
/**
766
- * Set price
767
- *
768
- * @param float $price
769
- * @return $this
738
+ * @inheritdoc
770
739
*/
771
740
public function setPrice ($ price )
772
741
{
773
742
return $ this ->setData (self ::KEY_PRICE , $ price );
774
743
}
775
744
776
745
/**
777
- * Set price type
778
- *
779
- * @param string $priceType
780
- * @return $this
746
+ * @inheritdoc
781
747
*/
782
748
public function setPriceType ($ priceType )
783
749
{
784
750
return $ this ->setData (self ::KEY_PRICE_TYPE , $ priceType );
785
751
}
786
752
787
753
/**
788
- * Set Sku
789
- *
790
- * @param string $sku
791
- * @return $this
754
+ * @inheritdoc
792
755
*/
793
756
public function setSku ($ sku )
794
757
{
@@ -929,7 +892,7 @@ public function setExtensionAttributes(
929
892
private function getOptionRepository ()
930
893
{
931
894
if (null === $ this ->optionRepository ) {
932
- $ this ->optionRepository = \ Magento \ Framework \ App \ ObjectManager::getInstance ()
895
+ $ this ->optionRepository = ObjectManager::getInstance ()
933
896
->get (\Magento \Catalog \Model \Product \Option \Repository::class);
934
897
}
935
898
return $ this ->optionRepository ;
@@ -943,7 +906,7 @@ private function getOptionRepository()
943
906
private function getMetadataPool ()
944
907
{
945
908
if (null === $ this ->metadataPool ) {
946
- $ this ->metadataPool = \ Magento \ Framework \ App \ ObjectManager::getInstance ()
909
+ $ this ->metadataPool = ObjectManager::getInstance ()
947
910
->get (\Magento \Framework \EntityManager \MetadataPool::class);
948
911
}
949
912
return $ this ->metadataPool ;
0 commit comments