diff -u b/commerce_pos.routing.yml b/commerce_pos.routing.yml --- b/commerce_pos.routing.yml +++ b/commerce_pos.routing.yml @@ -53,7 +53,8 @@ - path: '/commerce_pos/addToOrder/{product}' + path: '/commerce_pos/addToOrder/{product_var_id}/{quantity}' defaults: _controller: '\Drupal\commerce_pos\Controller\AutoCompleteAddOrder::addToOrder' _title: 'Add to order' requirements: _permission: 'access content' - product: \d+ + product_var_id: \d+ + quantity: \d+ diff -u b/js/commerce_pos_widget.js b/js/commerce_pos_widget.js --- b/js/commerce_pos_widget.js +++ b/js/commerce_pos_widget.js @@ -6,7 +6,7 @@ var product_id = product_title_value[1].replace(/\)/g, '').trim(); if(product_id) { $.ajax({ - url: "/commerce_pos/addToOrder/" + product_id, + url: "/commerce_pos/addToOrder/" + product_id + "/1", success: function (data) { response($.map(data[1], function (item) { return { diff -u b/src/Controller/AutoCompleteAddOrder.php b/src/Controller/AutoCompleteAddOrder.php --- b/src/Controller/AutoCompleteAddOrder.php +++ b/src/Controller/AutoCompleteAddOrder.php @@ -2,16 +2,14 @@ namespace Drupal\commerce_pos\Controller; -use Drupal\commerce_cart\CartManager; -use Drupal\commerce_product\Entity\Product; +use Drupal\commerce_pos\CurrentOrder; +use Drupal\commerce_product\Entity\ProductVariation; use Drupal\Core\Controller\ControllerBase; use Symfony\Component\HttpFoundation\JsonResponse; -use Drupal\commerce_cart\CartProviderInterface; -use Drupal\commerce_cart\CartManagerInterface; +use \Drupal\commerce_order\Entity\OrderItem; use Drupal\user\PrivateTempStoreFactory; use Symfony\Component\DependencyInjection\ContainerInterface; - /** * Class AutoCompleteAddOrder. * @@ -20,18 +18,11 @@ class AutoCompleteAddOrder extends ControllerBase { /** - * The cart manager. + * The currentOrder object. * - * @var \Drupal\commerce_cart\CartManagerInterface + * @var \Drupal\commerce_pos\CurrentOrder */ - protected $cartManager; - - /** - * The cart provider. - * - * @var \Drupal\commerce_cart\CartProviderInterface - */ - protected $cartProvider; + protected $currentOrder; /** * The tempstore object. @@ -43,15 +34,13 @@ /** * Constructs a new CartController object. * - * @param \Drupal\commerce_cart\CartManagerInterface $cart_manager - * The cart manager. - * - * \Drupal\commerce_cart\CartProviderInterface $cart_provider - * The cart provider. + * @param Drupal\commerce_pos\CurrentOrder $currentOrder + * The current Order. + * @param Drupal\user\PrivateTempStoreFactory $temp_store_factory + * The temporary storage. */ - public function __construct(CartManagerInterface $cart_manager, CartProviderInterface $cart_provider, PrivateTempStoreFactory $temp_store_factory) { - $this->cartManager = $cart_manager; - $this->cartProvider = $cart_provider; + public function __construct(CurrentOrder $currentOrder, PrivateTempStoreFactory $temp_store_factory) { + $this->currentUser = $currentOrder; $this->tempStore = $temp_store_factory; } @@ -60,37 +49,56 @@ */ public static function create(ContainerInterface $container) { return new static( - $container->get('commerce_cart.cart_manager'), - $container->get('commerce_cart.cart_provider'), + $container->get('commerce_pos.current_order'), $container->get('user.private_tempstore') ); } /** * Add to the the order. - * - * @return string - * Return Hello string. */ - public function addToOrder($product) { + public function addToOrder($product_var_id, $quantity) { - // Loading the product from the product ID. - $product_obj = Product::load($product); + // Getting the Store . + $register = $this->tempStore->get('register'); + if (!empty($register)) { + $store_id = $register->getStoreId(); + } + else { + $store_storage = \Drupal::entityTypeManager()->getStorage('commerce_store'); + $store_id = $store_storage->loadDefault()->id(); + } + + $order = $this->currentOrder->get(); + + if (!$order) { + $order = Order::create([ + 'type' => 'pos', + 'field_cashier' => \Drupal::currentUser()->id(), + ]); - // Get the default product variation. - $product_variant_object = $product_obj->getDefaultVariation(); + $order->setStoreId($store_id); - //Getting the current cart. - $user = \Drupal::currentUser(); - $carts = $this->cartProvider->getCarts($user); - if (!empty($carts)) { - $cart = array_shift($carts); - $this->cartManager->addEntity($cart,$product_variant_object); - drupal_set_message('Product has been added to cart.', 'status'); - $results['status'] = ['success']; + $order->save(); + + $this->currentOrder->set($order); } + // Loading the product variation object. + $pro_variation_obj = ProductVariation::load($product_var_id); + + // Getting new order Item for adding into existing order. + $order_item = OrderItem::create([ + 'type' => 'product_variation', + 'purchased_entity' => $pro_variation_obj, + 'quantity' => $quantity, + 'unit_price' => $pro_variation_obj->getPrice(), + ]); + + // Adding the item into the Order. + $order->addItem($order_item); + $results['status'] = ['success']; return new JsonResponse($results); } diff -u b/src/Controller/PosOrderItemAutoComplete.php b/src/Controller/PosOrderItemAutoComplete.php --- b/src/Controller/PosOrderItemAutoComplete.php +++ b/src/Controller/PosOrderItemAutoComplete.php @@ -6,8 +6,8 @@ use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; use Drupal\commerce_product\Entity\Product; - - +use Drupal\user\PrivateTempStoreFactory; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Class PosOrderItemAutoComplete. @@ -16,27 +16,62 @@ */ class PosOrderItemAutoComplete extends ControllerBase { + /** + * The tempstore object. + * + * @var \Drupal\user\SharedTempStore + */ + protected $tempStore; + + /** + * Constructs a new POS object. + * + * @param \Drupal\user\PrivateTempStoreFactory $temp_store_factory + * The tempstore factory. + */ + public function __construct(ContainerInterface $container, PrivateTempStoreFactory $temp_store_factory) { + $this->container = $container; + $this->tempStore = $temp_store_factory->get('commerce_pos'); + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container, + $container->get('user.private_tempstore') + ); + } /** * Order item auto complete handler. * - * @return string - * Return Hello string. + * @param Symfony\Component\HttpFoundation\Request $request + * The request object. + * @param string $value + * The Element value. + * @param int $count + * The suggestions count. + * + * @return object + * The Json object for auto complete suggestions. */ public function orderItemAutoCompleteHandler(Request $request, $value, $count) { $results = []; - if ($input = $request->query->get('q')) { - $string = $request->query->get('q'); $suggestions = $this->searchQueryString($string); - foreach ($suggestions as $key) { - $results[] = [ - 'value' => $key->title . " (" . $key->product_id . ")", - 'label' => $this->showProductasHTML($key->product_id), - ]; + $product = Product::load($key->product_id); + $variations = $product->getVariations(); + foreach ($variations as $variation) { + $results[] = [ + 'value' => $variation->getTitle() . " (" . $variation->id() . ")", + 'label' => $this->showProductasHtml($variation), + ]; + } } } @@ -47,37 +82,57 @@ * Helper function for searching the product. + * + * @param string $string + * The Query string. + * + * @return mixed + * The query search result. */ public function searchQueryString($string) { + // Getting the Store ID. + $register = $this->tempStore->get('register'); + if (!empty($register)) { + $store_id = $register->getStoreId(); + } + else { + $store_storage = \Drupal::entityTypeManager()->getStorage('commerce_store'); + $store_id = $store_storage->loadDefault()->id(); + } + $connection = \Drupal::service('database'); $query = $connection->select('commerce_product_field_data', 'cpd') ->fields('cpd', ['product_id', 'title']) - ->condition('cpd.title', "%" . $string . "%", 'LIKE') ->range(0, 10); + $query->join('commerce_product__stores', 'cps', 'cps.entity_id = cpd.product_id AND cps.stores_target_id = :store_id + AND cpd.title LIKE :string', [':store_id' => $store_id, ':string' => '%' . $string . '%']); $result = $query->execute()->fetchAll(); return $result; } /** * Helper function to do return the HTML output. + * + * @param Drupal\commerce_product\Entity\ProductVariation $variation + * The Product variation. + * + * @return string + * The html string. */ - public function showProductasHTML($product_id) { - - $product = Product::load($product_id); - $variation = $product->getDefaultVariation(); + public function showProductasHtml($variation) { + $sku = $variation->getSku(); $price = $variation->getPrice()->__toString(); // @todo Need to add a default image, HTML, CSS $html_output = '
- Drupal Commerce Ski Cap (Black, One Size) +
-
' . $product->getTitle() . '
+
' . $variation->getTitle() . '
+
' . $sku . '
' . $price . '
'; return $html_output; - } - } diff -u b/src/Plugin/Field/FieldWidget/PosOrderItemWidget.php b/src/Plugin/Field/FieldWidget/PosOrderItemWidget.php --- b/src/Plugin/Field/FieldWidget/PosOrderItemWidget.php +++ b/src/Plugin/Field/FieldWidget/PosOrderItemWidget.php @@ -7,7 +7,6 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\inline_entity_form\Plugin\Field\FieldWidget; - /** * Plugin implementation of the 'pos_order_item_widget' widget. *