Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release/1.16.0 #104

Merged
merged 4 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions Model/Api/Builder/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Paazl\CheckoutWidget\Model\Config;
use Paazl\CheckoutWidget\Model\ExtInfoHandler;
use Paazl\CheckoutWidget\Model\Handler\Item as ItemHandler;
use Paazl\CheckoutWidget\Model\System\Config\Source\DimensionsMetric;

/**
* Class Order
Expand Down Expand Up @@ -387,19 +388,33 @@ private function getProductDimemension(Item $item)
$dimensionArray = [];
$product = $item->getProduct();

switch ($this->scopeConfig->getDimensionsMetric()) {
case DimensionsMetric::METRIC_MM:
$k = 0.1;
break;
case DimensionsMetric::METRIC_CM:
$k = 1;
break;
case DimensionsMetric::METRIC_M:
$k = 100;
break;
default:
$k = 1;
}

if ($widthAttribute = $this->config->getProductAttributeWidth()) {
if ($width = $product->getData($widthAttribute)) {
if ($width = $product->getData($widthAttribute) * $k) {
$dimensionArray['width'] = (int)$width;
}
}

if ($heightAttribute = $this->config->getProductAttributeHeight()) {
if ($height = $product->getData($heightAttribute)) {
if ($height = $product->getData($heightAttribute) * $k) {
$dimensionArray['height'] = (int)$height;
}
}

if ($lengthAttribute = $this->config->getProductAttributeLength()) {
if ($lengthAttribute = $this->config->getProductAttributeLength() * $k) {
if ($length = $product->getData($lengthAttribute)) {
$dimensionArray['length'] = (int)$length;
}
Expand Down
96 changes: 92 additions & 4 deletions Model/Checkout/WidgetConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
use Paazl\CheckoutWidget\Helper\General as GeneralHelper;
use Paazl\CheckoutWidget\Model\Config;
use Paazl\CheckoutWidget\Model\Handler\Item as ItemHandler;
use Paazl\CheckoutWidget\Model\System\Config\Source\CalculateVolume;
use Paazl\CheckoutWidget\Model\System\Config\Source\DimensionsMetric;
use Paazl\CheckoutWidget\Model\TokenRetriever;

/**
Expand Down Expand Up @@ -118,12 +120,14 @@ public function getConfig()
}

$goods = [];

$widthAttribute = $this->scopeConfig->getProductAttributeWidth();
$heightAttribute = $this->scopeConfig->getProductAttributeHeight();
$lengthAttribute = $this->scopeConfig->getProductAttributeLength();
$useDimensions = $widthAttribute || $lengthAttribute || $heightAttribute;
foreach ($this->getQuote()->getAllItems() as $item) {
if ($item->getProductType() == 'simple') {
$product = $this->productRepository->getById($item->getProduct()->getId());
$goodsItem = [
"quantity" => $item->getParentItem()
? (int)($item->getParentItem()->getQty())
Expand All @@ -132,10 +136,28 @@ public function getConfig()
"price" => $this->itemHandler->getPriceValue($item)
];
if ($useDimensions) {
$product = $this->productRepository->getById($item->getProduct()->getId());
$goodsItem["length"] = (float)$product->getData($lengthAttribute);
$goodsItem["width"] = (float)$product->getData($widthAttribute);
$goodsItem["height"] = (float)$product->getData($heightAttribute);
switch ($this->scopeConfig->getDimensionsMetric()) {
case DimensionsMetric::METRIC_MM:
$k = 0.1;
break;
case DimensionsMetric::METRIC_CM:
$k = 1;
break;
case DimensionsMetric::METRIC_M:
$k = 100;
break;
default:
$k = 1;
}
$goodsItem["length"] =
(float)str_replace(',', '.', $product->getData($lengthAttribute)) * $k;
$goodsItem["width"] =
(float)str_replace(',', '.', $product->getData($widthAttribute)) * $k;
$goodsItem["height"] =
(float)str_replace(',', '.', $product->getData($heightAttribute)) * $k;
}
if ($this->scopeConfig->addVolume()) {
$goodsItem["volume"] = $this->getProductVolume($product);
}

if ($deliveryMatrixCode = $this->getProductDeliveryMatrix($item)) {
Expand Down Expand Up @@ -202,6 +224,10 @@ public function getConfig()
$config['shipmentParameters']['startMatrix'] = $this->getFreeShippingMatrixLetter();
}

if ($this->scopeConfig->addVolume()) {
$config['shipmentParameters']['totalVolume'] = $this->getTotalVolume($goods);
}

switch ($this->scopeConfig->getTotalPrice()) {
case "grand_total":
$totalPriceValue = (float) $shippingAddress->getGrandTotal() -
Expand Down Expand Up @@ -356,6 +382,21 @@ public function getTotalWeight($goods)
return $weight;
}

/**
* @return float
*/
public function getTotalVolume($goods): float
{
$volume = 0;
$quote = $this->getQuote();
foreach ($goods as $good) {
if (isset($good['volume']) && isset($good['quantity'])) {
$volume += ($good['volume'] * $good['quantity']);
}
}
return (float)$volume;
}

/**
* @return float
*/
Expand Down Expand Up @@ -490,4 +531,51 @@ protected function isFreeShippingEnabled()
{
return $this->scopeConfig->isFreeShippingEnabled($this->getQuote()->getStoreId());
}

