Skip to content
Open
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
28 changes: 6 additions & 22 deletions core/components/minishop3/src/Controllers/Delivery/Delivery.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use MiniShop3\Model\msDelivery;
use MiniShop3\Model\msOrder;
use MiniShop3\Services\Order\OrderCostEngine;
use MiniShop3\Services\Order\OrderService;
use MODX\Revolution\modX;

/**
Expand Down Expand Up @@ -65,7 +66,7 @@ public function __construct(MiniShop3 $ms3, array $config = [])
* Calculate delivery cost (default implementation)
*
* Standard calculation logic includes:
* - Cost by weight (weight_price * order weight)
* - Cost by weight (weight_price * weight of the order products)
* - Free delivery when threshold exceeded (free_delivery_amount)
* - Fixed cost or percentage of order amount
*
Expand All @@ -79,32 +80,15 @@ public function __construct(MiniShop3 $ms3, array $config = [])
*/
public function getCost(msOrder $order, msDelivery $delivery, float $cost): float
{
// Get cart data for weight calculation
$cart = [
'total_weight' => 0,
'total_cost' => 0
];

// Load controllers if not loaded yet
if (empty($this->ms3->cart)) {
$this->ms3->services->load($this->ms3->config['ctx'] ?? 'web');
}

// Get cart data if controller is available
if (!empty($this->ms3->cart)) {
$response = $this->ms3->cart->status();
if ($response['success']) {
$cart = $response['data'];
}
}

$cartWeight = (float) ($cart['total_weight'] ?? 0);
// Weight of the order's own products (per-unit weight × count). The cart controller
// is not used: outside a storefront session it has no customer token.
$orderWeight = OrderService::aggregateProductsTotals($order->getMany('Products') ?? [])['weight'];

return OrderCostEngine::calculateDefaultDeliveryCost(
$this->modx,
$delivery,
$cost,
$cartWeight
$orderWeight
);
}

Expand Down
91 changes: 91 additions & 0 deletions core/components/minishop3/tests/DeliveryWeightCostTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

/**
* Default delivery cost takes weight from the order products (weight_price × Σ weight × count).
*
* Run: php tests/DeliveryWeightCostTest.php
*/

declare(strict_types=1);

require __DIR__ . '/stubs/ModxStub.php';
require __DIR__ . '/stubs/XpdoStub.php';
require __DIR__ . '/../vendor/autoload.php';

use MiniShop3\Controllers\Delivery\DefaultDelivery;
use MiniShop3\MiniShop3;
use MiniShop3\Model\msDelivery;
use MiniShop3\Model\msOrder;
use MiniShop3\Model\msOrderProduct;
use MODX\Revolution\modX;

$fail = static function (string $message): never {
fwrite(STDERR, "FAIL: {$message}\n");
exit(1);
};

$assertFloat = static function (float $expected, float $actual, string $case) use ($fail): void {
if (abs($expected - $actual) > 0.000001) {
$fail($case . ': expected ' . $expected . ', got ' . $actual);
}
};

$ref = new ReflectionClass(MiniShop3::class);
/** @var MiniShop3 $ms3 */
$ms3 = $ref->newInstanceWithoutConstructor();
$ms3->modx = new modX();

$delivery = static function (array $fields): msDelivery {
return new class($fields) extends msDelivery {
public function __construct(private array $fields)
{
}

public function get($key, $format = null, $formatTemplate = null): mixed
{
return $this->fields[$key] ?? null;
}
};
};

$product = static function (float $weight, int $count): msOrderProduct {
return new class(['weight' => $weight, 'count' => $count]) extends msOrderProduct {
public function __construct(private array $fields)
{
}

public function get($key, $format = null, $formatTemplate = null): mixed
{
return $this->fields[$key] ?? null;
}
};
};

$order = static function (array $products): msOrder {
return new class($products) extends msOrder {
public function __construct(private array $products)
{
}

public function getMany($alias, $criteria = null, $cacheFlag = true)
{
return $alias === 'Products' ? $this->products : [];
}
};
};

$controller = new DefaultDelivery($ms3);
$post = $delivery(['id' => 4, 'price' => '390', 'weight_price' => 60, 'free_delivery_amount' => 0]);

// 0.4 kg × 2 + 1 kg × 1 = 1.8 kg → 390 + 60 × 1.8
$assertFloat(498.0, $controller->getCost($order([$product(0.4, 2), $product(1.0, 1)]), $post, 2000.0), 'weight × count');
$assertFloat(390.0, $controller->getCost($order([]), $post, 0.0), 'empty order: base price only');

$courier = $delivery(['id' => 2, 'price' => '350', 'weight_price' => 60, 'free_delivery_amount' => 5000]);
$assertFloat(0.0, $controller->getCost($order([$product(3.0, 1)]), $courier, 6000.0), 'free delivery threshold wins over weight');
$assertFloat(530.0, $controller->getCost($order([$product(3.0, 1)]), $courier, 1000.0), 'below threshold: price + weight');

$percent = $delivery(['id' => 5, 'price' => '10%', 'weight_price' => 100, 'free_delivery_amount' => 0]);
$assertFloat(150.0, $controller->getCost($order([$product(0.5, 1)]), $percent, 1000.0), 'percent price + weight');

echo "OK DeliveryWeightCostTest\n";
Loading