S
S
symply_rabbit2019-04-26 16:49:50
1C-Bitrix
symply_rabbit, 2019-04-26 16:49:50

How to display products with discounts in the Bitrix cart using the API?

Good afternoon! the situation is as follows - when the goods in the cart are discounted depending on how many goods are in the cart and for what amount.
normal display of the basket and its contents

<?
// Выведем актуальную корзину для текущего пользователя

$arBasketItems = array();

$dbBasketItems = CSaleBasket::GetList(
        array(
                "NAME" => "ASC",
                "ID" => "ASC"
            ),
        array(
                "FUSER_ID" => CSaleBasket::GetBasketUserID(),
                "LID" => SITE_ID,
                "ORDER_ID" => "NULL"
            ),
        false,
        false,
        array("ID", "NAME", "CALLBACK_FUNC", "MODULE", 
              "PRODUCT_ID", "QUANTITY", "DELAY", 
              "CAN_BUY", "PRICE", "WEIGHT")
    );
while ($arItems = $dbBasketItems->Fetch())
{
    if (strlen($arItems["CALLBACK_FUNC"]) > 0)
    {
        CSaleBasket::UpdatePrice($arItems["ID"], 
                                 $arItems["NAME"],
                                 $arItems["CALLBACK_FUNC"], 
                                 $arItems["MODULE"], 
                                 $arItems["PRODUCT_ID"], 
                                 $arItems["QUANTITY"]);
        $arItems = CSaleBasket::GetByID($arItems["ID"]);
    }

    $arBasketItems[] = $arItems;

    $name[] = $arItems["NAME"];
    $price[] = round($arItems["PRICE"], 2);
    
}

?>


<?
$a = array($price);
$sum = array_sum($price);
echo $sum;?>

displays the cart without taking into account the discount.
How to display a shopping cart (products in it, prices and their amount) at a discount?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
symply_rabbit, 2019-04-26
@symply_rabbit

2 answers found: 1)

<?$fuserId = CSaleBasket::GetBasketUserID();

$dbBasketItems = CSaleBasket::GetList(
   array("ID" => "ASC"),
   array(
       "FUSER_ID" => $fuserId,
       "LID" => SITE_ID,
       "ORDER_ID" => "NULL",
       "DELAY"=>"N"
   ),
   false,
   false,
   array(
       "ID", "NAME", "CALLBACK_FUNC", "MODULE", "PRODUCT_ID", "PRODUCT_PRICE_ID", "QUANTITY", "DELAY", "CAN_BUY",
       "PRICE", "WEIGHT", "DETAIL_PAGE_URL", "NOTES", "CURRENCY", "VAT_RATE", "CATALOG_XML_ID",
       "PRODUCT_XML_ID", "SUBSCRIBE", "DISCOUNT_PRICE", "PRODUCT_PROVIDER_CLASS", "TYPE", "SET_PARENT_ID"
   )
);


while ($arBasketItems = $dbBasketItems->Fetch())
{
   $allSum = ($arBasketItems["PRICE"] * $arBasketItems["QUANTITY"]);
   $allWeight = ($arBasketItems["WEIGHT"] * $arBasketItems["QUANTITY"]);
   $arResult[] = $arBasketItems;
}

$arOrder = array(
   'SITE_ID' => SITE_ID,
   'USER_ID' => $GLOBALS["USER"]->GetID(),
   'ORDER_PRICE' => $allSum, // сумма всей корзины
   'ORDER_WEIGHT' => $allWeight, // вес всей корзины
   'BASKET_ITEMS' => $arResult // товары сами
);

$arOptions = array(
   'COUNT_DISCOUNT_4_ALL_QUANTITY' => "Y",
);

$arErrors = array();

CSaleDiscount::DoProcessOrder($arOrder, $arOptions, $arErrors);


echo $arOrder["ORDER_PRICE"];?>

2)
<?$basket = \Bitrix\Sale\Basket::loadItemsForFUser(
  \Bitrix\Sale\Fuser::getId(),
  \Bitrix\Main\Context::getCurrent()->getSite()
); // текущая корзина
$fuser = new \Bitrix\Sale\Discount\Context\Fuser($basket->getFUserId(true));
$discounts = \Bitrix\Sale\Discount::buildFromBasket($basket, $fuser);
$discounts->calculate();
$result = $discounts->getApplyResult(true);
$prices = $result['PRICES']['BASKET']; // цены товаров с учетом скидки
print_r($prices);



foreach ($prices as $price):

//echo $price[PRICE];?><br>
<?
$summs[]=$price[PRICE];
?>
<?endforeach;?>



<?
$a = array($summs);
$summsss = array_sum($summs);
echo $summsss;?>

About option working

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question