Inheritance: extends Form
 public function testForm()
 {
     $controller = new Product_Controller($this->objFromFixture("Product", "socks"));
     $form = new AddProductForm($controller);
     $form->setMaximumQuantity(10);
     $this->markTestIncomplete("test can't go over max quantity");
     $data = array('Quantity' => 4);
     $form->addtocart($data, $form);
     $this->markTestIncomplete('check quantity');
 }
Ejemplo n.º 2
0
 public function __construct($controller, $name = "VariationForm")
 {
     parent::__construct($controller, $name);
     $product = $controller->data();
     $farray = array();
     $requiredfields = array();
     $attributes = $product->VariationAttributeTypes();
     foreach ($attributes as $attribute) {
         $farray[] = $attribute->getDropDownField("Choose {$attribute->Label} ...", $product->possibleValuesForAttributeType($attribute));
         $requiredfields[] = "ProductAttributes[{$attribute->ID}]";
     }
     $fields = new FieldList($farray);
     if (self::$include_json) {
         $vararray = array();
         if ($vars = $product->Variations()) {
             foreach ($vars as $var) {
                 $vararray[$var->ID] = $var->AttributeValues()->map('ID', 'ID');
             }
         }
         $fields->push(new HiddenField('VariationOptions', 'VariationOptions', json_encode($vararray)));
     }
     $fields->merge($this->Fields());
     $this->setFields($fields);
     $requiredfields[] = 'Quantity';
     $this->setValidator(new VariationFormValidator($requiredfields));
     $this->extend('updateVariationForm');
 }
Ejemplo n.º 3
0
 public function __construct($controller, $name = "VariationForm")
 {
     parent::__construct($controller, $name);
     $product = $controller->data();
     $farray = array();
     $requiredfields = array();
     $attributes = $product->VariationAttributeTypes();
     foreach ($attributes as $attribute) {
         $attributeDropdown = $attribute->getDropDownField(_t('VariationForm.ChooseAttribute', "Choose {attribute} …", '', array('attribute' => $attribute->Label)), $product->possibleValuesForAttributeType($attribute));
         if ($attributeDropdown) {
             $farray[] = $attributeDropdown;
             $requiredfields[] = "ProductAttributes[{$attribute->ID}]";
         }
     }
     $fields = FieldList::create($farray);
     if (self::$include_json) {
         $vararray = array();
         $query = $query2 = new SQLQuery();
         $query->setSelect('ID')->setFrom('ProductVariation')->addWhere(array('ProductID' => $product->ID));
         if (!Product::config()->allow_zero_price) {
             $query->addWhere('"Price" > 0');
         }
         foreach ($query->execute()->column('ID') as $variationID) {
             $query2->setSelect('ProductAttributeValueID')->setFrom('ProductVariation_AttributeValues')->setWhere(array('ProductVariationID' => $variationID));
             $vararray[$variationID] = $query2->execute()->keyedColumn();
         }
         $fields->push(HiddenField::create('VariationOptions', 'VariationOptions', json_encode($vararray)));
     }
     $fields->merge($this->Fields());
     $this->setFields($fields);
     $requiredfields[] = 'Quantity';
     $this->setValidator(VariationFormValidator::create($requiredfields));
     $this->extend('updateVariationForm');
 }
 /**
  * Add one of an item to a cart (Product Page)
  *
  * @see the addtocart function within AddProductForm class
  * @param SS_HTTPRequest $request
  * @param AjaxHTTPResponse $response
  * @param Buyable $buyable [optional]
  * @param int $quantity [optional]
  * @param AddProductForm $form [optional]
  */
 public function updateAddProductFormResponse(&$request, &$response, $buyable, $quantity, $form)
 {
     if ($request->isAjax()) {
         if (!$response) {
             $response = $this->owner->getController()->getResponse();
         }
         $response->removeHeader('Content-Type');
         $response->addHeader('Content-Type', 'application/json; charset=utf-8');
         $shoppingcart = ShoppingCart::curr();
         $shoppingcart->calculate();
         // recalculate the shopping cart
         $data = array('id' => (string) $buyable->ID, 'internalItemID' => $buyable->InternalItemID, 'title' => $buyable->Title, 'url' => $buyable->URLSegment, 'categories' => $buyable->getCategories()->column('Title'), 'addLink' => $buyable->addLink(), 'removeLink' => $buyable->removeLink(), 'removeallLink' => $buyable->removeallLink(), 'setquantityLink' => $buyable->Item()->setquantityLink(), 'message' => array('content' => $form->Message(), 'type' => $form->MessageType()));
         $form->clearMessage();
         // include totals if required
         if ($shoppingcart) {
             $data['subTotal'] = $shoppingcart->SubTotal();
             $data['grandTotal'] = $shoppingcart->GrandTotal();
         }
         $this->owner->extend('updateAddProductFormResponseShopJsonResponse', $data, $request, $response, $buyable, $quantity, $form);
         $response->setBody(json_encode($data));
     }
 }
 /**
  * @param SS_HTTPRequest $request
  * @param AjaxHTTPResponse $response
  * @param AddProductForm $form [optional]
  */
 public function updateCartFormResponse(&$request, &$response, $form = null)
 {
     if ($request->isAjax()) {
         if (!$response) {
             $response = $this->owner->getAjaxResponse();
         }
         $this->setupRenderContexts($response);
         if (self::config()->show_ajax_messages) {
             $response->triggerEvent('statusmessage', array('content' => $form->Message(), 'type' => $form->MessageType()));
             $form->clearMessage();
         }
         // Because ShoppingCart::current() calculates the order once and
         // then remembers the total, and that was called BEFORE the product
         // was added, we need to recalculate again here. Under non-ajax
         // requests the redirect eliminates the need for this but under
         // ajax the total lags behind the subtotal without this.
         ShoppingCart::curr()->calculate();
         $response->pushRegion('CartFormAjax', $this->owner, array('Editable' => true));
     }
 }