Skip to content
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
PISHPW-276: fixed missing customer and added logging
  • Loading branch information
uehler committed Nov 28, 2025
commit cdcfa9f14f128433b41488bfe83194eb9c16193c
72 changes: 65 additions & 7 deletions Components/CurrentCustomer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

namespace MollieShopware\Components;

use ArrayObject;
use Psr\Log\LoggerInterface;
use Shopware\Models\Customer\Customer;
use function sprintf;

class CurrentCustomer
{
/** @var \Enlight_Components_Session_Namespace */
Expand All @@ -10,12 +15,17 @@ class CurrentCustomer
/** @var \Shopware\Components\Model\ModelManager */
protected $modelManager;

/** @var LoggerInterface */
private $logger;

public function __construct(
\Enlight_Components_Session_Namespace $session,
\Shopware\Components\Model\ModelManager $modelManager
\Shopware\Components\Model\ModelManager $modelManager,
LoggerInterface $logger
) {
$this->session = $session;
$this->modelManager = $modelManager;
$this->logger = $logger;
}

/**
Expand All @@ -25,7 +35,46 @@ public function __construct(
*/
public function getCurrentId()
{
return !empty($this->session->sUserId) ? $this->session->sUserId : $this->session->offsetGet('auto-user');
/** @var null|numeric-string $customerId */
$customerId = $this->session->offsetGet('sUserId');
if (!empty($customerId)) {
return (int)$customerId;
}

$this->logger->error('sUserId not set in session');

/** @var null|int $customerId */
$customerId = $this->session->offsetGet('auto-user');
if (!empty($customerId)) {
return (int)$customerId;
}

$this->logger->error('auto-user not set in session');

/** @var null|ArrayObject $sOrderVariables */
$sOrderVariables = $this->session->offsetGet('sOrderVariables');
if (!($sOrderVariables instanceof ArrayObject)) {
$this->logger->error('sOrderVariables not set in session');

return 0;
}

/** @var null|array $sUserData */
$sUserData = $sOrderVariables->offsetGet('sUserData');
if ($sUserData === null) {
$this->logger->error('sUserData not set in session');

return 0;
}

$customerId = isset($sUserData['additional']['user']['id']) ? $sUserData['additional']['user']['id'] : 0;
if (empty($customerId)) {
$this->logger->error('sUserData does not contain a user id');

return 0;
}

return (int)$customerId;
}

/**
Expand All @@ -35,17 +84,26 @@ public function getCurrentId()
*/
public function getCurrent()
{
$userId = $this->getCurrentId();
/** @var int $customerId */
$customerId = $this->getCurrentId();

if ($customerId === 0) {
$this->logger->error('no customer id found');

if (empty($userId)) {
return null;
}

/** @var \Shopware\Models\Customer\Customer $customer */
/** @var null|\Shopware\Models\Customer\Customer $customer */
$customer = $this->modelManager->getRepository(
\Shopware\Models\Customer\Customer::class
)->find($userId);
)->find($customerId);

if ($customer instanceof Customer) {
return $customer;
}

$this->logger->error(sprintf('customer with id "%d" not found', $customerId));

return $customer;
return null;
}
}
6 changes: 5 additions & 1 deletion Components/Order/OrderCancellation.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,11 @@ public function cancelAndRestoreByOrder($swOrder)

# it's important to restore the order before the placed order is cancelled
# otherwise the original quantity of the line items can't be restored
$currentCustomer = new CurrentCustomer(Shopware()->Session(), Shopware()->Models());
$currentCustomer = new CurrentCustomer(
Shopware()->Session(),
Shopware()->Models(),
Shopware()->Container()->get('mollie_shopware.components.logger')
);
if ((int)$currentCustomer->getCurrentId() === (int)$swOrder->getCustomer()->getId()) {
$this->restoreCartFromOrder($swOrder);
}
Expand Down
6 changes: 5 additions & 1 deletion Facades/CheckoutSession/CheckoutSessionFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,11 @@ public function startCheckoutSession($basketUserId, $paymentShortName, $basketSi
*/
private function buildTransaction($basketSignature, $currency)
{
$currentCustomerClass = new CurrentCustomer(Shopware()->Session(), Shopware()->Models());
$currentCustomerClass = new CurrentCustomer(
Shopware()->Session(),
Shopware()->Models(),
Shopware()->Container()->get('mollie_shopware.components.logger')
);
$customer = $currentCustomerClass->getCurrent();

$locale = $this->localeFinder->getPaymentLocale(Shopware()->Shop()->getLocale()->getLocale());
Expand Down
3 changes: 2 additions & 1 deletion Resources/services/components/mixed.xml
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@
<service id="mollie_shopware.customer" class="MollieShopware\Components\CurrentCustomer" public="true">
<argument type="service" id="session"/>
<argument type="service" id="models"/>
<argument type="service" id="mollie_shopware.components.logger"/>
</service>

<service id="mollie_shopware.components.basket_snapshot.basket_snapshot"
Expand Down Expand Up @@ -209,4 +210,4 @@
</service>

</services>
</container>
</container>