Пример #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, "");
 }