G
G
Georgy Kotov2019-07-16 13:07:01
1C-Bitrix
Georgy Kotov, 2019-07-16 13:07:01

How to get discounts in add2basket via ajax for unregistered users basket?

The code does not give prices with discounts from unregistered users.
There are no restrictions in the rules for the operation of the basket: in the restrictions tab -> User groups that the rule applies to: All is selected.
Moreover, the code works in the component, if not accessed through AJAX. It seems to me that something with the session.

$uid = \Bitrix\Sale\Fuser::getId(true);
$fullBasket = Sale\Basket::loadItemsForFUser($uid, SITE_ID);
$discount = Bitrix\Sale\Discount::loadByBasket($fullBasket);
$discountResult = $discount->calculate();
if ($discountResult->isSuccess()) {
  $BB = $discount->getShowPrices();
} else {
  $BB = false;
}

Revealed that the discount does not come after updating or creating a product.
And after removing $item->delete(); - the discount remains.
if($type == 'update') {
     $basketItems = $basket->getBasketItems(); 
     foreach($basketItems as $item){	 
        if($item->getProductId() == $productId) {
     $fields = [
        'QUANTITY' => (int) $quantity
            ]; 
     $item->setFields($fields);
  }
    } 
    $basket->save();
}

$basket = \Bitrix\Sale\Basket::loadItemsForFUser(\Bitrix\Sale\Fuser::getId(), $site_id);
$discount = Bitrix\Sale\Discount::loadByBasket($basket);
if($discount): //discount пуст (
    $discount->calculate();
    $discounts = $discount->getShowPrices(); 
endif;

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question