Skip to content

Commit 1e7f567

Browse files
committed
updated files
0 parents  commit 1e7f567

File tree

16 files changed

+1101
-0
lines changed

16 files changed

+1101
-0
lines changed

Block/Estimate.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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\Block;
22+
23+
use Magento\Framework\View\Element\Template\Context;
24+
use Magento\Framework\View\Element\Template;
25+
use Magento\Catalog\Block\Product\View;
26+
27+
class Estimate extends Template
28+
{
29+
protected $_product_view;
30+
protected $_helperData;
31+
32+
/**
33+
* Estimate constructor.
34+
* @param View $_product_view
35+
* @param Context $context
36+
* @param \Lof\EstimateShippingRate\Helper\Data $helperData
37+
* @param array $data
38+
*/
39+
public function __construct(
40+
View $_product_view,
41+
Context $context,
42+
\Lof\EstimateShippingRate\Helper\Data $helperData,
43+
array $data = []
44+
) {
45+
$this->_product_view = $_product_view;
46+
$this->_helperData = $helperData;
47+
parent::__construct($context, $data);
48+
}
49+
50+
/**
51+
* @return \Magento\Catalog\Model\Product
52+
*/
53+
public function getProductInfo() {
54+
return $this->_product_view->getProduct();
55+
}
56+
/**
57+
* Prepare Content HTML
58+
*
59+
* @return string
60+
*/
61+
protected function _toHtml()
62+
{
63+
if(!$this->_helperData->getEnable()){
64+
return;
65+
}
66+
return parent::_toHtml();
67+
}
68+
69+
}

Controller/Product/Estimate.php

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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+
}

Helper/Data.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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\Helper;
22+
23+
use Magento\Framework\App\Config\ScopeConfigInterface;
24+
25+
/**
26+
* Lof EstimateShippingRate Data Helper
27+
*
28+
*/
29+
class Data extends \Magento\Framework\App\Helper\AbstractHelper
30+
{
31+
protected $_storeManager;
32+
/**
33+
* @param \Magento\Framework\App\Helper\Context
34+
* @param \Magento\Store\Model\StoreManagerInterface
35+
*/
36+
public function __construct(
37+
\Magento\Framework\App\Helper\Context $context,
38+
\Magento\Store\Model\StoreManagerInterface $storeManager
39+
) {
40+
parent::__construct($context);
41+
$this->_storeManager = $storeManager;
42+
}
43+
44+
public function getConfig($key, $store = null)
45+
{
46+
$store = $this->_storeManager->getStore($store);
47+
48+
$result = $this->scopeConfig->getValue(
49+
$key,
50+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
51+
$store);
52+
return $result;
53+
}
54+
/*
55+
* Return module status
56+
*/
57+
58+
public function getEnable($storeId = null)
59+
{
60+
return $this->getConfig('lof_estimateshippingrate/general/enable', $storeId);
61+
}
62+
63+
public function getDefaultCountryCode($storeId = null){
64+
return $this->getConfig('general/country/default', $storeId);
65+
}
66+
}

0 commit comments

Comments
 (0)