Skip to content

Commit

Permalink
Merge pull request #50 from Paazl/1.4.0
Browse files Browse the repository at this point in the history
1.4.0
  • Loading branch information
Marvin-Magmodules authored Nov 4, 2020
2 parents 4b0a6c5 + 8e149f7 commit 6ab69a9
Show file tree
Hide file tree
Showing 24 changed files with 408 additions and 90 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
language: php

php:
- 5.6
- 7.0
- 7.1
- 7.2
- 7.3
- 7.4

sudo: false

Expand Down
12 changes: 11 additions & 1 deletion Model/Admin/Order/Create/LanguageProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ class LanguageProvider implements ConfigProviderInterface
*/
private $resolver;

private $allowedCodes = [
'en' => 'eng',
'nl' => 'nld',
'de' => 'deu',
'pl' => 'pol'
];

/**
* LanguageProvider constructor.
*
Expand All @@ -39,10 +46,13 @@ public function __construct(
*/
public function getConfig()
{
$languageCode = 'en';
$languageCode = 'eng';
if ($this->resolver->getLocale()) {
$locale = $this->resolver->getLocale();
$languageCode = explode('_', $locale)[0];
if (isset($this->allowedCodes[$languageCode])) {
$languageCode = $this->allowedCodes[$languageCode];
}
}

return [
Expand Down
13 changes: 11 additions & 2 deletions Model/Admin/Order/Create/WidgetConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
namespace Paazl\CheckoutWidget\Model\Admin\Order\Create;

use Magento\Backend\Model\Session\Quote as SessionQuote;
use Magento\Catalog\Model\ProductRepository;
use Magento\Checkout\Model\ConfigProviderInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Quote\Model\Quote;
use Magento\Quote\Model\Quote\Item\AbstractItem;
use Magento\Sales\Model\OrderFactory;
use Magento\Catalog\Model\ProductRepository;
use Magento\Framework\Exception\NoSuchEntityException;
use Paazl\CheckoutWidget\Helper\General as GeneralHelper;
use Paazl\CheckoutWidget\Model\Config;
use Paazl\CheckoutWidget\Model\TokenRetriever;
Expand Down Expand Up @@ -137,6 +137,7 @@ public function getConfig()
"loadPaazlBasedData" => true,
"loadCarrierBasedData" => true,
"availableTabs" => $this->getAvailableTabs(),
"headerTabType" => $this->getWidgetSectionToggle(),
"defaultTab" => $this->getDefaultTab(),
"style" => $this->getWidgetTheme(),
"nominatedDateEnabled" => $this->getNominatedDateEnabled(),
Expand Down Expand Up @@ -240,6 +241,14 @@ public function getAvailableTabs()
return $this->scopeConfig->getAvailableTabs($this->getQuote()->getStoreId());
}

/**
* @return mixed
*/
public function getWidgetSectionToggle()
{
return $this->scopeConfig->getWidgetSectionToggle($this->getQuote()->getStoreId());
}

/**
* @return mixed
*/
Expand Down
44 changes: 28 additions & 16 deletions Model/Api/Builder/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,10 @@ public function getCreateOrderData(OrderInterface $order)
$shippingAddress = $order->getBillingAddress();
}
$address = $this->parseAddress($shippingAddress);
$defaultHouseNumber = $this->config->getHouseNumberDefaultOption() ? 0 : '';