/**
* @param $product
* @return float
*/
private function getProductVolume($product): float
{
switch ($this->scopeConfig->addVolume()) {
case CalculateVolume::CALCULATE_VOLUME_USE_ATTRIBUTE:
return $product->getData($this->scopeConfig->getProductAttributeVolume());
case CalculateVolume::CALCULATE_VOLUME_CALCULATE:
return $this->calculateVolumeByDimensions($product);
case CalculateVolume::CALCULATE_VOLUME_CALCULATE_IF_MISSED:
return $product->getData($this->scopeConfig->getProductAttributeVolume()) ?:
$this->calculateVolumeByDimensions($product);
}
}

/**
* Calculate volume in m^3 using dimensions in cm
*
* @param $product
* @return float
*/
private function calculateVolumeByDimensions($product): float
{
switch ($this->scopeConfig->getDimensionsMetric()) {
case DimensionsMetric::METRIC_MM:
$k = 0.000000001;
break;
case DimensionsMetric::METRIC_CM:
$k = 0.000001;
break;
case DimensionsMetric::METRIC_M:
$k = 1;
break;
default:
$k = 0.000001;
}
$widthAttribute = $this->scopeConfig->getProductAttributeWidth();
$heightAttribute = $this->scopeConfig->getProductAttributeHeight();
$lengthAttribute = $this->scopeConfig->getProductAttributeLength();
return (float)str_replace(',', '.', $product->getData($widthAttribute)) *
(float)str_replace(',', '.', $product->getData($heightAttribute)) *
(float)str_replace(',', '.', $product->getData($lengthAttribute)) *
$k;
}
}
30 changes: 30 additions & 0 deletions Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,16 @@ public function getCustomDescription($store = null)
return $this->getValue(self::API_CONFIG_PATH . '/custom_description', $store);
}

/**
* @param null|Store|int|string $store
*
* @return mixed
*/
public function getDimensionsMetric($store = null)
{
return $this->getValue(self::API_CONFIG_PATH . '/dimensions_metric', $store);
}

/**
* @param null|Store|int|string $store
*
Expand Down Expand Up @@ -409,6 +419,26 @@ public function getProductAttributeHeight($store = null)
return $this->getValue(self::API_CONFIG_PATH . '/height_attribute', $store);
}

/**
* @param null|Store|int|string $store
*
* @return mixed
*/
public function getProductAttributeVolume($store = null)
{
return $this->getValue(self::API_CONFIG_PATH . '/volume_attribute', $store);
}

/**
* @param null|Store|int|string $store
*
* @return mixed
*/
public function addVolume($store = null)
{
return $this->getValue(self::API_CONFIG_PATH . '/add_volume', $store);
}

/**
* @param null|Store|int|string $store
*
Expand Down
55 changes: 55 additions & 0 deletions Model/System/Config/Source/CalculateVolume.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* Copyright © Paazl. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Paazl\CheckoutWidget\Model\System\Config\Source;

use Magento\Framework\Data\OptionSourceInterface;

/**
* Class CalculateVolume
*/
class CalculateVolume implements OptionSourceInterface
{

public const CALCULATE_VOLUME_NO = 0;
public const CALCULATE_VOLUME_USE_ATTRIBUTE = 1;
public const CALCULATE_VOLUME_CALCULATE = 2;
public const CALCULATE_VOLUME_CALCULATE_IF_MISSED = 3;

/**
* @var array
*/
public $options;

/**
* @inheritDoc
*/
public function toOptionArray()
{
if (!$this->options) {
$this->options = [
[
'value' => self::CALCULATE_VOLUME_NO,
'label' => __('No')
],
[
'value' => self::CALCULATE_VOLUME_USE_ATTRIBUTE,
'label' => __('Yes, use volume attribute')
],
[
'value' => self::CALCULATE_VOLUME_CALCULATE,
'label' => __('Yes, always calculate using dimensions')
],
[
'value' => self::CALCULATE_VOLUME_CALCULATE_IF_MISSED,
'label' => __('Yes, calculate using dimensions if volume missed')
]
];
}

return $this->options;
}
}
50 changes: 50 additions & 0 deletions Model/System/Config/Source/DimensionsMetric.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* Copyright © Paazl. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Paazl\CheckoutWidget\Model\System\Config\Source;

use Magento\Framework\Data\OptionSourceInterface;

/**
* Class DimensionsMetric
*/
class DimensionsMetric implements OptionSourceInterface
{

public const METRIC_MM = 'mm';
public const METRIC_CM = 'cm';
public const METRIC_M = 'm';

/**
* @var array
*/
public $options;

/**
* @inheritDoc
*/
public function toOptionArray()
{
if (!$this->options) {
$this->options = [
[
'value' => self::METRIC_MM,
'label' => __('mm')
],
[
'value' => self::METRIC_CM,
'label' => __('cm')
],
[
'value' => self::METRIC_M,
'label' => __('m')
]
];
}

return $this->options;
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "paazl/magento2-checkout-widget",
"description": "Paazl checkoutWidget for Magento 2",
"type": "magento2-module",
"version": "1.15.1",
"version": "1.16.0",
"keywords": [
"Paazl",
"Magento 2",
Expand Down
Loading
Loading