Documentation

Shopping cart

Attention! Shipments are always associated with orders. It's categorically prohibited to use the method \Bitrix\Sale\Basket::save() for saving shipments. When updating a shipment changes, associated entities may be updated, which will not be saved. Saving action must be performed via the order method: \Bitrix\Sale\Order::save(). Additionally, calling \Bitrix\Sale\Basket::save() will generate error E_WARNING.

  • Creating a shopping cart
  • Getting a shopping cart:
  • Adding a shopping cart item
  • Getting a position
  • Updating a product position
  • Getting a shopping cart weight
  • Getting a shopping cart price:
  • Updating shopping cart with provider data
  • Setting a product custom price
  • Deleting a shopping cart item
  • Creating a shopping cart

    $siteId = 's1'; // site ID with associated shopping cart
    $basket = \Bitrix\Sale\Basket::create($siteId);
    

    Getting a shopping cart


    • shopping cart not associated with an order:
      /*
       * $fuser - user ID
       * $siteId - site ID with associated shopping cart
       */
      
      $basket = \Bitrix\Sale\Basket::loadItemsForFUser($fuser, $siteId); 
      

    • shopping cart, associated with an order:
      /*
       * $orderId - order id
       */
      $order = \Bitrix\Sale\Order::load($orderId);
      
      $basket = $order->getBasket();
      

    • getting list of products:

      Additionally to objects, you can directly query the database using the method \Bitrix\Sale\Basket::getList($parameters).

      Method always returns object Bitrix\Main\DB\Result, to retrieve data via the method fetch().

      Parameter Description Available from version
      $parameters Array with structure fully matching ORM's getList.
      // getting list of products for current user
      $dbRes = \Bitrix\Sale\Basket::getList([
      	'select' => ['NAME', 'QUANTITY'],
      	'filter' => [
      		'=FUSER_ID' => \Bitrix\Sale\Fuser::getId(), 
      		'=ORDER_ID' => null,
      		'=LID' => \Bitrix\Main\Context::getCurrent()->getSite(),
      		'=CAN_BUY' => 'Y',
      	]
      ]);
      
      while ($item = $dbRes->fetch())
      {
      	var_dump($item);
      }
      

    Adding a shopping cart position

    • Variant 1:
      /*
       * $basket - object of class \Bitrix\Sale\Basket
       */
      
      $basketItem = $basket->createItem($moduleId, $productId);
      $basketItem->setField('QUANTITY', 4);
      
    • Variant 2:
      $basketItem = \Bitrix\Sale\BasketItem::create($basket, $moduleId, $productId);
      $basketItem->setField('QUANTITY', 4);
      
      $basket->addItem($basketItem);
      

    Getting a position

    • by ID:
      $basketItem = $basket->getItemById($id);
      
    • by basketCode:
      $basketItem = $basket->getItemByBasketCode($itemCode);
      

      itemCode - item code. To get the code: $basketItem->getBasketCode();.

    • by internal index:
      $basketItem = $basket->getItemByIndex($index);
      

    Updating a position

    You can update both individual field and group of fields:


    • Updating a separate field:
      • When shopping cart is not associated with an order:
        $basket = Sale\Basket::loadItemsForFUser($fuser, $siteId);
        
        $basketItem = $basket->getItemById($basketItemId);
        if ($basketItem)
        {
        	$basketItem->setField('FIELD_NAME', $value);
        }
        
        $basket->save();
        
      • When shopping cart is associated with order:
        $order = \Bitrix\Sale\Order::load(123);
        
        $basket = $order->getBasket();
        
        $basketItem = $basket->getItemById(1);
        if ($basketItem)
        {
        	$basketItem->setField('FIELD_NAME', $value);
        }
        
        $order->save();
        

    • Updating a group of fields:
      • When shopping cart is not associate with an order:
        $basket = Sale\Basket::loadItemsForFUser($fuser, $siteId);
        
        $basketItem = $basket->getItemById($basketItemId);
        if ($basketItem)
        {
        	$basketItem->setFields([
        		'FIELD_NAME_1' => $value,
        		...
        		'FIELD_NAME_2' => $value,
        	]);
        }
        
        $basket->save();
        
      • When shopping cart is associated to an order:
        $order = \Bitrix\Sale\Order::load(123);
        
        $basket = $order->getBasket();
        
        $basketItem = $basket->getItemById(1);
        if ($basketItem)
        {
        	$basketItem->setFields([
        		'FIELD_NAME_1' => $value,
        		...
        		'FIELD_NAME_2' => $value,
        	]);
        }
        
        $order->save();
        

    List of fields, available for update:

    "NAME", // product name
    "LID", // site used for purchase
    "SORT", // sorting
    "PRODUCT_ID", // product ID
    "BASE_PRICE", // price without discounts and extra charges
    "PRICE", // price with discounts and extra charges
    "DISCOUNT_PRICE", // discount
    "CURRENCY", // Currency
    "CUSTOM_PRICE", // flag(Y/N): custom price
    "QUANTITY", // amount
    "WEIGHT", // weight
    "DIMENSIONS", // dimensions
    "MEASURE_CODE", // unit of measurement ID
    "DELAY", // Flag "product reserved" (Y/N)
    "CAN_BUY", // Flag "product available for purchase" (Y/N)
    "NOTES", // Additional notes
    "VAT_RATE",  // VAT rate
    "VAT_INCLUDED", // flag(Y/N): disable/enable tax
    "BARCODE_MULTI",  // flag(Y/N): unique barcode
    "SUBSCRIBE", // flag(Y/N): product subscription
    "PRODUCT_PROVIDER_CLASS", // provider class name
    "TYPE", // product type
    "XML_ID" // external ID
    

    Getting shopping cart weight

    $basket->getWeight();
    

    Getting shopping cart price :

    • without discounts and extra charges
      $basket->getBasePrice();
      
    • with discounts and extra charges
      • When shopping cart is associated with order:
        $price = $basket->getPrice();
        
      • When shopping cart is not associated with order (applicable discounts are not stored until the order is created):
        /*
         * $fuser - user ID
         * $siteId - site ID, with associated shopping cart
         */
        
        $basket = \Bitrix\Sale\Basket::loadItemsForFUser($fuser, $siteId); // $basket - object of class \Bitrix\Sale\Basket
        
        $context = new \Bitrix\Sale\Discount\Context\Fuser($basket->getFUserId());
        $discounts = \Bitrix\Sale\Discount::buildFromBasket($basket, $context);
        $r = $discounts->calculate();
        if (!$r->isSuccess())
        {
        	var_dump($r->getErrorMessages());
        }
        
        $result = $discounts->getApplyResult();
        if (isset($result['BASKET_ITEMS']))
        {
        	$r = $basket->applyDiscount($result['BASKET_ITEMS']);
        	if (!$r->isSuccess())
        	{
        		var_dump($r->getErrorMessages());
        	}
        }
        

    Updating shopping cart with provider data

    $basket->refresh();
    

    Setting a product custom price

    // load the order
    $order = \Bitrix\Sale\Order::load($ID);
    
    // get a shopping cart
    $basket = $order->getBasket();
    
    foreach ($basket as $basketItem)
    {
    	if ((int)$basketItem->getProductId() === 120)
    	{
    		$basketItem->setField('CUSTOM_PRICE', 'Y');
    		$basketItem->setField('PRICE', 100);
    		}
    }
    
    $r = $order->save();
    if (!$r->isSuccess())
    {
        var_dump($r->getErrorMessages());
    }
    
    

    Deleting a shopping cart item

    • When shopping cart is associated with an order:
      $order = \Bitrix\Sale\Order::load(123);
      
      $basket = $order->getBasket();
      
      $basketItem = $basket->getItemById(1);
      if ($basketItem)
      {
      	$basketItem->delete();
      }
      
      $r = $order->save();
      if (!$r->isSuccess())
      {
      	var_dump($r->getErrorMessages());
      }
      
    • When shopping cart is not associated with an order:
      $basket = Sale\Basket::loadItemsForFUser($fuser, SITE_ID);
      
      $basketItem = $basket->getItemById($basketItemId);
      $result = $basketItem->delete();
      if ($result->isSuccess())
      {
      	$basket->save();
      }
      


    © «Bitrix24», 2001-2024
    Up