$result = [
'additionalInstruction' => $order->getCustomerNote(),
'additionalInstruction' => '',
'consignee' => [
'email' => $order->getCustomerEmail(),
'name' => $shippingAddress->getName(),
Expand All @@ -117,8 +118,8 @@ public function getCreateOrderData(OrderInterface $order)
'country' => $shippingAddress->getCountryId(),
'postalCode' => $shippingAddress->getPostcode(),
'province' => $shippingAddress->getRegionCode(),
'street' => $address['street'],
'houseNumber' => $address['houseNumber'],
'street' => $address['street'] ?? '',
'houseNumber' => $address['houseNumber'] ?? $defaultHouseNumber,
'houseNumberExtension' => $address['houseNumberExtension'] ?? '',
]
],
Expand Down Expand Up @@ -176,10 +177,19 @@ public function getCreateOrderData(OrderInterface $order)
* @param Address $shippingAddress
*
* @return array
* @throws LocalizedException
*/
public function parseAddress(Address $shippingAddress)
{
$street = implode(' ', $shippingAddress->getStreet());
$street = $shippingAddress->getStreet();
if ($this->config->housenumberExtensionOnThirdStreet()) {
return [
'street' => trim($street[0] ?? null),
'houseNumber' => trim(isset($street[1]) ? $street[1] : null),
'houseNumberExtension' => trim(isset($street[2]) ? $street[2] : null),
];
}
$street = implode(' ', $street);
$street = trim(str_replace("\t", ' ', $street));
$houseExtensionPattern = '(?<houseNumber>\d{1,5})[[:punct:]\-\/\s]*(?<houseNumberExtension>[^[:space:]]{1,2})?';
$streetPattern = '(?<street>.+)';
Expand All @@ -197,9 +207,9 @@ public function parseAddress(Address $shippingAddress)
}

return [
'street' => trim($matches['street'] ?? ''),
'houseNumber' => trim($matches['houseNumber'] ?? ''),
'houseNumberExtension' => trim($matches['houseNumberExtension'] ?? ''),
'street' => trim($matches['street'] ?? null),
'houseNumber' => trim($matches['houseNumber'] ?? null),
'houseNumberExtension' => trim($matches['houseNumberExtension'] ?? null),
];
}

Expand Down Expand Up @@ -252,15 +262,17 @@ public function parseAddress(Address $shippingAddress)
return $probably;
}

/**
* Fallback to full address on street and housenumber set to 0
* This is a new requirement from Paazl API.
*/
return [
'street' => $street,
'houseNumber' => 0,
'houseNumberExtension' => null,
];
if ($this->config->getHouseNumberDefaultOption()) {
return [
'street' => $street,
'houseNumber' => null,
'houseNumberExtension' => null,
];
} else {
throw new LocalizedException(
__('This order cannot be committed to Paazl, please make sure the address has a valid housenumber.')
);
}
}

/**
Expand Down
12 changes: 11 additions & 1 deletion Model/Checkout/LanguageProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ class LanguageProvider implements ConfigProviderInterface
*/
private $resolver;

private $allowedCodes = [
'en' => 'eng',
'nl' => 'nld',
'de' => 'deu',
'pl' => 'pol'
];

/**
* LanguageProvider constructor.
*
Expand All @@ -39,10 +46,13 @@ public function __construct(
*/
public function getConfig()
{
$languageCode = 'en';
$languageCode = 'eng';
if ($this->resolver->getLocale()) {
$locale = $this->resolver->getLocale();
$languageCode = explode('_', $locale)[0];
if (isset($this->allowedCodes[$languageCode])) {
$languageCode = $this->allowedCodes[$languageCode];
}
}

return [
Expand Down
14 changes: 11 additions & 3 deletions Model/Checkout/WidgetConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@

namespace Paazl\CheckoutWidget\Model\Checkout;

use Magento\Catalog\Model\ProductRepository;
use Magento\Checkout\Helper\Data;
use Magento\Checkout\Model\ConfigProviderInterface;
use Magento\Directory\Model\Currency;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Quote\Model\Quote;
use Magento\Quote\Model\Quote\Item\AbstractItem;
use Magento\Sales\Model\OrderFactory;
use Magento\Catalog\Model\ProductRepository;
use Magento\Framework\Exception\NoSuchEntityException;
use Paazl\CheckoutWidget\Helper\General as GeneralHelper;
use Paazl\CheckoutWidget\Model\Config;
use Paazl\CheckoutWidget\Model\Handler\Item as ItemHandler;
Expand Down Expand Up @@ -149,6 +148,7 @@ public function getConfig()
"loadPaazlBasedData" => true,
"loadCarrierBasedData" => true,
"availableTabs" => $this->getAvailableTabs(),
"headerTabType" => $this->getWidgetSectionToggle(),
"defaultTab" => $this->getDefaultTab(),
"style" => $this->getWidgetTheme(),
"nominatedDateEnabled" => $this->getNominatedDateEnabled(),
Expand Down Expand Up @@ -252,6 +252,14 @@ public function getAvailableTabs()
return $this->scopeConfig->getAvailableTabs();
}

/**
* @return mixed
*/
public function getWidgetSectionToggle()
{
return $this->scopeConfig->getWidgetSectionToggle();
}

/**
* @return mixed
*/
Expand Down
23 changes: 23 additions & 0 deletions Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,16 @@ public function getNominatedDateEnabled($store = null)
);
}

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

