Skip to content

Instantly share code, notes, and snippets.

@inspiran
Created March 13, 2012 23:38
Show Gist options
  • Save inspiran/2032697 to your computer and use it in GitHub Desktop.
Save inspiran/2032697 to your computer and use it in GitHub Desktop.
<?php
/**
* (c) 2011-2012 Vespolina Project http://www.vespolina-project.org
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Vespolina\CartBundle\Pricing;
use Doctrine\Common\Collections\ArrayCollection;
use Vespolina\CartBundle\Model\CartableItemInterface;
use Vespolina\CartBundle\Model\CartItemInterface;
use Vespolina\CartBundle\Pricing\CartableItemPricingHandlerInterface;
/**
* @author Daniel Kucharski <daniel@xerias.be>
* @author Richard Shank <develop@zestic.com>
*/
class DefaultCartableItemPricingHandler implements CartableItemPricingHandlerInterface
{
protected $metadata;
public function __construct()
{
$this->loadMetadata();
}
public function determineCartItemPrices(CartableItemInterface $cartableItem, CartItemInterface $cartItem, $pricingContext)
{
$unitPrice = $cartableItem->getPricing($this->metadata['unitPrice']);
$upcharge = 0;
foreach($cartItem->getOptions() as $type => $value) {
if ($productOption = $cartItem->getCartableItem()->getOptionSet(array($type => $value))) {
$upcharge += $productOption->getUpcharge();
}
}
$pricingContext['upcharge'] = $upcharge;
$pricingContext['total'] = ($cartItem->getQuantity() * $unitPrice) + $upcharge;
}
protected function loadMetadata()
{
$this->metadata = new ArrayCollection();
$this->metadata->set('unitPrice', 'unitPrice'); //For now we map it 1:1
$this->metadata->set('totalPrice', 'totalPrice');
}
public function getMetadata()
{
return $this->metadata;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment