Exemplo n.º 1
0
 private function buildProducts()
 {
     // load product options for this campaign
     $campProducts = new CampaignProducts('CampaignProduct');
     $campProducts->load($this->campaign->campaign_id);
     // build order_products
     $orderProducts = new OrderProducts('OrderProduct');
     // does this order already exist? wipe out any existing order products, in case of prior order errors.
     // this will prevent dupe order prods in the DB
     if (!$this->newOrder) {
         $orderProducts->loadByOrderId($this->order->order_id);
         if ($orderProducts->getNumProducts()) {
             $orderProducts->deleteOrderProductsFromDb();
         }
     }
     // in the future, will there be an input array of products?
     // replace POST with DB value?  campaign dependent info?
     $product_ids = array($this->post['product_id']);
     $this->upsell_id = 0;
     foreach ($product_ids as $product_id) {
         // make sure product passed to api is valid for this campaign
         if (!$campProducts->productValid($product_id)) {
             //is there only available product id for the campaign? and one passed over product id?  if so, assign id to the value
             if (count($campProducts->products) == 1 && count($product_ids) == 1) {
                 $product_id = $campProducts->products[0]->product_id;
             } else {
                 // multiple product ids available.  abort
                 $this->apiError('invalid product supplied');
             }
         }
         $orderProducts->add($product_id, $this->order->shipping_id, $this->order->order_id);
         // check for upsells
         if (!$this->upsell_id) {
             $this->upsell_id = $campProducts->hasUpsell($product_id);
         }
     }
     unset($campProducts);
     if (!$orderProducts->save()) {
         $this->apiError('opError ' . $orderProducts->error);
     }
     // grab amounts to return.
     $this->amount_product = $orderProducts->getTotal();
     unset($orderProducts);
     return true;
 }