diff --git a/locale.xml b/locale.xml index 800a309917b85cdef4e8d2cd3b07fc3cda915d9e..abe028a202a4e53edaa4e8cd4f9e8ed8582aa54b 100644 --- a/locale.xml +++ b/locale.xml @@ -110,6 +110,10 @@ <de><![CDATA[Sie können mit der Bestellung leider nicht fortfahren.]]></de> <en><![CDATA[Unfortunately you cannot continue with your order.]]></en> </locale> + <locale name="exception.no.shipping.selected"> + <de><![CDATA[Bitte wählen Sie eine Versandart aus.]]></de> + <en><![CDATA[Please select a shipping method.]]></en> + </locale> <locale name="shipping.mail.settings.title"> <de><![CDATA[Bestell Verhalten]]></de> <en><![CDATA[Order behavior]]></en> diff --git a/src/QUI/ERP/Shipping/Order/Shipping.php b/src/QUI/ERP/Shipping/Order/Shipping.php index 12e40fa27f59b3956fec68347a855b1f390ed5ad..2de6275fd4470c9add8e00967a9d508b9f4562ed 100644 --- a/src/QUI/ERP/Shipping/Order/Shipping.php +++ b/src/QUI/ERP/Shipping/Order/Shipping.php @@ -62,19 +62,7 @@ public function getBody() $Customer = $Order->getCustomer(); $Shipping = QUI\ERP\Shipping\Shipping::getInstance(); - $userShipping = $Shipping->getUserShipping($User); - - $shippingList = []; - - foreach ($userShipping as $ShippingEntry) { - $ShippingEntry->setOrder($Order); - - if ($ShippingEntry->isValid() - && $ShippingEntry->canUsedInOrder($Order) - && $ShippingEntry->canUsedBy($User)) { - $shippingList[] = $ShippingEntry; - } - } + $shippingList = $this->getValidShipping(); // debugging logger if (QUI\ERP\Shipping\Shipping::getInstance()->debuggingEnabled()) { @@ -166,7 +154,30 @@ public function validate() } + // if shipping are selectable and no shipping is selected + $shippingList = $this->getValidShipping(); + + if ($Shipping === null && \count($shippingList) === 1) { + try { + $Order->setShipping($shippingList[0]); + $Order->save(); + } catch (QUI\Exception $Exception) { + QUI\System\Log::addDebug($Exception->getMessage()); + } + + $Shipping = $Order->getShipping(); + } + + + if ($Shipping === null && !empty($shippingList)) { + throw new QUI\ERP\Order\Exception([ + 'quiqqer/shipping', + 'exception.no.shipping.selected' + ]); + } + // if no shipping exists, BUT order can be continued + // and if really no shipping is selectable if ($Shipping === null && $behavior === ShippingHandler::NO_RULE_FOUND_ORDER_CANCEL) { throw new QUI\ERP\Order\Exception([ @@ -196,6 +207,30 @@ public function validate() } } + /** + * @return array + */ + protected function getValidShipping() + { + $Order = $this->getOrder(); + $User = $Order->getCustomer(); + + $userShipping = QUI\ERP\Shipping\Shipping::getInstance()->getUserShipping($User); + $shippingList = []; + + foreach ($userShipping as $ShippingEntry) { + $ShippingEntry->setOrder($Order); + + if ($ShippingEntry->isValid() + && $ShippingEntry->canUsedInOrder($Order) + && $ShippingEntry->canUsedBy($User)) { + $shippingList[] = $ShippingEntry; + } + } + + return $shippingList; + } + /** * Save the shipping to the order *