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
16
17
use Magento \Catalog \Model \Product \Option \Type \File ;
17
18
use Magento \Catalog \Model \Product \Option \Type \Select ;
18
19
use Magento \Catalog \Model \Product \Option \Type \Text ;
20
+ use Magento \Catalog \Model \Product \Option \Value ;
19
21
use Magento \Catalog \Model \ResourceModel \Product \Option \Value \Collection ;
20
- use Magento \Catalog \Pricing \Price \BasePrice ;
22
+ use Magento \Catalog \Pricing \Price \CalculateCustomOptionCatalogRule ;
23
+ use Magento \Framework \App \ObjectManager ;
21
24
use Magento \Framework \EntityManager \MetadataPool ;
22
25
use Magento \Framework \Exception \LocalizedException ;
23
26
use Magento \Framework \Model \AbstractExtensibleModel ;
@@ -123,6 +126,11 @@ class Option extends AbstractExtensibleModel implements ProductCustomOptionInter
123
126
*/
124
127
private $ customOptionValuesFactory ;
125
128
129
+ /**
130
+ * @var CalculateCustomOptionCatalogRule
131
+ */
132
+ private $ calculateCustomOptionCatalogRule ;
133
+
126
134
/**
127
135
* @param \Magento\Framework\Model\Context $context
128
136
* @param \Magento\Framework\Registry $registry
@@ -138,6 +146,7 @@ class Option extends AbstractExtensibleModel implements ProductCustomOptionInter
138
146
* @param ProductCustomOptionValuesInterfaceFactory|null $customOptionValuesFactory
139
147
* @param array $optionGroups
140
148
* @param array $optionTypesToGroups
149
+ * @param CalculateCustomOptionCatalogRule $calculateCustomOptionCatalogRule
141
150
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
142
151
*/
143
152
public function __construct (
@@ -154,14 +163,17 @@ public function __construct(
154
163
array $ data = [],
155
164
ProductCustomOptionValuesInterfaceFactory $ customOptionValuesFactory = null ,
156
165
array $ optionGroups = [],
157
- array $ optionTypesToGroups = []
166
+ array $ optionTypesToGroups = [],
167
+ CalculateCustomOptionCatalogRule $ calculateCustomOptionCatalogRule = null
158
168
) {
159
169
$ this ->productOptionValue = $ productOptionValue ;
160
170
$ this ->optionTypeFactory = $ optionFactory ;
161
171
$ this ->string = $ string ;
162
172
$ this ->validatorPool = $ validatorPool ;
163
173
$ this ->customOptionValuesFactory = $ customOptionValuesFactory ?:
164
- \Magento \Framework \App \ObjectManager::getInstance ()->get (ProductCustomOptionValuesInterfaceFactory::class);
174
+ ObjectManager::getInstance ()->get (ProductCustomOptionValuesInterfaceFactory::class);
175
+ $ this ->calculateCustomOptionCatalogRule = $ calculateCustomOptionCatalogRule ??
176
+ ObjectManager::getInstance ()->get (CalculateCustomOptionCatalogRule::class);
165
177
$ this ->optionGroups = $ optionGroups ?: [
166
178
self ::OPTION_GROUP_DATE => Date::class,
167
179
self ::OPTION_GROUP_FILE => File::class,
@@ -193,10 +205,7 @@ public function __construct(
193
205
}
194
206
195
207
/**
196
- * Get resource instance
197
- *
198
- * @return \Magento\Framework\Model\ResourceModel\Db\AbstractDb
199
- * @deprecated 101.1.0 because resource models should be used directly
208
+ * @inheritdoc
200
209
*/
201
210
protected function _getResource ()
202
211
{
@@ -462,10 +471,12 @@ public function afterSave()
462
471
*/
463
472
public function getPrice ($ flag = false )
464
473
{
465
- if ($ flag && $ this ->getPriceType () == self ::$ typePercent ) {
466
- $ basePrice = $ this ->getProduct ()->getPriceInfo ()->getPrice (BasePrice::PRICE_CODE )->getValue ();
467
- $ price = $ basePrice * ($ this ->_getData (self ::KEY_PRICE ) / 100 );
468
- return $ price ;
474
+ if ($ flag ) {
475
+ return $ this ->calculateCustomOptionCatalogRule ->execute (
476
+ $ this ->getProduct (),
477
+ (float )$ this ->getData (self ::KEY_PRICE ),
478
+ $ this ->getPriceType () === Value::TYPE_PERCENT
479
+ );
469
480
}
470
481
return $ this ->_getData (self ::KEY_PRICE );
471
482
}
@@ -559,9 +570,7 @@ public function getSearchableData($productId, $storeId)
559
570
}
560
571
561
572
/**
562
- * Clearing object's data
563
- *
564
- * @return $this
573
+ * @inheritdoc
565
574
*/
566
575
protected function _clearData ()
567
576
{
@@ -571,9 +580,7 @@ protected function _clearData()
571
580
}
572
581
573
582
/**
574
- * Clearing cyclic references
575
- *
576
- * @return $this
583
+ * @inheritdoc
577
584
*/
578
585
protected function _clearReferences ()
579
586
{
@@ -594,9 +601,7 @@ protected function _getValidationRulesBeforeSave()
594
601
}
595
602
596
603
/**
597
- * Get product SKU
598
- *
599
- * @return string
604
+ * @inheritdoc
600
605
*/
601
606
public function getProductSku ()
602
607
{
@@ -608,9 +613,7 @@ public function getProductSku()
608
613
}
609
614
610
615
/**
611
- * Get option id
612
- *
613
- * @return int|null
616
+ * @inheritdoc
614
617
* @codeCoverageIgnoreStart
615
618
*/
616
619
public function getOptionId ()
@@ -619,60 +622,47 @@ public function getOptionId()
619
622
}
620
623
621
624
/**
622
- * Get option title
623
- *
624
- * @return string
625
+ * @inheritdoc
625
626
*/
626
627
public function getTitle ()
627
628
{
628
629
return $ this ->_getData (self ::KEY_TITLE );
629
630
}
630
631
631
632
/**
632
- * Get option type
633
- *
634
- * @return string
633
+ * @inheritdoc
635
634
*/
636
635
public function getType ()
637
636
{
638
637
return $ this ->_getData (self ::KEY_TYPE );
639
638
}
640
639
641
640
/**
642
- * Get sort order
643
- *
644
- * @return int
641
+ * @inheritdoc
645
642
*/
646
643
public function getSortOrder ()
647
644
{
648
645
return $ this ->_getData (self ::KEY_SORT_ORDER );
649
646
}
650
647
651
648
/**
652
- * Get is require
653
- *
654
- * @return bool
655
- * @SuppressWarnings(PHPMD.BooleanGetMethodName)
649
+ * @inheritdoc
656
650
*/
657
651
public function getIsRequire ()
658
652
{
659
653
return $ this ->_getData (self ::KEY_IS_REQUIRE );
660
654
}
661
655
662
656
/**
663
- * Get price type
664
- *
665
- * @return string|null
657
+ * @inheritdoc
666
658
*/
667
659
public function getPriceType ()
668
660
{
669
661
return $ this ->_getData (self ::KEY_PRICE_TYPE );
670
662
}
671
663
672
664
/**
673
- * Get Sku
674
- *
675
- * @return string|null
665
+ * @inheritdoc
676
666
*/
677
667
public function getSku ()
678
668
{
@@ -720,98 +710,71 @@ public function getImageSizeY()
720
710
}
721
711
722
712
/**
723
- * Set product SKU
724
- *
725
- * @param string $productSku
726
- * @return $this
713
+ * @inheritdoc
727
714
*/
728
715
public function setProductSku ($ productSku )
729
716
{
730
717
return $ this ->setData (self ::KEY_PRODUCT_SKU , $ productSku );
731
718
}
732
719
733
720
/**
734
- * Set option id
735
- *
736
- * @param int $optionId
737
- * @return $this
721
+ * @inheritdoc
738
722
*/
739
723
public function setOptionId ($ optionId )
740
724
{
741
725
return $ this ->setData (self ::KEY_OPTION_ID , $ optionId );
742
726
}
743
727
744
728
/**
745
- * Set option title
746
- *
747
- * @param string $title
748
- * @return $this
729
+ * @inheritdoc
749
730
*/
750
731
public function setTitle ($ title )
751
732
{
752
733
return $ this ->setData (self ::KEY_TITLE , $ title );
753
734
}
754
735
755
736
/**
756
- * Set option type
757
- *
758
- * @param string $type
759
- * @return $this
737
+ * @inheritdoc
760
738
*/
761
739
public function setType ($ type )
762
740
{
763
741
return $ this ->setData (self ::KEY_TYPE , $ type );
764
742
}
765
743
766
744
/**
767
- * Set sort order
768
- *
769
- * @param int $sortOrder
770
- * @return $this
745
+ * @inheritdoc
771
746
*/
772
747
public function setSortOrder ($ sortOrder )
773
748
{
774
749
return $ this ->setData (self ::KEY_SORT_ORDER , $ sortOrder );
775
750
}
776
751
777
752
/**
778
- * Set is require
779
- *
780
- * @param bool $isRequired
781
- * @return $this
753
+ * @inheritdoc
782
754
*/
783
755
public function setIsRequire ($ isRequired )
784
756
{
785
757
return $ this ->setData (self ::KEY_IS_REQUIRE , $ isRequired );
786
758
}
787
759
788
760
/**
789
- * Set price
790
- *
791
- * @param float $price
792
- * @return $this
761
+ * @inheritdoc
793
762
*/
794
763
public function setPrice ($ price )
795
764
{
796
765
return $ this ->setData (self ::KEY_PRICE , $ price );
797
766
}
798
767
799
768
/**
800
- * Set price type
801
- *
802
- * @param string $priceType
803
- * @return $this
769
+ * @inheritdoc
804
770
*/
805
771
public function setPriceType ($ priceType )
806
772
{
807
773
return $ this ->setData (self ::KEY_PRICE_TYPE , $ priceType );
808
774
}
809
775
810
776
/**
811
- * Set Sku
812
- *
813
- * @param string $sku
814
- * @return $this
777
+ * @inheritdoc
815
778
*/
816
779
public function setSku ($ sku )
817
780
{
@@ -952,7 +915,7 @@ public function setExtensionAttributes(
952
915
private function getOptionRepository ()
953
916
{
954
917
if (null === $ this ->optionRepository ) {
955
- $ this ->optionRepository = \ Magento \ Framework \ App \ ObjectManager::getInstance ()
918
+ $ this ->optionRepository = ObjectManager::getInstance ()
956
919
->get (\Magento \Catalog \Model \Product \Option \Repository::class);
957
920
}
958
921
return $ this ->optionRepository ;
@@ -966,7 +929,7 @@ private function getOptionRepository()
966
929
private function getMetadataPool ()
967
930
{
968
931
if (null === $ this ->metadataPool ) {
969
- $ this ->metadataPool = \ Magento \ Framework \ App \ ObjectManager::getInstance ()
932
+ $ this ->metadataPool = ObjectManager::getInstance ()
970
933
->get (\Magento \Framework \EntityManager \MetadataPool::class);
971
934
}
972
935
return $ this ->metadataPool ;
0 commit comments