Пример #1
0
 public function executeCheckout(sfWebRequest $request)
 {
     $cart = $this->getUser()->getAttribute('cart', new cartCore());
     if ($request->getParameter('agreebutton')) {
         $cart->agreed = true;
     }
     if ($request->getParameter('agree')) {
         $cart->agreed = true;
     }
     if ($request->getParameter('disagree')) {
         $cart->agreed = false;
         exit;
     }
     if ($request->getParameter('quoteselection')) {
         $cart->setSelectedQuoteAmount($request->getParameter('quoteselection'));
         cart::show_summary();
         exit;
     }
     if ($request->getParameter('shipselect')) {
         if ($request->getParameter('shipquotes')) {
             $cart->setSelectedQuoteAmount($request->getParameter('shipquotes'));
         }
     }
     if ($request->getParameter('couponcode')) {
         $submittedCode = $request->getParameter('couponcode');
         $coupon = Doctrine_Query::create()->select('*')->from('coupon c')->where("c.code = ?", $submittedCode)->andWhere('active=1')->execute();
         if (count($coupon) != 0) {
             if ($coupon[0]['minprice'] < $cart->getSubTotal()) {
                 $cart->apply_coupon($coupon[0]);
             } else {
                 $cart->clear_coupon();
                 $this->getUser()->setFlash('notice', sprintf("Coupon requires minimum purchase of \$" . $coupon[0]['minprice']));
             }
         } else {
             $cart->clear_coupon();
             $this->getUser()->setFlash('error', sprintf("Invalid coupon code."));
         }
     }
     if ($request->getParameter('update_location')) {
         if ($request->getParameter('zipcode')) {
             $err = $cart->processZipCode($request->getParameter('zipcode'));
             if ($err != "") {
                 $_SESSION['zipcode_error'] = $err;
             }
         }
     } else {
         if ($request->getParameter('update_cart')) {
             $qty = $request->getParameter('itemqty');
             $id = $request->getParameter('itemid');
             if ($qty != null and $id != null) {
                 $product = Doctrine::getTable('product')->createQuery('a')->where('id=' . $id)->execute();
                 ///////////////////SECTION NOT WORKING
                 //TODO: Cart quantities update properly, but there are no errors when requesting more than available stock.
                 $qtyInStock = $product[0]['quantity'];
                 if ($qtyInStock < $qty) {
                     $cart->qtyerror = "Sorry, there are only " . $qtyInStock . " " . $product[0]['name'] . " left in stock.";
                 } else {
                     $cart->update_item($id, $qty);
                 }
             }
         } else {
             if ($request->getParameter('paypalcheckout')) {
                 if (!$cart->agreed) {
                     $cart->termserror = "You must agree to the terms and conditions.";
                 } else {
                     $paypal_count = 1;
                     //paypal count starts at 1
                     $items_query_string;
                     foreach ($cart->get_contents() as $item) {
                         $items_query_string .= '&item_name_' . $paypal_count . '=' . $item['name'];
                         $items_query_string .= '&amount_' . $paypal_count . '=' . $item['price'];
                         $items_query_string .= '&quantity_' . $paypal_count . '=' . $item['qty'];
                         ++$paypal_count;
                     }
                     $cart->empty_cart();
                     $paypalId = "*****@*****.**";
                     $this->redirect('https://www.paypal.com/cgi-bin/webscr?cmd=_cart&upload=1&business=' . $paypalId . $items_query_string);
                 }
             }
         }
     }
 }