ShoppingCart_Controller::direct PHP Method

direct() public static method

This is used here and in VariationForm and AddProductForm
public static direct ( boolean | string $status = true ) : boolean
$status boolean | string
return boolean
    public static function direct($status = true)
    {
        if (Director::is_ajax()) {
            return $status;
        }
        if (self::config()->direct_to_cart_page && ($cartlink = CartPage::find_link())) {
            Controller::curr()->redirect($cartlink);
            return;
        } else {
            Controller::curr()->redirectBack();
            return;
        }
    }

Usage Example

 /**
  * Handles form submission
  * @param array $data
  * @return bool|\SS_HTTPResponse
  */
 public function addtocart(array $data)
 {
     $groupedProduct = $this->getController()->data();
     if (empty($data) || empty($data['Product']) || !is_array($data['Product'])) {
         $this->sessionMessage(_t('GroupedCartForm.EMPTY', 'Please select at least one product.'), 'bad');
         $this->extend('updateErrorResponse', $this->request, $response, $groupedProduct, $data, $this);
         return $response ? $response : $this->controller->redirectBack();
     }
     $cart = ShoppingCart::singleton();
     foreach ($data['Product'] as $id => $prodReq) {
         if (!empty($prodReq['Quantity']) && $prodReq['Quantity'] > 0) {
             $prod = Product::get()->byID($id);
             if ($prod && $prod->exists()) {
                 $saveabledata = !empty($this->saveablefields) ? Convert::raw2sql(array_intersect_key($data, array_combine($this->saveablefields, $this->saveablefields))) : $prodReq;
                 $buyable = $prod;
                 if (isset($prodReq['Attributes'])) {
                     $buyable = $prod->getVariationByAttributes($prodReq['Attributes']);
                     if (!$buyable || !$buyable->exists()) {
                         $this->sessionMessage("{$prod->InternalItemID} is not available with the selected options.", "bad");
                         $this->extend('updateErrorResponse', $this->request, $response, $groupedProduct, $data, $this);
                         return $response ? $response : $this->controller->redirectBack();
                     }
                 }
                 if (!$cart->add($buyable, (int) $prodReq['Quantity'], $saveabledata)) {
                     $this->sessionMessage($cart->getMessage(), $cart->getMessageType());
                     $this->extend('updateErrorResponse', $this->request, $response, $groupedProduct, $data, $this);
                     return $response ? $response : $this->controller->redirectBack();
                 }
             }
         }
     }
     $this->extend('updateGroupCartResponse', $this->request, $response, $groupedProduct, $data, $this);
     return $response ? $response : ShoppingCart_Controller::direct($cart->getMessageType());
 }
All Usage Examples Of ShoppingCart_Controller::direct