示例#1
0
 /**
  * {@inheritdoc}
  */
 public function buildForm(array $form, FormStateInterface $form_state, CartInterface $cart = NULL)
 {
     $form['#attached']['library'][] = 'uc_cart/uc_cart.styles';
     $cart_config = $this->config('uc_cart.settings');
     $form['items'] = array('#type' => 'table', '#tree' => TRUE, '#header' => array('remove' => array('data' => $this->t('Remove'), 'class' => array('remove')), 'image' => array('data' => $this->t('Products'), 'class' => array('image')), 'desc' => array('data' => '', 'class' => array('desc')), 'qty' => array('data' => $this->t('Quantity'), 'class' => array('qty')), 'total' => array('data' => $this->t('Total'), 'class' => array('price'))));
     $form['data'] = array('#tree' => TRUE, '#parents' => array('items'));
     $i = 0;
     $subtotal = 0;
     foreach ($cart->getContents() as $cart_item) {
         $item = \Drupal::moduleHandler()->invoke($cart_item->data->module, 'uc_cart_display', array($cart_item));
         if (Element::children($item)) {
             $form['items'][$i]['remove'] = $item['remove'];
             $form['items'][$i]['remove']['#name'] = 'remove-' . $i;
             $form['items'][$i]['image'] = uc_product_get_picture($item['nid']['#value'], 'uc_cart');
             $form['items'][$i]['desc']['title'] = $item['title'];
             $form['items'][$i]['desc']['description'] = $item['description'];
             $form['items'][$i]['qty'] = $item['qty'];
             $form['items'][$i]['total'] = array('#theme' => 'uc_price', '#price' => $item['#total'], '#wrapper_attributes' => array('class' => 'total'));
             if (!empty($item['#suffixes'])) {
                 $form['items'][$i]['total']['#suffixes'] = $item['#suffixes'];
             }
             $form['data'][$i]['module'] = $item['module'];
             $form['data'][$i]['nid'] = $item['nid'];
             $form['data'][$i]['data'] = $item['data'];
             $form['data'][$i]['title'] = array('#type' => 'value', '#value' => drupal_render($item['title']));
             $subtotal += $item['#total'];
         }
         $i++;
     }
     $form['items'][]['total'] = array('#theme' => 'uc_price', '#prefix' => '<span id="subtotal-title">' . $this->t('Subtotal') . ':</span> ', '#price' => $subtotal, '#wrapper_attributes' => array('colspan' => 5, 'class' => array('subtotal')));
     $form['actions'] = array('#type' => 'actions');
     // If the continue shopping element is enabled...
     if (($cs_type = $cart_config->get('continue_shopping_type')) !== 'none') {
         // Add the element to the form based on the element type.
         if ($cart_config->get('continue_shopping_type') == 'link') {
             $form['actions']['continue_shopping'] = array('#type' => 'link', '#title' => $this->t('Continue shopping'), '#url' => Url::fromUri('internal:' . $this->continueShoppingUrl()));
         } elseif ($cart_config->get('continue_shopping_type') == 'button') {
             $form['actions']['continue_shopping'] = array('#type' => 'submit', '#value' => $this->t('Continue shopping'), '#submit' => array(array($this, 'submitForm'), array($this, 'continueShopping')));
         }
     }
     // Add the empty cart button if enabled.
     if ($cart_config->get('empty_button')) {
         $form['actions']['empty'] = array('#type' => 'submit', '#value' => $this->t('Empty cart'), '#submit' => array(array($this, 'emptyCart')));
     }
     // Add the control buttons for updating and proceeding to checkout.
     $form['actions']['update'] = array('#type' => 'submit', '#name' => 'update-cart', '#value' => $this->t('Update cart'), '#submit' => array(array($this, 'submitForm'), array($this, 'displayUpdateMessage')));
     $form['actions']['checkout'] = array('#theme' => 'uc_cart_checkout_buttons');
     if ($cart_config->get('checkout_enabled')) {
         $form['actions']['checkout']['checkout'] = array('#type' => 'submit', '#value' => $this->t('Checkout'), '#button_type' => 'primary', '#submit' => array(array($this, 'submitForm'), array($this, 'checkout')));
     }
     $this->renderer->addCacheableDependency($form, $cart);
     $this->renderer->addCacheableDependency($form, $cart_config);
     return $form;
 }
 public function testDeletedCartItem()
 {
     // Add a product to the cart, then delete the node.
     $this->addToCart($this->product);
     $this->product->delete();
     // Test that the cart is empty.
     $this->drupalGet('cart');
     $this->assertText('There are no products in your shopping cart.');
     $this->assertIdentical($this->cart->getContents(), [], 'There are no items in the cart.');
 }