-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from yangyao/protection
Init protection feature
- Loading branch information
Showing
8 changed files
with
298 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
.DS_Store | ||
.DS_Store | ||
.vscode/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
/** | ||
* AfterShip Tracking Constants | ||
* | ||
* @author AfterShip <[email protected]> | ||
* @copyright 2023 AfterShip | ||
* @license MIT http://opensource.org/licenses/MIT | ||
* @link https://aftership.com | ||
*/ | ||
|
||
namespace AfterShip\Tracking; | ||
|
||
/** | ||
* All Constants use for this plugin. | ||
* | ||
* @author AfterShip <[email protected]> | ||
* @license MIT http://opensource.org/licenses/MIT | ||
* @link https://aftership.com | ||
*/ | ||
class Constants | ||
{ | ||
public const AFTERSHIP_PROTECTION_SKU = 'aftership-protection'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,226 @@ | ||
<?php | ||
|
||
namespace AfterShip\Tracking\Controller\Protection; | ||
|
||
use Magento\Catalog\Api\Data\ProductInterface; | ||
use Magento\Catalog\Api\ProductRepositoryInterface; | ||
use Magento\Framework\App\Action\Action; | ||
use Magento\Framework\App\Action\Context; | ||
use Magento\Checkout\Model\Cart; | ||
use Magento\Framework\View\Asset\Repository as AssetRepository; | ||
use Magento\Store\Model\StoreManagerInterface; | ||
use AfterShip\Tracking\Constants; | ||
use Magento\Catalog\Model\Product\Attribute\Source\Status; | ||
use Magento\Catalog\Model\Product\Visibility; | ||
use Magento\Catalog\Model\Product\Type; | ||
use Magento\Catalog\Api\Data\ProductInterfaceFactory; | ||
use Magento\Framework\App\Filesystem\DirectoryList; | ||
use Magento\Framework\Filesystem\Io\File; | ||
use Exception; | ||
use Throwable; | ||
|
||
class Adjust extends Action | ||
{ | ||
protected $cart; | ||
protected $assetRepository; | ||
protected $storeManager; | ||
protected $productInterfaceFactory; | ||
protected $productRepository; | ||
protected $directoryList; | ||
protected $file; | ||
|
||
|
||
/** | ||
* Inject dependencies | ||
* | ||
* @param Context $context | ||
* @param Cart $cart | ||
* @param AssetRepository $assetRepository | ||
* @param StoreManagerInterface $storeManager | ||
* @param ProductInterfaceFactory $productInterfaceFactory | ||
* @param DirectoryList $directoryList | ||
* @param File $file | ||
* @see http://www.example.com/aftership/protection/adjust | ||
*/ | ||
public function __construct( | ||
Context $context, | ||
Cart $cart, | ||
AssetRepository $assetRepository, | ||
StoreManagerInterface $storeManager, | ||
ProductInterfaceFactory $productInterfaceFactory, | ||
ProductRepositoryInterface $productRepository, | ||
DirectoryList $directoryList, | ||
File $file | ||
) | ||
{ | ||
parent::__construct($context); | ||
$this->cart = $cart; | ||
$this->assetRepository = $assetRepository; | ||
$this->storeManager = $storeManager; | ||
$this->productRepository = $productRepository; | ||
$this->productInterfaceFactory = $productInterfaceFactory; | ||
$this->directoryList = $directoryList; | ||
$this->file = $file; | ||
} | ||
|
||
/** | ||
* Add protection product to cart | ||
* | ||
* @return void | ||
*/ | ||
public function execute() | ||
{ | ||
try { | ||
$newPrice = abs($this->getRequest()->getParam('price')); | ||
$product = $this->getProtectionProduct(); | ||
$this->cart->addProduct($product, [ | ||
'product' => $product->getId(), | ||
'qty' => 1 | ||
]); | ||
$quote = $this->cart->getQuote(); | ||
$quote->getItemByProduct($product) | ||
->setQty(1) | ||
->setBaseCost($newPrice) | ||
->setPrice($newPrice) | ||
->setPriceInclTax($newPrice) | ||
->setBasePrice($newPrice) | ||
->setBasePriceInclTax($newPrice) | ||
->setCustomPrice($newPrice) | ||
->setOriginalCustomPrice($newPrice) | ||
->setRowTotal($newPrice) | ||
->setRowTotalInclTax($newPrice) | ||
->setRowTotalWithDiscount($newPrice) | ||
->setBaseRowTotal($newPrice) | ||
->setBaseRowTotalInclTax($newPrice) | ||
->setTaxAmount(0) | ||
->setBaseTaxAmount(0) | ||
->setBaseTaxBeforeDiscount(0) | ||
->setTaxBeforeDiscount(0) | ||
->getProduct() | ||
->setIsSuperMode(true) | ||
->save(); | ||
$quote | ||
->setTotalsCollectedFlag(false) | ||
->collectTotals() | ||
->save(); | ||
$result = [ | ||
'price' => $newPrice, | ||
'success' => true | ||
]; | ||
} catch (Throwable $t) { | ||
$this->getResponse()->setHttpResponseCode(400); | ||
$result = [ | ||
'success' => false, | ||
'detail' => 'failed to create protection product', | ||
'error_message' => $t->getMessage(), | ||
'error_trace' => $t->getTraceAsString(), | ||
]; | ||
} catch (Exception $e) { | ||
$this->getResponse()->setHttpResponseCode(400); | ||
$result = [ | ||
'success' => false, | ||
'detail' => 'failed to create protection product', | ||
'error_message' => $e->getMessage(), | ||
'error_trace' => $e->getTraceAsString(), | ||
]; | ||
} | ||
return $this->getResponse()->representJson(json_encode($result)); | ||
} | ||
|
||
/** | ||
* Get full path for protection image on plugin folder | ||
* | ||
* @return bool|string|null | ||
*/ | ||
public function getProtectionImageFullPath() | ||
{ | ||
$fileId = 'AfterShip_Tracking::images/aftership_protection.png'; | ||
$params = [ | ||
'area' => 'frontend' | ||
]; | ||
$asset = $this->assetRepository->createAsset($fileId, $params); | ||
return $asset->getSourceFile(); | ||
} | ||
|
||
/** | ||
* Get protection product | ||
* | ||
* @return ProductInterface|void | ||
*/ | ||
protected function getProtectionProduct() | ||
{ | ||
try { | ||
return $this->productRepository->get(Constants::AFTERSHIP_PROTECTION_SKU); | ||
} catch (Exception $e) { | ||
return $this->createProtectionProduct(); | ||
} | ||
} | ||
|
||
/** | ||
* Copy protection image from plugin to media directory | ||
* | ||
* @param $sourcePath | ||
* @param $fileName | ||
* @return string | ||
*/ | ||
protected function copyImageToMediaDirectory($sourcePath, $fileName) | ||
{ | ||
$mediaDirectory = $this->directoryList->getPath(DirectoryList::MEDIA); | ||
$destinationPath = $mediaDirectory . '/' . $fileName; | ||
$this->file->cp($sourcePath, $destinationPath); | ||
return $destinationPath; | ||
} | ||
|
||
/** | ||
* Create protection product | ||
* | ||
* @return void | ||
*/ | ||
protected function createProtectionProduct() | ||
{ | ||
$productData = [ | ||
'sku' => Constants::AFTERSHIP_PROTECTION_SKU, | ||
'name' => 'AfterShip Protection', | ||
'description' => 'Shipping Protection provided by AfterShip', | ||
'price' => 0.01, | ||
'status' => Status::STATUS_ENABLED, | ||
'visibility' => Visibility::VISIBILITY_NOT_VISIBLE, | ||
'type_id' => Type::TYPE_VIRTUAL, | ||
'attribute_set_id' => 4, | ||
'tax_class_id' => 0, | ||
'website_ids' => $this->getAllSiteIds(), | ||
]; | ||
$product = $this->productInterfaceFactory->create(); | ||
$product->setData($productData); | ||
$product->setStockData([ | ||
'is_in_stock' => true, | ||
'backorders' => true, | ||
'qty' => 0, | ||
'use_config_backorders' => false, | ||
'use_config_manage_stock' => false | ||
]); | ||
$imagePath = $this->getProtectionImageFullPath(); | ||
$mediaPath = $this->copyImageToMediaDirectory($imagePath, 'aftership_protection.png'); | ||
if ($mediaPath) { | ||
$product->addImageToMediaGallery($mediaPath, ['image', 'thumbnail', 'small_image'], true, false); | ||
} | ||
$product->save(); | ||
return $product; | ||
} | ||
|
||
/** | ||
* Get all site ids | ||
* | ||
* @return array | ||
*/ | ||
protected function getAllSiteIds() | ||
{ | ||
$websites = $this->storeManager->getWebsites(); | ||
$siteIds = []; | ||
foreach ($websites as $website) { | ||
$siteIds[] = $website->getId(); | ||
} | ||
return $siteIds; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?xml version="1.0"?> | ||
<csp_whitelist xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Csp/etc/csp_whitelist.xsd"> | ||
<policies> | ||
<policy id="script-src"> | ||
<values> | ||
<value id="googleAjax" type="host">ajax.googleapis.com</value> | ||
<value id="googleFonts" type="host">fonts.googleapis.com</value> | ||
<value id="automizelyScriptCom" type="host">widgets.automizely.com</value> | ||
<value id="automizelyScriptIo" type="host">widgets.automizely.io</value> | ||
</values> | ||
</policy> | ||
|
||
<policy id="style-src"> | ||
<values> | ||
<value id="googleFontsStype" type="host">fonts.googleapis.com</value> | ||
<value id="automizelyStypeCom" type="host">widgets.automizely.com</value> | ||
<value id="automizelyStyleIo" type="host">widgets.automizely.io</value> | ||
</values> | ||
</policy> | ||
|
||
<policy id="img-src"> | ||
<values> | ||
<value id="automizelyImgCom" type="host">widgets.automizely.com</value> | ||
<value id="automizelyImgIo" type="host">widgets.automizely.io</value> | ||
</values> | ||
</policy> | ||
|
||
<policy id="connect-src"> | ||
<values> | ||
<value id="automizelyAPICom" type="host">api.automizely.com</value> | ||
<value id="automizelyAPIIo" type="host">api.automizely.io</value> | ||
</values> | ||
</policy> | ||
|
||
</policies> | ||
</csp_whitelist> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd"> | ||
<router id="standard"> | ||
<route id="aftership" frontName="aftership"> | ||
<module name="AfterShip_Tracking" /> | ||
</route> | ||
</router> | ||
</config> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
<?xml version="1.0"?> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> | ||
<module name="AfterShip_Tracking" setup_version="1.0.1" schema_version="1.0.0"> | ||
<module name="AfterShip_Tracking" setup_version="1.0.2" schema_version="1.0.0"> | ||
</module> | ||
</config> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.