Exemplo n.º 1
0
 private function upsellProcess()
 {
     // is this request still valid?  set to one hour.  there is a cron that salvages orphaned upsell orders after 1 hour
     if ($this->intervalCheck) {
         $this->checkInterval($this->order->updated);
     }
     // load previous order upsells, if any
     $orderUpsells = AFActiveDataProvider::models('OrderUpsell');
     $orderUpsells->loadId($this->order->order_id);
     // is this a switch main product upsell?
     $replace = false;
     $upsell = new Upsell();
     if (!$upsell->fillFromDbPk($this->post['uid'])) {
         apiError('order upsell invalid');
     }
     if ($upsell->product_replace && $this->post['act_up'] == 'yes') {
         $replace = true;
         // overwrite main order_product record and reset upsell path
         $this->post['product_id'] = $upsell->product_id;
         $this->newOrder = false;
         // $this->upsell_id is built via the function below
         $this->buildProducts();
         $this->order->amount_product = $this->amount_product;
         if ($upsell->shipping_id) {
             $shipping = new Shipping();
             if ($shipping->fillFromDbPk($upsell->shipping_id)) {
                 $this->order->shipping_id = $shipping->shipping_id;
                 $this->order->amount_shipping = $shipping->amount_initial;
             }
             unset($shipping);
         }
         if (!$this->order->save()) {
             fb('order not saved');
             continue;
         }
         // overwrite step so we can delete all previous steps below
         $this->post['step'] = 0;
     }
     // is this a replace product, or has this step already been recorded?  if so, wipe out steps in DB
     if ($replace || (int) $this->post['step'] <= $orderUpsells->getUpsellStep()) {
         $orderUpsells->deleteSteps($this->post['step']);
     }
     if (!$replace) {
         $orderUpsells->add($this->order->order_id, $this->post['uid'], $this->post['act_up'], $this->post['step']);
         if (!$orderUpsells->save()) {
             apiError('order upsell invalid');
         }
         // is there another upsell based on the asnwer given?
         $this->upsell_id = $orderUpsells->nextUpsell();
     }
     if (!$this->upsell_id) {
         // We're all done with upsells, insert upsell products into order products
         // retrieve existing order_products
         $orderProducts = new OrderProducts('OrderProduct');
         $orderProducts->loadByOrderId($this->order->order_id);
         foreach ($orderUpsells->orderUpsells as $oa) {
             if ($oa->answer == 'yes') {
                 $orderProducts->add($oa->product_id, $oa->shipping_id, $this->order->order_id);
             }
             // remove from DB
             $oa->delete();
         }
         if (!$orderProducts->save()) {
             continue;
         }
         // update amounts
         $this->order->amount_product = $orderProducts->getTotal();
         if (!$this->order->save()) {
             fb('order not saved');
             continue;
         }
         // process Payment
         $this->webPayment();
     } else {
         // send user to next upsell page
         $this->upsellDirect((int) $this->post['step'] + 1);
     }
 }