Пример #1
0
 /**
  * Try checking out an order without specifying a payment gateway
  */
 function testCheckoutWithoutPaymentGateway()
 {
     $productA = $this->objFromFixture('Product', 'productA');
     $this->loginAs('admin');
     $productA->doPublish();
     $this->logOut();
     $buyer = $this->objFromFixture('Customer', 'buyer');
     $this->assertEquals(1, $buyer->Orders()->Count());
     $this->loginAs($this->objFromFixture('Customer', 'buyer'));
     $productALink = $productA->Link();
     $this->get(Director::makeRelative($productALink));
     $this->submitForm('ProductForm_ProductForm', null, array('Quantity' => 1));
     $order = Cart::get_current_order();
     $items = $order->Items();
     $this->assertEquals(1, $items->Count());
     $checkoutPage = DataObject::get_one('CheckoutPage');
     $this->get(Director::makeRelative($checkoutPage->Link()));
     //Submit the form without restrictions on what can be POST'd
     $data = $this->getFormData('OrderForm_OrderForm');
     $data['PaymentMethod'] = '';
     $this->post(Director::absoluteURL('/checkout/OrderForm'), $data);
     $this->assertEquals(1, $buyer->Orders()->Count());
 }
Пример #2
0
 /**
  * Validate this field, check that the current {@link Item} is in the current 
  * {@Link Order} and is valid for adding to the cart.
  * 
  * @see FormField::validate()
  * @return Boolean
  */
 function validate($validator)
 {
     $valid = true;
     $item = $this->Item();
     $currentOrder = Cart::get_current_order();
     $items = $currentOrder->Items();
     $quantity = $this->Value();
     $removingItem = false;
     if ($quantity <= 0) {
         $removingItem = true;
     }
     //Check that item exists and is in the current order
     if (!$item || !$item->exists() || !$items->find('ID', $item->ID)) {
         $errorMessage = _t('Form.ITEM_IS_NOT_IN_ORDER', 'This product is not in the Cart.');
         if ($msg = $this->getCustomValidationMessage()) {
             $errorMessage = $msg;
         }
         $validator->validationError($this->getName(), $errorMessage, "error");
         $valid = false;
     } else {
         if ($item) {
             //If removing item, cannot subtract past 0
             if ($removingItem) {
                 if ($quantity < 0) {
                     $errorMessage = _t('Form.ITEM_QUANTITY_LESS_ONE', 'The quantity must be at least 0');
                     if ($msg = $this->getCustomValidationMessage()) {
                         $errorMessage = $msg;
                     }
                     $validator->validationError($this->getName(), $errorMessage, "error");
                     $valid = false;
                 }
             } else {
                 //If quantity is invalid
                 if ($quantity == null || !is_numeric($quantity)) {
                     $errorMessage = _t('Form.ITEM_QUANTITY_INCORRECT', 'The quantity must be a number');
                     if ($msg = $this->getCustomValidationMessage()) {
                         $errorMessage = $msg;
                     }
                     $validator->validationError($this->getName(), $errorMessage, "error");
                     $valid = false;
                 } else {
                     if ($quantity > 2147483647) {
                         $errorMessage = _t('Form.ITEM_QUANTITY_INCORRECT', 'The quantity must be less than 2,147,483,647');
                         if ($msg = $this->getCustomValidationMessage()) {
                             $errorMessage = $msg;
                         }
                         $validator->validationError($this->getName(), $errorMessage, "error");
                         $valid = false;
                     }
                 }
                 $validation = $item->validateForCart();
                 if (!$validation->valid()) {
                     $errorMessage = $validation->message();
                     if ($msg = $this->getCustomValidationMessage()) {
                         $errorMessage = $msg;
                     }
                     $validator->validationError($this->getName(), $errorMessage, "error");
                     $valid = false;
                 }
             }
         }
     }
     return $valid;
 }
Пример #3
0
 /**
  * Add an item to the current cart ({@link Order}) for a given {@link Product}.
  * 
  * @param Array $data
  * @param Form $form
  */
 public function add(array $data, Form $form)
 {
     Cart::get_current_order(true)->addItem($this->getProduct(), $this->getVariation(), $this->getQuantity(), $this->getOptions());
     //Show feedback if redirecting back to the Product page
     if (!$this->getRequest()->requestVar('Redirect')) {
         $cartPage = DataObject::get_one('CartPage');
         $message = _t('ProductForm.PRODUCT_ADDED', 'The product was added to your cart.');
         if ($cartPage->exists()) {
             $message = _t('ProductForm.PRODUCT_ADDED_LINK', 'The product was added to {openanchor}your cart{closeanchor}.', array('openanchor' => "<a href=\"{$cartPage->Link()}\">", 'closeanchor' => "</a>"));
         }
         $form->sessionMessage(DBField::create_field("HTMLText", $message), 'good');
     }
     $this->goToNextPage();
 }
Пример #4
0
 /**
  * Adding non published product to a cart should fail
  */
 public function testAddNonPublishedProductToCart()
 {
     $productA = $this->objFromFixture('Product', 'productA');
     $this->assertEquals(false, $productA->isPublished());
     $productALink = $productA->Link();
     $this->get(Director::makeRelative($productALink));
     $message = null;
     try {
         $this->submitForm('ProductForm_ProductForm', null, array('Quantity' => 1));
     } catch (Exception $e) {
         $message = $e->getMessage();
     }
     $this->assertStringEndsWith('Object not written.', $message);
     $order = Cart::get_current_order();
     $items = $order->Items();
     $this->assertEquals(0, $items->Count());
 }
Пример #5
0
 function OrderForm()
 {
     $order = Cart::get_current_order();
     $member = Customer::currentUser() ? Customer::currentUser() : singleton('Customer');
     $form = OrderForm::create($this, 'OrderForm')->disableSecurityToken();
     //Populate fields the first time form is loaded
     $form->populateFields();
     return $form;
 }
Пример #6
0
 /**
  * Validate this form field, make sure the {@link Item} exists, is in the current 
  * {@link Order} and the item is valid for adding to the cart.
  * 
  * @see FormField::validate()
  * @return Boolean
  */
 public function validate($validator)
 {
     $valid = true;
     $item = $this->Item();
     $currentOrder = Cart::get_current_order();
     $items = $currentOrder->Items();
     //Check that item exists and is in the current order
     if (!$item || !$item->exists() || !$items->find('ID', $item->ID)) {
         $errorMessage = _t('Form.ITEM_IS_NOT_IN_ORDER', 'This product is not in the Order.');
         if ($msg = $this->getCustomValidationMessage()) {
             $errorMessage = $msg;
         }
         $validator->validationError($this->getName(), $errorMessage, "error");
         $valid = false;
     } else {
         if ($item) {
             $validation = $item->validateForCart();
             if (!$validation->valid()) {
                 $errorMessage = $validation->message();
                 if ($msg = $this->getCustomValidationMessage()) {
                     $errorMessage = $msg;
                 }
                 $validator->validationError($this->getName(), $errorMessage, "error");
                 $valid = false;
             }
         }
     }
     return $valid;
 }