Esempio n. 1
1
 /**
  * This function will create recurring order and entity programmatically.
  * @param $product_ids
  * @return \entity
  */
 public static function createProgrammatically($product_ids)
 {
     global $user;
     global $entities;
     $product = array();
     $quantity = 1;
     $uid = $user->uid;
     $add_shipping = FALSE;
     //
     $subscription_products = Utils::getSubscriptionProductsList()->verify(get_class());
     foreach ($product_ids as $product_id) {
         if ($product = commerce_product_load($product_id)) {
             if ($product->type == 'product') {
                 $add_shipping = TRUE;
             }
             if (in_array($product->sku, $subscription_products)) {
                 $product->commerce_price[LANGUAGE_NONE][0]['amount'] = 0;
                 $line_item = commerce_product_line_item_new($product, $quantity);
             } else {
                 $line_item = commerce_product_line_item_new($product, $quantity);
             }
             commerce_cart_product_add($uid, $line_item);
         }
     }
     // if no product loaded then need to return null
     if (empty($product)) {
         return NULL;
     }
     $order = commerce_cart_order_load($uid);
     $order = commerce_order_status_update($order, "pending", TRUE);
     if ($add_shipping) {
         // Save and add the line item to the order.
         $line_item = new Shipping(NULL, $order->order_id);
         $line_item = $line_item->createShippingLineItemProgrammatically($order);
         $new_line_item = commerce_shipping_add_shipping_line_item($line_item, $order, TRUE);
     }
     commerce_avatax_calculate_sales_tax($order);
     commerce_order_save($order);
     drupal_static_reset('commerce_recurring_order_load_recurring_line_items');
     commerce_checkout_complete($order);
     $order_object = new CommerceOrder($order->order_id);
     $entities['commerce_order'][$order_object->getId()] = $order_object;
     $order_object->reload();
     return new Response(TRUE, $order_object, "");
 }
 /**
  * Helper function creating multiple dummy orders.
  * If no customers or products exist, then one of each get created.
  */
 protected function createOrders($amount = 1, $createTransactions = FALSE, $possibleDates = array())
 {
     if (empty($this->products)) {
         $this->createProducts();
     }
     if (empty($this->customers)) {
         $this->createCustomers();
     }
     $order_status = 'completed';
     if ($createTransactions) {
         $order_status = 'pending';
     }
     for ($i = 0; $i < $amount; $i++) {
         $totalProducts = rand(1, count($this->products));
         $products = array();
         for ($x = 0; $x < $totalProducts; $x++) {
             $product = $this->products[array_rand($this->products)];
             $products[$product->product_id] = rand(1, 10);
         }
         if (!($customer = next($this->customers))) {
             $customer = reset($this->customers);
         }
         $order = $this->createDummyOrder($customer->uid, $products, $order_status);
         if (!empty($possibleDates)) {
             $date = $possibleDates[array_rand($possibleDates)];
             $order->created = $date;
             commerce_order_save($order);
         }
         $transaction = NULL;
         if ($createTransactions) {
             $transaction = commerce_payment_transaction_new('commerce_payment_example', $order->order_id);
             $transaction->amount = $order->commerce_order_total[LANGUAGE_NONE][0]['amount'];
             $transaction->status = 'success';
             commerce_payment_transaction_save($transaction);
         }
         $this->orders[] = array('commerce_transaction' => $transaction, 'commerce_order' => $order, 'products' => $products);
     }
 }