Skip to content

Commit

Permalink
Update Self Hosted Module (#28)
Browse files Browse the repository at this point in the history
* Update Self-Hosted Checkout Module.

* Update Self-Hosted Checkout Module.

---------

Co-authored-by: MykolaMalovanets <[email protected]>
  • Loading branch information
nmalevanec and MykolaMalovanets committed Nov 23, 2023
1 parent 1cc352d commit c4caa2a
Show file tree
Hide file tree
Showing 16 changed files with 347 additions and 69 deletions.
1 change: 1 addition & 0 deletions Checkout/Api/Platform/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ public static function setCoupon(
}
$quote->setTotalsCollectedFlag(false);
$quote->setCouponCode($isCodeLengthValid ? $requestBody->couponCode : '')->collectTotals()->save();
/** @var Bold_Checkout_Model_Config $config */
$config = Mage::getSingleton(Bold_Checkout_Model_Config::RESOURCE);
$validateCouponCodes = $config->getValidateCouponCodes((int)$quote->getStore()->getWebsiteId());
if ($validateCouponCodes && (!$isCodeLengthValid || $requestBody->couponCode !== $quote->getCouponCode())) {
Expand Down
23 changes: 12 additions & 11 deletions Checkout/Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class Bold_Checkout_Model_Config
const VALUE_TYPE_STANDARD = 0;
const VALUE_TYPE_PARALLEL = 1;
const VALUE_TYPE_SELF = 2;
const VALUE_TYPE_SELF_REACT = 3;

/**
* Check if bold functionality enabled.
Expand Down Expand Up @@ -159,17 +160,6 @@ public function getWeightConversionRate($websiteId)
return (float)Mage::app()->getWebsite($websiteId)->getConfig(self::PATH_WEIGHT_CONVERSION_RATE) ?: 1000;
}

/**
* Get configured weight unit.
*
* @param int $websiteId
* @return string
*/
public function getWeightUnit($websiteId)
{
return Mage::app()->getWebsite($websiteId)->getConfig(self::PATH_WEIGHT_UNIT) ?: 'kg';
}

/**
* Get Bold API url.
*
Expand Down Expand Up @@ -247,6 +237,17 @@ public function isCheckoutTypeSelfHosted($websiteId)
return (int)Mage::app()->getWebsite($websiteId)->getConfig(self::PATH_TYPE) === self::VALUE_TYPE_SELF;
}

/**
* Retrieve self-hosted (react app) is enabled config flag.
*
* @param int $websiteId
* @return bool
*/
public function isCheckoutTypeSelfHostedReactApp($websiteId)
{
return (int)Mage::app()->getWebsite($websiteId)->getConfig(self::PATH_TYPE) === self::VALUE_TYPE_SELF_REACT;
}

/**
* Get configured LiFE elements.
*
Expand Down
22 changes: 16 additions & 6 deletions Checkout/Model/System/Config/Source/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,22 @@ class Bold_Checkout_Model_System_Config_Source_Type
public function toOptionArray()
{
return [
Bold_Checkout_Model_Config::VALUE_TYPE_STANDARD =>
Mage::helper('core')->__('Standard'),
Bold_Checkout_Model_Config::VALUE_TYPE_PARALLEL =>
Mage::helper('core')->__('Parallel'),
Bold_Checkout_Model_Config::VALUE_TYPE_SELF =>
Mage::helper('core')->__('Self-Hosted'),
[
'value' => Bold_Checkout_Model_Config::VALUE_TYPE_STANDARD,
'label' => __('Standard'),
],
[
'value' => Bold_Checkout_Model_Config::VALUE_TYPE_PARALLEL,
'label' => __('Parallel'),
],
[
'value' => Bold_Checkout_Model_Config::VALUE_TYPE_SELF,
'label' => __('Self-Hosted (Magento storefront)'),
],
[
'value' => Bold_Checkout_Model_Config::VALUE_TYPE_SELF_REACT,
'label' => __('Self-Hosted (React application)'),
],
];
}
}
1 change: 0 additions & 1 deletion Checkout/PlatformClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class Bold_Checkout_PlatformClient
* @param int $websiteId
* @param string|null $data
* @return string
* @throws Mage_Core_Exception
*/
public static function call($method, $url, $websiteId, $data = null)
{
Expand Down
73 changes: 53 additions & 20 deletions CheckoutSelfHosted/Block/Checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
*/
class Bold_CheckoutSelfHosted_Block_Checkout extends Mage_Core_Block_Template
{
const UPLOAD_DIR = 'bold/checkout/template';

/**
* @var null | stdClass
*/
private $orderData = null;

/**
* Get order data.
* Get Bold order data.
*
* @return stdClass
* @throws Exception
Expand All @@ -34,38 +36,82 @@ public function getOrderData()
return $this->orderData;
}

/**
* Retrieve public order id from bold checkout data.
*
* @return string
*/
public function getPublicOrderId()
{
$session = Mage::getSingleton('checkout/session');
return isset($session->getBoldCheckoutData()['data']['public_order_id'])
? $session->getBoldCheckoutData()['data']['public_order_id']
: '';
}

/**
* Retrieve template script URL.
*
* @return string
*/
public function getCheckoutTemplateScriptUrl()
{
$checkoutSession = Mage::getSingleton('checkout/session');
$websiteId = (int)$checkoutSession->getQuote()->getStore()->getWebsiteId();
/** @var Bold_CheckoutSelfHosted_Model_Config $config */
$config = Mage::getSingleton(Bold_CheckoutSelfHosted_Model_Config::RESOURCE);
$templateUrl = $config->getCheckoutTemplateUrl($websiteId);
$templateType = $config->getCheckoutTemplateType($websiteId);
if ($templateUrl) {
return rtrim($templateUrl, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $templateType . '.js';
}
$templateFile = $config->getCheckoutTemplateFile($websiteId);
if ($templateFile) {
$mediaUrl = $checkoutSession->getQuote()->getStore()->getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA);
return $mediaUrl . self::UPLOAD_DIR . DIRECTORY_SEPARATOR . $templateFile;
}
return $config->getViewFileUrl($websiteId);
}

/**
* Get shop identifier.
*
* @return string
* @throws Mage_Core_Exception
*/
public function getShopIdentifier()
{
$websiteId = Mage::app()->getWebsite()->getId();
return Mage::getModel(Bold_Checkout_Model_Config::RESOURCE)->getShopIdentifier($websiteId);
/** @var Bold_Checkout_Model_Config $config */
$config = Mage::getModel(Bold_Checkout_Model_Config::RESOURCE);
return $config->getShopIdentifier($websiteId);
}

/**
* Get shop alias.
*
* @return string
* @throws Exception
*/
public function getShopAlias()
{
return $this->getOrderData()->data->initial_data->shop_name;
try {
return $this->getOrderData()->data->initial_data->shop_name;
} catch (Exception $e) {
return '';
}
}

/**
* Get custom domain.
*
* @return string
* @throws Exception
*/
public function getCustomDomain()
{
return $this->getOrderData()->data->initial_data->shop_name;
try {
return $this->getOrderData()->data->initial_data->shop_name;
} catch (Exception $e) {
return '';
}
}

/**
Expand Down Expand Up @@ -99,19 +145,6 @@ public function getLoginUrl()
return Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB, 'customer/account/login');
}

/**
* Get react app url.
*
* @return string
* @throws Mage_Core_Exception
*/
public function getTemplateUrl()
{
/** @var Bold_CheckoutSelfHosted_Model_Config $selfHostedConfig */
$selfHostedConfig = Mage::getSingleton(Bold_CheckoutSelfHosted_Model_Config::RESOURCE);
return rtrim($selfHostedConfig->getTemplateUrl(Mage::app()->getWebsite()->getId()), '/') . '/three_page.js';
}

/**
* Get store logo.
*
Expand Down
51 changes: 51 additions & 0 deletions CheckoutSelfHosted/Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ class Bold_CheckoutSelfHosted_Model_Config
{
const RESOURCE = 'bold_checkout_self_hosted/config';
const PATH_SELF_HOSTED_TEMPLATE_URL = 'checkout/bold_advanced/self_hosted_template_url';
const CONFIG_PATH_TEMPLATE_URL = 'checkout/bold_advanced/template_url';
const CONFIG_PATH_TEMPLATE_TYPE = 'checkout/bold_advanced/template_type';
const CONFIG_PATH_TEMPLATE_FILE = 'checkout/bold_advanced/template_file';

/**
* Get react app template url.
Expand All @@ -19,4 +22,52 @@ public function getTemplateUrl($websiteId)
{
return Mage::app()->getWebsite($websiteId)->getConfig(self::PATH_SELF_HOSTED_TEMPLATE_URL);
}

/**
* Retrieve configured checkout template url.
*
* @param int $websiteId
* @return string|null
* @throws Mage_Core_Exception
*/
public function getCheckoutTemplateUrl($websiteId)
{
return Mage::app()->getWebsite($websiteId)->getConfig(self::CONFIG_PATH_TEMPLATE_URL);
}

/**
* Retrieve configured checkout template type (one-page, three-pages).
*
* @param int $websiteId
* @return string|null
* @throws Mage_Core_Exception
*/
public function getCheckoutTemplateType($websiteId)
{
return Mage::app()->getWebsite($websiteId)->getConfig(self::CONFIG_PATH_TEMPLATE_TYPE);
}

/**
* Retrieve custom checkout template file path save in configuration.
*
* @param int $websiteId
* @return string|null
* @throws Mage_Core_Exception
*/
public function getCheckoutTemplateFile($websiteId)
{
return Mage::app()->getWebsite($websiteId)->getConfig(self::CONFIG_PATH_TEMPLATE_FILE);
}

/**
* Retrieve default one-page or three-pages template file url.
*
* @param int $websiteId
* @return string
*/
public function getViewFileUrl($websiteId)
{
return Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) . 'bold/checkout/template/'
. $this->getCheckoutTemplateType($websiteId) . '.js';
}
}
15 changes: 15 additions & 0 deletions CheckoutSelfHosted/Model/System/Config/Backend/File.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

/**
* "Bold Checkout Template File" system configuration backend class.
*/
class Bold_CheckoutSelfHosted_Model_System_Config_Backend_File extends Mage_Adminhtml_Model_System_Config_Backend_File
{
/**
* @inheritDoc
*/
protected function _getAllowedExtensions()
{
return ['js'];
}
}
22 changes: 22 additions & 0 deletions CheckoutSelfHosted/Model/System/Config/Source/Template/Type.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/**
* "Bold Checkout Template Type" system configuration source class.
*/
class Bold_CheckoutSelfHosted_Model_System_Config_Source_Template_Type
{
const VALUE_TYPE_ONE_PAGE = 'one_page';
const VALUE_TYPE_THREE_PAGE = 'three_page';

/**
* Get template types options.
*/
public function toOptionArray()
{
$helper = Mage::helper('core');
return [
['value' => self::VALUE_TYPE_THREE_PAGE, 'label' => $helper->__('Three Page (Default)')],
['value' => self::VALUE_TYPE_ONE_PAGE, 'label' => $helper->__('One Page')],
];
}
}
2 changes: 1 addition & 1 deletion CheckoutSelfHosted/Observer/CheckoutObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function beforeCheckout(Varien_Event_Observer $event)
$websiteId = $quote->getStore()->getWebsiteId();
/** @var Bold_Checkout_Model_Config $selfHostedConfig */
$selfHostedConfig = Mage::getSingleton(Bold_Checkout_Model_Config::RESOURCE);
if (!$selfHostedConfig->isCheckoutTypeSelfHosted($websiteId)) {
if (!$selfHostedConfig->isCheckoutTypeSelfHostedReactApp($websiteId)) {
parent::beforeCheckout($event);
return;
}
Expand Down
Loading

0 comments on commit c4caa2a

Please sign in to comment.