Skip to content

Instantly share code, notes, and snippets.

@americanidiot
Created October 8, 2015 11:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save americanidiot/678a8730ca74db7c3d9e to your computer and use it in GitHub Desktop.
Save americanidiot/678a8730ca74db7c3d9e to your computer and use it in GitHub Desktop.
add2basket
<?
/*************************************************************************
Processing of the Buy link
*************************************************************************/
?>
<?require_once($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_before.php");?>
<?
use Bitrix\Main\Loader;
$arParams["OFFERS_CART_PROPERTIES"] = array(
0 => "COLOR_REF",
);
$arParams["IBLOCK_ID"] = 2;
$app = \Bitrix\Main\Application::getInstance();
$request = $app->getContext()->getRequest();
$strError = '';
$successfulAdd = true;
if (!is_null( $request->get('action')) && !is_null( $request->get('id')))
{
$action = $request->get('action');
$productID = (int)$request->get('id');
if($action == "ADD2BASKET" && $productID > 0)
{
if (Loader::includeModule("sale") && Loader::includeModule("catalog"))
{
$QUANTITY = 0;
$product_properties = array();
$arRewriteFields = array();
$intProductIBlockID = (int)CIBlockElement::GetIBlockByID($productID);
if ($intProductIBlockID != $arParams["IBLOCK_ID"])
{
if (!empty($arParams["OFFERS_CART_PROPERTIES"]) )
{
$skuAddProps = '';
$product_properties = CIBlockPriceTools::GetOfferProperties(
$productID,
$arParams["IBLOCK_ID"],
$arParams["OFFERS_CART_PROPERTIES"],
$skuAddProps
);
}
}
if (!is_null($request->get('quantity')))
{
$QUANTITY = doubleval($request->get('quantity'));
}
if (0 >= $QUANTITY)
{
$rsRatios = CCatalogMeasureRatio::getList(
array(),
array('PRODUCT_ID' => $productID),
false,
false,
array('PRODUCT_ID', 'RATIO')
);
if ($arRatio = $rsRatios->Fetch())
{
$intRatio = (int)$arRatio['RATIO'];
$dblRatio = doubleval($arRatio['RATIO']);
$QUANTITY = ($dblRatio > $intRatio ? $dblRatio : $intRatio);
}
}
if (0 >= $QUANTITY)
$QUANTITY = 1;
if ( !Add2BasketByProductID ( $productID, $QUANTITY, $arRewriteFields, $product_properties ) )
{
$successfulAdd = false;
}
if ( $successfulAdd )
{
$addResult = array ( 'STATUS' => 'OK');
}
else
{
$addResult = array ( 'STATUS' => 'ERROR' );
}
echo \Bitrix\Main\Web\Json::encode($addResult);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment