1
+ <?php
2
+ /**
3
+ * LandOfCoder
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Landofcoder.com license that is
8
+ * available through the world-wide-web at this URL:
9
+ * http://www.landofcoder.com/license-agreement.html
10
+ *
11
+ * DISCLAIMER
12
+ *
13
+ * Do not edit or add to this file if you wish to upgrade this extension to newer
14
+ * version in the future.
15
+ *
16
+ * @category LandOfCoder
17
+ * @package Lof_EstimateShippingRate
18
+ * @copyright Copyright (c) 2020 Landofcoder (http://www.LandOfCoder.com/)
19
+ * @license http://www.LandOfCoder.com/LICENSE-1.0.html
20
+ */
21
+ namespace Lof \EstimateShippingRate \Controller \Product ;
22
+ use \Lof \EstimateShippingRate \Helper \Data as LofData ;
23
+ use Magento \{Framework \App \Action \Context ,
24
+ Framework \App \Action \Action ,
25
+ Catalog \Api \ProductRepositoryInterface ,
26
+ Quote \Model \QuoteFactory ,
27
+ Framework \Pricing \Helper \Data };
28
+
29
+ class Estimate extends Action
30
+ {
31
+ protected $ product_repository ;
32
+ protected $ quote ;
33
+ protected $ pricingHelper ;
34
+ protected $ helperData ;
35
+
36
+ /**
37
+ * Estimate constructor.
38
+ * @param ProductRepositoryInterface $product_repository
39
+ * @param QuoteFactory $quote
40
+ * @param Data $pricingHelper
41
+ * @param LofData $helperData
42
+ * @param Context $context
43
+ */
44
+ public function __construct (
45
+ ProductRepositoryInterface $ product_repository ,
46
+ QuoteFactory $ quote ,
47
+ Data $ pricingHelper ,
48
+ LofData $ helperData ,
49
+ Context $ context
50
+ ) {
51
+ $ this ->product_repository = $ product_repository ;
52
+ $ this ->quote = $ quote ;
53
+ $ this ->pricingHelper = $ pricingHelper ;
54
+ $ this ->helperData = $ helperData ;
55
+ parent ::__construct ($ context );
56
+ }
57
+
58
+ /**
59
+ * @return \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\ResultInterface|void
60
+ */
61
+ public function execute ()
62
+ {
63
+ $ _params = $ this ->getRequest ()->getParams ();
64
+ $ storeId = isset ($ _params ['storeId ' ])?$ _params ['storeId ' ]:0 ;
65
+ $ response = [];
66
+ if ($ this ->helperData ->getEnable ($ storeId )){
67
+ if (
68
+ empty ($ _params ) ||
69
+ !isset ($ _params ['cep ' ]) ||
70
+ $ _params ['cep ' ] == ""
71
+ ) {
72
+ $ response ['error ' ]['message ' ] = __ ('Postcode not informed ' );
73
+ } else if (
74
+ !isset ($ _params ['product ' ]) ||
75
+ $ _params ['product ' ] == "" ||
76
+ $ _params ['product ' ] == 0 ||
77
+ !is_numeric ($ _params ['product ' ])
78
+ ) {
79
+ $ response ['error ' ]['message ' ] = __ ('Amount reported is invalid ' );
80
+ }
81
+
82
+ if (!isset ($ response ['error ' ])) {
83
+ if (
84
+ !isset ($ _params ['qty ' ]) ||
85
+ $ _params ['qty ' ] == "" ||
86
+ $ _params ['qty ' ] == 0 ||
87
+ !is_numeric ($ _params ['qty ' ])
88
+ ) {
89
+ $ qty = 1 ;
90
+ } else {
91
+ $ qty = $ _params ['qty ' ];
92
+ }
93
+
94
+ try {
95
+ $ _product = $ this ->product_repository ->getById ($ _params ['product ' ]);
96
+ $ default_country_id = $ this ->helperData ->getDefaultCountryCode ($ storeId );
97
+ $ quote = $ this ->quote ->create ();
98
+ $ quote ->addProduct ($ _product , $ qty );
99
+ $ quote ->getShippingAddress ()->setCountryId ($ default_country_id );
100
+ $ quote ->getShippingAddress ()->setPostcode ($ _params ['cep ' ]);
101
+ $ quote ->getShippingAddress ()->setCollectShippingRates (true );
102
+ $ quote ->getShippingAddress ()->collectShippingRates ();
103
+ $ rates = $ quote ->getShippingAddress ()->getShippingRatesCollection ();
104
+
105
+ if (count ($ rates )>0 ){
106
+ $ shipping_methods = [];
107
+
108
+ foreach ($ rates as $ rate ) {
109
+ $ _message = !$ rate ->getErrorMessage () ? "" : $ rate ->getErrorMessage ();
110
+ $ shipping_methods [$ rate ->getCarrierTitle ()][] = array (
111
+ 'title ' => $ rate ->getMethodTitle (),
112
+ 'price ' => $ this ->pricingHelper ->currency ($ rate ->getPrice ()),
113
+ 'message ' => $ _message ,
114
+ );
115
+ }
116
+
117
+ $ response = $ shipping_methods ;
118
+ } else {
119
+ $ response ['error ' ]['message ' ] = __ ('There is no shipping method available at this time. ' );
120
+ }
121
+
122
+ } catch (\Exception $ e ){
123
+ $ response ['error ' ]['message ' ] = $ e ->getMessage ();
124
+ echo json_encode ($ response , true );
125
+ exit ;
126
+ }
127
+ }
128
+ }
129
+ echo json_encode ($ response , true );
130
+ exit ;
131
+ }
132
+ }
0 commit comments