V
V
Vadim Timoshenko2017-01-12 19:02:38
PHP
Vadim Timoshenko, 2017-01-12 19:02:38

Why doesn't PREVIEV_PICTURE show up in BITRIX?

Got a site on Bitrix. Haven't come across it yet.
When you add a product to the cart, it is added to the session.
All data passes, but the preview image does not pass.
Added with this code:

<?php
require_once($_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/main/include/prolog_before.php');
require_once $_SERVER['DOCUMENT_ROOT'] . '/autoloader.php';
use BitrixError\BitrixError;
use EndingWord\EndingWord;

$req = json_decode(file_get_contents('php://input'), true);

$arProducts = $req['product'];

if (empty($arProducts)) {
    $err400 = new BitrixError(400);
    $err400->out(array('msg' => 'Не указан товар.'));
}

if (empty($_SESSION['samogonovo']['cart']['items'])) {
    $_SESSION['samogonovo']['cart']['items'] = array();
}

$arDiscounts = array();
require_once $_SERVER['DOCUMENT_ROOT'] . '/cabinet/cache_discounts_sum.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/cabinet/total_sum.php';
$discountIndividual = 0;
foreach ($arDiscounts as $item) {
    if ($totalSum > $item['sum']) {
        $discountIndividual = $item['discount'];
    }
}
$_SESSION['samogonovo']['cart']['discount_individual'] = $discountIndividual;

CModule::IncludeModule('iblock');
$cElem = new CIBlockElement;
$arFilter = array(
    'IBLOCK_ID' => CFG::IB_CATALOG,
);
$arSelect = array(
    '*', 'PROPERTY_PRICE', 'PROPERTY_PRICE_OLD', 'PROPERTY_DISCOUNT'
);
foreach ($arProducts as $item) {
    // найдем товар и добавим в корзину, если есть
    $id = (int)$item['id'];
    if (!$id) {
        continue;
    }
    $arFilter['ID'] = $id;
    $arProd = $cElem->GetList(array(), $arFilter, false, false, $arSelect)->GetNext();
    if (empty($arProd)) {
        $err404 = new BitrixError(404);
        $err404->out(array('msg' => 'Товар не найден.'));
    }
    if (empty($_SESSION['samogonovo']['cart']['items'][$arProd['ID']])) {
        $discount = (int)$arProd['PROPERTY_DISCOUNT_VALUE'];
        $price = (int)$arProd['PROPERTY_PRICE_VALUE'];
        $price_old = (int)$arProd['PROPERTY_PRICE_OLD_VALUE'];
        if ($discount) {
            $price_old = $price;
            $price = round($price - $price / 100 * $discount);
        }
        $img = CFG::NOPHOTO_CATALOG;
        $imgSize = 'initial';
        $imgColor = '#ebebeb';
        if ($arElem['PREVIEW_PICTURE']) {
            $img = $cFile->GetPath($arProd['PREVIEW_PICTURE']);
            $imgSize = 'contain';
            $imgColor = 'transparent';
        }
        $_SESSION['samogonovo']['cart']['items'][$arProd['ID']] = array(
            'id' => $arProd['ID'],
            'name' => $arProd['NAME'],
            'img' => $img,
            'img_size' => $imgSize,
            'img_color' => $imgColor,
            'url' => $arProd['DETAIL_PAGE_URL'],
            'discount' => $discount,
            'price' => $price,
            'price_old' => $price_old,
            'count' => (int)$item['count'],
        );
    } else {
        $_SESSION['samogonovo']['cart']['items'][$arProd['ID']]['count'] += (int)$item['count'];
    }
}
$sum = 0;
$amountProducts = 0;
foreach ($_SESSION['samogonovo']['cart']['items'] as $item) {
    $sum += $item['price'] * $item['count'];
    $amountProducts += $item['count'];
}

$ew = new EndingWord($amountProducts, array('товаров', 'товар', 'товара'));
$_SESSION['samogonovo']['cart']['amount_products'] = $amountProducts;
$_SESSION['samogonovo']['cart']['text'] = $amountProducts . ' ' . $ew->get() . ' на сумму ';
$_SESSION['samogonovo']['cart']['text'] .= number_format($sum, 0, '', ' ') . ' р.';

$result = array(
    'amount_products' => $amountProducts,
    'sum' => $sum,
    'text_cart' => $_SESSION['samogonovo']['cart']['text'],
    'msg' => 'Товар добавлен в корзину.',
);

echo json_encode($result);
//unset($_SESSION['samogonovo']['cart']);
require_once($_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/main/include/epilog_after.php');

In the product, the preview image is set.
Tell me what documentation to read? Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Grechushnikov, 2017-01-13
@PbI6A_KuT

Let's begin with
the getlist method is static CIBlockElement::GetList
and calling GetNext() without checking will drop your cart if the product is not found for some reason.
regarding the question.
In addition to GetNext, look into other methods for getting data (GetNextElement etc.) and try explicitly specifying the preview_picture selection

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question