/** * 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, ""); }
/** * This function will run all applicable cron on recurring entity * @return array|bool */ public function runCron($cron = 'All') { $recurring_order = array(); $return_value = array(); module_load_include('inc', 'commerce_recurring', 'commerce_recurring.rules'); module_load_include('inc', 'mp_order', 'mp_order.rules'); module_load_include('inc', 'mp_upgrade', 'mp_upgrade.rules'); // Create Order Based on recurring entity due date $recurring_entity = $this->getEntity(); if (in_array($cron, array('All', 'create_order'))) { $due_entities = commerce_recurring_rules_get_due_items(); $entity_array = array(); foreach ($due_entities['commerce_recurring_entities'] as $entity_key => $entity) { $entity_array[$entity->id] = $entity; } if (array_key_exists($this->getId(), $entity_array)) { // Passing recurring entity and create order $recurring_order = commerce_recurring_rules_generate_order_from_recurring($recurring_entity); if (!empty($recurring_order) && isset($recurring_order['commerce_order'])) { $return_value['commerce_order'] = $recurring_order['commerce_order']; global $entities; $order = new CommerceOrder($recurring_order['commerce_order']->order_id); $order->reload(); $entities['commerce_order'][$order->getId()] = $order; mp_order_update_order_with_store_credit($recurring_order['commerce_order']); mp_subscription_rules_action_update_recurring_billing_due_date($recurring_order['commerce_order']); // Attaching order with recurring entity } } } //Payment and Order Status of Above Created Order if (in_array($cron, array('All', 'pending_payment'))) { $orders = $this->getCommerceRecurringOrderValues(); foreach ($orders as $associated_oder) { $order = new CommerceOrder($associated_oder['target_id']); if ($order->getStatusValues() == 'recurring_pending') { $card_response = commerce_cardonfile_rules_action_order_select_default_card($order->getEntity()); $total = $order->getFieldItems('commerce_order_total'); $charge_response = commerce_cardonfile_rules_action_order_charge_card($order->getEntity(), $total[0], $card_response['select_card_response']); $return_value['charge_response'] = $charge_response; } } } //Upgrade Yearly Plan if (in_array($cron, array('All', 'upgrade'))) { $recurring_entities = mp_upgrade_rules_action_get_upgraded_items(); if (array_key_exists($this->getId(), $recurring_entities['commerce_recurring_entities'])) { mp_upgrade_rules_action_upgrade_recurring_license($this->getEntity()); } } return new Response(TRUE, $return_value, NULL); }
/** * Press the Add to cart button. * * @return Response * Response object. */ public function submit($show_quantity = FALSE) { $show_quantity = $show_quantity ? $show_quantity : $this->arguments['show_quantity']; $response = $this->pressButton(t('Add to cart'), array(), $this->arguments['line_item'], $show_quantity, $this->cart_context); if (!$response->getSuccess()) { return $response; } $form_state = $this->getFormState(); $order_id = $form_state['line_item']->order_id; $order = new CommerceOrder($order_id); $order->reload(); // To trigger the rule "Override price for recurring", we need to load the // order entity from the database after clearing the cache. /*$commerce_order = $order->getEntity(); unset($commerce_order->data['last_cart_refresh']); $order->setEntity($commerce_order); drupal_static_reset('commerce_cart_commerce_order_load'); $order->reload();*/ global $entities; $entities['commerce_order'][$order->order_id] = $order; return new Response(TRUE, $order, ""); }