/**
* @param null|Store|int|string $store
*
Expand Down Expand Up @@ -310,6 +320,10 @@ public function housenumberOnSecondStreet($store = null)
*/
public function housenumberExtensionOnThirdStreet($store = null)
{
if (!$this->housenumberOnSecondStreet($store)) {
return false;
}

return (bool)$this->getValue(self::API_CONFIG_PATH . '/housenumber_extension_third_street', $store);
}

Expand Down Expand Up @@ -468,4 +482,13 @@ public function getInsuranceValue($store = null)
{
return abs((float)$this->getValue(self::API_CONFIG_PATH . '/insurance_value', $store));
}

/**
* @param null|Store|int|string $store
* @return bool
*/
public function getHouseNumberDefaultOption($store = null)
{
return !!$this->getValue(self::API_CONFIG_PATH . '/housenumber_default_value', $store);
}
}
3 changes: 3 additions & 0 deletions Model/ExtInfoHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ public function getInfoFromQuote(Quote $quote)
}

$info = $reference->getExtShippingInfo();
if (empty($info)) {
return null;
}

/** @var ShippingInfo $shippingInfo */
$shippingInfo = $this->shippingInfoFactory->create();
Expand Down
76 changes: 76 additions & 0 deletions Model/Quote/Totals/AppendShippingMethods.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
/**
* Copyright © 2019 Paazl. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Paazl\CheckoutWidget\Model\Quote\Totals;

use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Exception\StateException;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Api\Data\TotalsExtensionFactory;
use Magento\Quote\Api\Data\TotalsExtensionInterface;
use Magento\Quote\Api\Data\TotalsInterface;
use Magento\Quote\Api\ShippingMethodManagementInterface;

class AppendShippingMethods
{
/**
* @var ShippingMethodManagementInterface
*/
private $shippingMethodManagement;

/**
* @var TotalsExtensionFactory
*/
private $totalsExtensionFactory;

/**
* @var CartRepositoryInterface
*/
private $cartRepository;

/**
* AppendShippingMethods constructor.
*
* @param ShippingMethodManagementInterface $shippingMethodManagement
* @param TotalsExtensionFactory $totalsExtensionFactory
* @param CartRepositoryInterface $cartRepository
*/
public function __construct(
ShippingMethodManagementInterface $shippingMethodManagement,
TotalsExtensionFactory $totalsExtensionFactory,
CartRepositoryInterface $cartRepository
) {
$this->shippingMethodManagement = $shippingMethodManagement;
$this->totalsExtensionFactory = $totalsExtensionFactory;
$this->cartRepository = $cartRepository;
}

/**
* @param TotalsInterface $totals
* @param int $cartId
* @throws NoSuchEntityException
* @throws StateException
*/
public function append(TotalsInterface $totals, $cartId)
{
$quote = $this->cartRepository->get($cartId);
if (!$quote || $quote->getIsVirtual()) {
return;
}

// Adding the list of shipping methods to output
$methods = $this->shippingMethodManagement->getList($quote->getId());

/** @var TotalsExtensionInterface|null $extension */
$extension = $totals->getExtensionAttributes();
if (!$extension) {
$extension = $this->totalsExtensionFactory->create();
}
$extension->setShippingMethods($methods);
$totals->setExtensionAttributes($extension);
}
}
Loading

0 comments on commit 6ab69a9

Please sign in to comment.