Permalink
Browse files

Merge remote-tracking branch 'refs/remotes/printmighty/develop' into …

…develop
  • Loading branch information...
2 parents 21704e4 + c264a89 commit dcc3bafcbaf82c2068e803c9a119f9898dc27157 @jedateach jedateach committed Jun 6, 2012
Showing with 38 additions and 18 deletions.
  1. +6 −6 code/ShoppingCart.php
  2. +21 −12 code/forms/AddProductForm.php
  3. +11 −0 code/products/Product.php
View
@@ -65,7 +65,6 @@ protected function findOrMake(){
}
$order->write();
Session::set(self::$cartid_session_name,$order->ID);
- $order->write();
return $this->order = $order;
}
@@ -88,7 +87,7 @@ public function add(Buyable $buyable,$quantity = 1,$filter = array()){
return false;
}
if(!$item->_brandnew){
- $item->Quantity += $quantity; //TODO: only increment if it is not a new item
+ $item->Quantity += $quantity;
}else{
$item->Quantity = $quantity;
}
@@ -160,7 +159,7 @@ private function findOrMakeItem(Buyable $buyable,$filter = array()){
}
$item = $this->get($buyable,$filter);
if(!$item){
- if(!$buyable->canPurchase()){
+ if(!$buyable->canPurchase(Member::currentUser())){
$this->message(sprintf(_t("ShoppingCart.CANNOTPURCHASE","This %s cannot be purchased."),strtolower($buyable->i18n_singular_name())),'bad');
//TODO: get more specific message
return false;
@@ -333,9 +332,6 @@ static function getInstance(){
/**
* Manipulate the cart via urls.
- *
- * @TODO handle filter stuff
- * @TODO introduce security token
*/
class ShoppingCart_Controller extends Controller{
@@ -360,6 +356,10 @@ static function set_direct_to_cart($direct = true){
self::$direct_to_cart_page = $direct;
}
+ static function get_direct_to_cart(){
+ return self::$direct_to_cart_page;
+ }
+
static function add_item_link(Buyable $buyable, $parameters = array()) {
return self::build_url("add", $buyable,$parameters);
}
@@ -2,31 +2,40 @@
class AddProductForm extends Form{
- function __construct($controller,$product){
-
+ function __construct($controller,$buyable){
$fields = new FieldSet(
new NumericField("Quantity",_t("AddProductForm.QUANTITY","Quantity"),1),
- new HiddenField("ProductID","",$product->ID),
- new LiteralField("Price", "<h2 class=\"price\">".$product->dbObject('Price')->Nice()."</h2>")
+ new HiddenField("BuyableID","",$buyable->ID),
+ new HiddenField("BuyableClass","",$buyable->ClassName),
+ new LiteralField("Price", "<h2 class=\"price\">".$buyable->dbObject('Price')->Nice()."</h2>")
);
$actions = new FieldSet(
new FormAction('addtocart',_t("AddProductForm.ADDTOCART",'Add to Cart'))
);
-
$validator = new RequiredFields(array(
'Quantity',
- 'ProductID'
+ 'BuyableID'
));
-
parent::__construct($controller,"AddProductForm",$fields,$actions,$validator);
}
function addtocart($data,$form){
- $product = DataObject::get_by_id('Product',(int) $data['ProductID']);
- $cart = ShoppingCart::getInstance();
- $cart->add($product,(int) $data['Quantity'],$data);
- $form->SessionMessage($cart->getMessage(),$cart->getMessageType());
- ShoppingCart_Controller::direct($cart->getMessageType());
+ if($buyable = $this->getBuyable()){
+ $cart = ShoppingCart::getInstance();
+ $quantity = isset($data['Quantity']) ? (int) $data['Quantity']: 1;
+ $cart->add($buyable,$quantity,$data);
+ if(!ShoppingCart_Controller::get_direct_to_cart()){
+ $form->SessionMessage($cart->getMessage(),$cart->getMessageType());
+ }
+ ShoppingCart_Controller::direct($cart->getMessageType());
+ }
+ }
+
+ protected function getBuyable(){
+ if($this->controller->dataRecord instanceof Buyable){
+ return $this->controller->dataRecord;
+ }
+ return DataObject::get_by_id('Product',(int) $this->request->postVar("BuyableID")); //TODO: get buyable
}
}
@@ -243,6 +243,13 @@ function Currency() {
return Payment::site_currency();
}
}
+
+ function Link(){
+ $link = parent::Link();
+ $this->extend('updateLink',$link);
+ return $link;
+ }
+
//passing on shopping cart links ...is this necessary?? ...why not just pass the cart?
function addLink() {
return ShoppingCart_Controller::add_item_link($this);
@@ -271,6 +278,10 @@ class Product_Controller extends Page_Controller {
static $allowed_actions = array();
+ function AddProductForm(){
+ return new AddProductForm($this,$this->dataRecord);
+ }
+
}
class Product_Image extends Image {

0 comments on commit dcc3baf

Please sign in to comment.