【问题标题】:Add the drupal commerce products to cart without page reload programmatically无需以编程方式重新加载页面即可将 drupal 商务产品添加到购物车
【发布时间】:2014-10-01 05:36:07
【问题描述】:

如何实现以下任务:-

我有 2 个 div,在第一个 div 中我有产品名称和添加链接,如果用户点击添加链接相关产品应该添加到第二个 div。

在底部的第二个 div 中,我有添加到购物车按钮,因此单击购物车按钮所有添加的产品都应添加到 drupal Commerce 添加到购物车页面。

仅供参考,请查看以下链接:-

http://buildabagpartyfavours.ca/pages/build-your-own-goodie-bag

【问题讨论】:

    标签: drupal drupal-7 drupal-modules drupal-views drupal-commerce


    【解决方案1】:

    如果有人使用 Drupal 8,并且想解决这个问题,可以这样:

    如果您有产品变体 id $var_id,您可以创建一个带有“use-ajax”类的“a”标签:

    <a href="/add/product/'.$var_id.'" class="use-ajax -add-to-cart">Add to cart</a>
    

    在 routing.yml 中你必须用控制器添加以下路径:

    path: '/add/product/{pid}'
    _controller: Drupal\MY_MODULE\Controller\ShopProductController::addToCart
    

    你必须用 ajax 响应创建控制器:

        namespace Drupal\MY_MODULE\Controller;
    
        use Drupal\commerce_order\Entity\OrderInterface;
        use Drupal\commerce_order\Entity\OrderItem;
        use Drupal\commerce_order\Entity\Order;
        use Drupal\Core\Ajax\AjaxResponse;
        use Drupal\Core\Ajax\HtmlCommand;
        use Drupal\Core\Ajax\CssCommand;
    
    class ShopProductController {
        public function addToCart($pid) {
    
            $ajax_response = new AjaxResponse();
            #### ADD TO CART ####
            // variation_id of product.
    
            if (isset($pid) && !empty($pid)) {
    
                $store_id = 1;
                $order_type = 'default';
                $variation_id = $pid;
    
                $entity_manager = \Drupal::entityManager();
                $cart_manager = \Drupal::service('commerce_cart.cart_manager');
                $cart_provider = \Drupal::service('commerce_cart.cart_provider');
    
                // Drupal\commerce_store\Entity\Store::load($store_id);
                $store = $entity_manager->getStorage('commerce_store')->load($store_id); 
                $product_variation = $entity_manager->getStorage('commerce_product_variation')->load($variation_id);
    
                // order type and store
                $cart = $cart_provider->getCart($order_type, $store);
                if (!$cart) {
                    $cart = $cart_provider->createCart($order_type, $store);
                }
    
                //Create new order item 
                $order_item = $entity_manager->getStorage('commerce_order_item')->create(array(
                    'type' => 'default',
                    'purchased_entity' => (string) $variation_id,
                    'quantity' => 1,
                    'unit_price' => $product_variation->getPrice(),
                ));
    
                $order_item->save();
                $cart_manager->addOrderItem($cart, $order_item);
    
                $ajax_response->addCommand(new HtmlCommand('.-add-to-cart', '<span class="-added">Added</span>'));
    
                }
    
                return $ajax_response;
    
            }
    }
    

    【讨论】:

    • 您的代码似乎适用于 Drupal 8。OP 已经用 Drupal 7 标记了这个问题,这是完全不同的。
    【解决方案2】:

    让您的按钮触发一些 AJAX 调用,传递产品 ID,然后在您的 AJAX 回调脚本中收集产品 ID,从中创建订单项并将其添加到购物车 (https://www.drupal.org/node/1288414)。

    当 AJAX 调用完成后,您必须调用另一个来更新购物车块。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多