20
20
21
21
namespace Meta \Sales \Plugin ;
22
22
23
- use Magento \OfflineShipping \Model \Carrier \Flatrate ;
24
- use Magento \OfflineShipping \Model \Carrier \Freeshipping ;
25
- use Magento \OfflineShipping \Model \Carrier \Tablerate ;
23
+ use Magento \Framework \App \Config \ScopeConfigInterface ;
26
24
use Magento \OfflineShipping \Model \ResourceModel \Carrier \Tablerate \CollectionFactory ;
27
- use Magento \Shipping \Model \Carrier \AbstractCarrier ;
28
25
29
26
class ShippingData
30
27
{
31
28
/**
32
- * @var Flatrate
33
- */
34
- protected Flatrate $ flatRateModel ;
35
-
36
- /**
37
- * @var Tablerate
29
+ * @var CollectionFactory
38
30
*/
39
- protected Tablerate $ tableRateModel ;
31
+ protected CollectionFactory $ tableRateCollection ;
40
32
41
33
/**
42
- * @var Freeshipping
34
+ * @var ScopeConfigInterface
43
35
*/
44
- protected Freeshipping $ freeShippingModel ;
36
+ protected ScopeConfigInterface $ scopeConfig ;
45
37
46
38
/**
47
- * @var CollectionFactory
39
+ * @var string
48
40
*/
49
- protected CollectionFactory $ tableRateCollection ;
41
+ private string $ storeId ;
50
42
51
43
public const ATTR_ENABLED = 'enabled ' ;
52
44
public const ATTR_TITLE = 'title ' ;
@@ -58,78 +50,50 @@ class ShippingData
58
50
public const ATTR_FREE_SHIPPING_MIN_ORDER_AMOUNT = 'free_shipping_minimum_order_amount ' ;
59
51
60
52
/**
61
- * @param Flatrate $flatRateModel
62
- * @param Tablerate $tableRateModel
63
- * @param Freeshipping $freeShippingModel
64
53
* @param CollectionFactory $tableRateCollection
54
+ * @param ScopeConfigInterface $scopeConfig
65
55
*/
66
56
public function __construct (
67
- Flatrate $ flatRateModel ,
68
- Tablerate $ tableRateModel ,
69
- Freeshipping $ freeShippingModel ,
70
- CollectionFactory $ tableRateCollection
57
+ CollectionFactory $ tableRateCollection ,
58
+ ScopeConfigInterface $ scopeConfig
71
59
) {
72
- $ this ->flatRateModel = $ flatRateModel ;
73
- $ this ->tableRateModel = $ tableRateModel ;
74
- $ this ->freeShippingModel = $ freeShippingModel ;
60
+ $ this ->scopeConfig = $ scopeConfig ;
75
61
$ this ->tableRateCollection = $ tableRateCollection ;
76
62
}
77
63
78
64
/**
79
- * A function that builds a table rates shipping profile
80
- *
81
- * @return array
82
- */
83
- public function buildTableRatesProfile (): array
84
- {
85
- return $ this ->buildShippingProfile ($ this ->tableRateModel , true );
86
- }
87
-
88
- /**
89
- * A function that builds a flat-rate shipping profile
90
- *
91
- * @return array
92
- */
93
- public function buildFlatRateProfile (): array
94
- {
95
- return $ this ->buildShippingProfile ($ this ->flatRateModel , false );
96
- }
97
-
98
- /**
99
- * A function that builds a free shipping profile
65
+ * Setter for store id
100
66
*
101
- * @return array
67
+ * @param string $store_id
68
+ * @return void
102
69
*/
103
- public function buildFreeShippingProfile (): array
70
+ public function setStoreId ( string $ store_id )
104
71
{
105
- return $ this ->buildShippingProfile ( $ this -> freeShippingModel , false ) ;
72
+ $ this ->storeId = $ store_id ;
106
73
}
107
74
108
75
/**
109
76
* Returns shipping profiles based on the AbstractModel carrier passed in
110
77
*
111
- * @param AbstractCarrier $model
112
- * @param bool $tableRates
78
+ * @param string $shippingProfileType
113
79
* @return array
114
80
*/
115
- private function buildShippingProfile (AbstractCarrier $ model , bool $ tableRates ): array
81
+ public function buildShippingProfile (string $ shippingProfileType ): array
116
82
{
117
- $ isEnabled = $ model -> getConfigData ( 'active ' );
118
- $ title = $ model -> getConfigData ( 'title ' );
119
- $ methodName = $ model -> getConfigData ( 'name ' );
120
- $ price = (float )$ model -> getConfigData ( " price " ) ?? 0.0 ;
121
- $ allowedCountries = $ model -> getConfigData ( ' specificcountry ' );
122
- if ($ tableRates ) {
83
+ $ isEnabled = $ this -> getFieldFromModel ( $ shippingProfileType , 'active ' );
84
+ $ title = $ this -> getFieldFromModel ( $ shippingProfileType , 'title ' );
85
+ $ methodName = $ this -> getFieldFromModel ( $ shippingProfileType , 'name ' );
86
+ $ price = (float )$ this -> getFieldFromModel ( $ shippingProfileType , ' price ' ) ?? 0.0 ;
87
+ $ allowedCountries = $ this -> getFieldFromModel ( $ shippingProfileType , ' specificcountries ' );
88
+ if ($ shippingProfileType === ShippingProfileTypes:: TABLE_RATE ) {
123
89
$ shippingMethods = $ this ->getShippingMethodsInfoForTableRates ();
124
90
} else {
125
91
$ shippingMethods = $ this ->buildShippingMethodsInfo ($ allowedCountries , $ price );
126
92
}
127
-
128
- $ freeShippingThreshold = $ model ->getConfigData ('free_shipping_subtotal ' );
129
-
130
- $ handlingFee = $ model ->getConfigData ('handling_fee ' );
131
- $ handlingFeeType = $ model ->getConfigData ('handling_type ' );
132
- $ shippingType = $ model ->getConfigData ('type ' );
93
+ $ freeShippingThreshold = $ this ->getFieldFromModel ($ shippingProfileType , 'free_shipping_subtotal ' );
94
+ $ handlingFee = $ this ->getFieldFromModel ($ shippingProfileType , 'handling_fee ' );
95
+ $ handlingFeeType = $ this ->getFieldFromModel ($ shippingProfileType , 'handling_type ' );
96
+ $ shippingType = $ this ->getFieldFromModel ($ shippingProfileType , 'type ' );
133
97
return [
134
98
self ::ATTR_ENABLED => $ isEnabled ,
135
99
self ::ATTR_TITLE => $ title ,
@@ -142,6 +106,19 @@ private function buildShippingProfile(AbstractCarrier $model, bool $tableRates):
142
106
];
143
107
}
144
108
109
+ /**
110
+ * Get field from abstract carrier DB
111
+ *
112
+ * @param string $shippingProfileType
113
+ * @param string $field
114
+ * @return mixed
115
+ */
116
+ private function getFieldFromModel (string $ shippingProfileType , string $ field )
117
+ {
118
+ $ path = 'carriers/ ' . $ shippingProfileType . '/ ' . $ field ;
119
+ return $ this ->scopeConfig ->getValue ($ path , \Magento \Store \Model \ScopeInterface::SCOPE_STORE , $ this ->storeId );
120
+ }
121
+
145
122
/**
146
123
* A function that builds shipping methods info for shipping profiles which are not table rates
147
124
*
0 commit comments