Skip to content

Commit

Permalink
Merge pull request #1 from thomas-kl1/master
Browse files Browse the repository at this point in the history
Code cleanup + allow translation
  • Loading branch information
christophermallory committed Feb 3, 2021
2 parents 0d3f5f2 + 985f675 commit 1a4539a
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 95 deletions.
39 changes: 4 additions & 35 deletions ViewModel/Cart/ProgressBar.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@

use Magento\Checkout\Model\Session;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\DataObject;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Pricing\PriceCurrencyInterface;
use Magento\Framework\View\Element\Block\ArgumentInterface;
use Magento\Quote\Api\Data\CartInterface;
use Magento\Quote\Model\Quote;

class ProgressBar extends DataObject implements ArgumentInterface
class ProgressBar implements ArgumentInterface
{
/**
* System XML config path for ChrisMallory_FreeShippingBanner - Uses default checkout cart section
Expand Down Expand Up @@ -50,41 +49,15 @@ class ProgressBar extends DataObject implements ArgumentInterface
* @param ScopeConfigInterface $scopeConfig
* @param Session $session
* @param PriceCurrencyInterface $priceCurrency
* @param array $data
*/
public function __construct(
ScopeConfigInterface $scopeConfig,
Session $session,
PriceCurrencyInterface $priceCurrency,
array $data = []
PriceCurrencyInterface $priceCurrency
) {
$this->scopeConfig = $scopeConfig;
$this->session = $session;
$this->priceCurrency = $priceCurrency;
parent::__construct($data);
}

/**
* Check if free shipping countdown is enabled
*
* @return bool
*/
public function isEnabled(): bool
{
return (bool)$this->scopeConfig->getValue(self::CHECKOUT_CART_XML_CONFIG_PATH
. 'freeshipping_progress_enable');
}

/**
* Get Cart/Quote
*
* @return CartInterface|Quote
* @throws LocalizedException
* @throws NoSuchEntityException
*/
public function getQuote()
{
return $this->session->getQuote();
}

/**
Expand Down Expand Up @@ -121,9 +94,7 @@ public function getFreeShippingMethodMinValue(): float
*/
public function getCurrentTotal(): float
{
$quote = $this->session->getQuote();

return (float)$quote->getSubtotalWithDiscount();
return (float)$this->session->getQuote()->getSubtotalWithDiscount();
}

/**
Expand Down Expand Up @@ -156,9 +127,7 @@ public function isFreeShippingEligible(): bool
*/
public function getFreeShippingDifference(): float
{
$currentTotal = $this->getCurrentTotal();

return $this->getFreeShippingMinValue() - $currentTotal;
return $this->getFreeShippingMinValue() - $this->getCurrentTotal();
}

/**
Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
}
],
"minimum-stability": "dev",
"require": {},
"require": {
"magento/module-quote": "*",
"magento/module-checkout": "*"
},
"autoload": {
"files": [
"registration.php"
Expand Down
32 changes: 19 additions & 13 deletions etc/acl.xml
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
<?xml version="1.0" ?>
<!--
/**
* Copyright © Chris Mallory All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Acl/etc/acl.xsd">
<acl>
<resources>
<resource id="Magento_Backend::admin">
<resource id="Magento_Backend::stores">
<resource id="Magento_Backend::stores_settings">
<resource id="Magento_Config::config">
<resource id="ChrisMallory_FreeShippingProgressBar::config" title="Free Shipping Progress Bar"/>
</resource>
</resource>
</resource>
</resource>
</resources>
</acl>
<acl>
<resources>
<resource id="Magento_Backend::admin">
<resource id="Magento_Backend::stores">
<resource id="Magento_Backend::stores_settings">
<resource id="Magento_Config::config">
<resource id="ChrisMallory_FreeShippingProgressBar::config" title="Free Shipping Progress Bar"/>
</resource>
</resource>
</resource>
</resource>
</resources>
</acl>
</config>
6 changes: 6 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
<?xml version="1.0" ?>
<!--
/**
* Copyright © Chris Mallory All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<section id="checkout">
Expand Down
18 changes: 12 additions & 6 deletions etc/config.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
<?xml version="1.0" ?>
<!--
/**
* Copyright © Chris Mallory All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
<default>
<checkout>
<cart>
<default>
<checkout>
<cart>
<freeshipping_progress_enable>0</freeshipping_progress_enable>
<use_freeshipping_method_config>1</use_freeshipping_method_config>
</cart>
</checkout>
</default>
</cart>
</checkout>
</default>
</config>
15 changes: 12 additions & 3 deletions etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
<?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="ChrisMallory_FreeShippingProgressBar" setup_version="1.0.0">
<!--
/**
* Copyright © Chris Mallory All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="ChrisMallory_FreeShippingProgressBar">
<sequence>
<module name="Magento_Quote"/>
<module name="Magento_Checkout"/>
</sequence>
</module>
</config>
2 changes: 2 additions & 0 deletions registration.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* Copyright © Chris Mallory All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

use Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(ComponentRegistrar::MODULE, 'ChrisMallory_FreeShippingProgressBar', __DIR__);
12 changes: 11 additions & 1 deletion view/frontend/layout/checkout_cart_index.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Chris Mallory All rights reserved.
* See COPYING.txt for license details.
*/
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<head>
<css src="ChrisMallory_FreeShippingProgressBar::css/custom.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<referenceContainer name="cart.summary">
<block name="checkout.cart.freeshipping.progress.bar" before="checkout.cart.shipping" template="ChrisMallory_FreeShippingProgressBar::cart/freeshipping_progress_bar.phtml">
<block name="checkout.cart.freeshipping.progress.bar"
before="checkout.cart.shipping"
template="ChrisMallory_FreeShippingProgressBar::cart/freeshipping_progress_bar.phtml"
ifconfig="checkout/cart/freeshipping_progress_enable">
<arguments>
<argument name="viewModel" xsi:type="object">ChrisMallory\FreeShippingProgressBar\ViewModel\Cart\ProgressBar</argument>
</arguments>
Expand Down
82 changes: 46 additions & 36 deletions view/frontend/templates/cart/freeshipping_progress_bar.phtml
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
<?php
/**
* Copyright © Chris Mallory All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

use ChrisMallory\FreeShippingProgressBar\ViewModel\Cart\ProgressBar;
use Magento\Framework\View\Element\Template;
Expand All @@ -7,43 +12,48 @@ use Magento\Framework\View\Element\Template;
/** @var ProgressBar $viewModel */
$viewModel = $block->getData('viewModel');

$shippingLabel = 'FREE SHIPPING';

?>
<?php if ($viewModel->isEnabled()): ?>
<div class="freeship-progress">
<?php if (!$viewModel->isFreeShippingEligible()): ?>
<?php
$freeshippingProgress = $viewModel->getFreeShippingCompletionPercent();
$background = "background: linear-gradient(to right, #00b052 0%, #00b052 "
. $freeshippingProgress . "%, lightgreen);";
if ($freeshippingProgress > 90) {
$background = "background: linear-gradient(to right, #00b052 0%, #00b052 95%, lightgreen);";
}
?>
<p class="upsell">Add
<span class="freeship-price">
<?= /* @noEscape */ $viewModel->getFormattedPrice($viewModel->getFreeShippingDifference()) ?>
</span>
more to get
<span class="freeship-bold">FREE SHIPPING!</span>
</p>
<div class="freeship-progress-bar-wrapper">
<span class="min"><?= /* @noEscape */ $viewModel->getFormattedPrice(0, 0) ?></span>
<div class="freeship-progress-bar">
<div class="freeship-progress-bar-bg">
<div class="freeship-progress-bar-fill freeship-progress-bar-progress"
style="width: <?= /* @noEscape */ $viewModel->getFreeShippingCompletionPercent() ?>%;
<?= /* @noEscape */ $background ?>">
</div>
<div class="freeship-progress">
<?php if (!$viewModel->isFreeShippingEligible()): ?>
<?php
$freeshippingProgress = $viewModel->getFreeShippingCompletionPercent();
$background = "background: linear-gradient(to right, #00b052 0%, #00b052 "
. $freeshippingProgress . "%, lightgreen);";
if ($freeshippingProgress > 90) {
$background = "background: linear-gradient(to right, #00b052 0%, #00b052 95%, lightgreen);";
}
?>
<p class="upsell">
<?= /* @noEscape */ __(
'Add <span class="freeship-price">%1</span> more to get <span class="freeship-bold">%2!</span>',
$viewModel->getFormattedPrice($viewModel->getFreeShippingDifference()),
$shippingLabel
) ?>
</p>
<div class="freeship-progress-bar-wrapper">
<span class="min"><?= /* @noEscape */ $viewModel->getFormattedPrice(0, 0) ?></span>
<div class="freeship-progress-bar">
<div class="freeship-progress-bar-bg">
<div class="freeship-progress-bar-fill freeship-progress-bar-progress"
style="width: <?= /* @noEscape */ $viewModel->getFreeShippingCompletionPercent() ?>%;
<?= /* @noEscape */ $background ?>">
</div>
</div>
<span class="max">
<?= /* @noEscape */ $viewModel->getFormattedPrice($viewModel->getFreeShippingMinValue(), 0) ?>
</span>
</div>
<?php else: ?>
<div class="freeship-text">
<p>Your Order Is Eligible for <span class="freeship-bold">FREE SHIPPING!</span></p>
</div>
<?php endif; ?>
</div>
<?php endif ?>
<span class="max">
<?= /* @noEscape */ $viewModel->getFormattedPrice($viewModel->getFreeShippingMinValue(), 0) ?>
</span>
</div>
<?php else: ?>
<div class="freeship-text">
<p>
<?= /* @noEscape */ __(
'Your order is eligible for <span class="freeship-bold">%1!</span>',
$shippingLabel
) ?>
</p>
</div>
<?php endif; ?>
</div>

0 comments on commit 1a4539a

Please sign in to comment.