Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented Nocosts service decorator to avoid costs per item when us…
…ing click&collect
- Loading branch information
Showing
2 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
lib/mshoplib/src/MShop/Service/Provider/Decorator/Nocosts.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
/** | ||
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 | ||
* @copyright Aimeos (aimeos.org), 2020 | ||
* @package MShop | ||
* @subpackage Service | ||
*/ | ||
|
||
|
||
namespace Aimeos\MShop\Service\Provider\Decorator; | ||
|
||
|
||
/** | ||
* Decorator for service providers setting costs to zero. | ||
* | ||
* @package MShop | ||
* @subpackage Service | ||
*/ | ||
class Nocosts | ||
extends \Aimeos\MShop\Service\Provider\Decorator\Base | ||
implements \Aimeos\MShop\Service\Provider\Decorator\Iface | ||
{ | ||
/** | ||
* Returns the costs per item as negative value to get no costs at all. | ||
* | ||
* @param \Aimeos\MShop\Order\Item\Base\Iface $basket Basket object | ||
* @return \Aimeos\MShop\Price\Item\Iface Price item containing the price, shipping, rebate | ||
*/ | ||
public function calcPrice( \Aimeos\MShop\Order\Item\Base\Iface $basket ) : \Aimeos\MShop\Price\Item\Iface | ||
{ | ||
$costs = 0; | ||
$price = $this->getProvider()->calcPrice( $basket ); | ||
|
||
foreach( $basket->getProducts() as $product ) { | ||
$costs += $product->getPrice()->getCosts() * $product->getQuantity(); | ||
} | ||
|
||
return $price->setCosts( -$costs ); | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
lib/mshoplib/tests/MShop/Service/Provider/Decorator/NocostsTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
/** | ||
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 | ||
* @copyright Aimeos (aimeos.org), 2020 | ||
*/ | ||
|
||
|
||
namespace Aimeos\MShop\Service\Provider\Decorator; | ||
|
||
|
||
class NocostsTest extends \PHPUnit\Framework\TestCase | ||
{ | ||
private $object; | ||
private $basket; | ||
private $context; | ||
private $servItem; | ||
private $mockProvider; | ||
|
||
|
||
protected function setUp() : void | ||
{ | ||
$this->context = \TestHelperMShop::getContext(); | ||
|
||
$servManager = \Aimeos\MShop::create( $this->context, 'service' ); | ||
$this->servItem = $servManager->createItem()->setId( -1 ); | ||
|
||
$this->mockProvider = $this->getMockBuilder( \Aimeos\MShop\Service\Provider\Decorator\Nocosts::class ) | ||
->disableOriginalConstructor()->getMock(); | ||
|
||
$orderManager = \Aimeos\MShop\Order\Manager\Factory::create( $this->context ); | ||
$this->basket = $orderManager->getSubManager( 'base' )->createItem()->off(); // remove plugins | ||
|
||
$this->object = new \Aimeos\MShop\Service\Provider\Decorator\Nocosts( $this->mockProvider, $this->context, $this->servItem ); | ||
} | ||
|
||
|
||
protected function tearDown() : void | ||
{ | ||
unset( $this->object, $this->basket, $this->mockProvider, $this->servItem, $this->context ); | ||
} | ||
|
||
|
||
public function testCalcPrice() | ||
{ | ||
$this->basket->addProduct( $this->getOrderProduct() ); | ||
$priceItem = \Aimeos\MShop::create( $this->context, 'price' )->createItem(); | ||
|
||
$this->mockProvider->expects( $this->once() )->method( 'calcPrice' )->will( $this->returnValue( $priceItem ) ); | ||
|
||
$price = $this->object->calcPrice( $this->basket ); | ||
$this->assertEquals( '0.00', $price->getValue() ); | ||
$this->assertEquals( '-5.00', $price->getCosts() ); | ||
} | ||
|
||
|
||
/** | ||
* Returns an order product item | ||
* | ||
* @return \Aimeos\MShop\Order\Item\Base\Product\Iface Order product item | ||
*/ | ||
protected function getOrderProduct() | ||
{ | ||
$priceManager = \Aimeos\MShop::create( $this->context, 'price' ); | ||
$productManager = \Aimeos\MShop::create( $this->context, 'product' ); | ||
$orderProductManager = \Aimeos\MShop::create( $this->context, 'order/base/product' ); | ||
|
||
$price = $priceManager->createItem(); | ||
$price->setValue( '20.00' )->setCosts( '5.00' ); | ||
|
||
$product = $productManager->createItem()->setId( '-1' ); | ||
$product->setCode( 'test' )->setType( 'test' ); | ||
|
||
$orderProduct = $orderProductManager->createItem(); | ||
$orderProduct->copyFrom( $product ); | ||
$orderProduct->setPrice( $price ); | ||
|
||
return $orderProduct; | ||
} | ||
} |