Skip to content

Instantly share code, notes, and snippets.

@kuntashov
Created June 4, 2018 12:43
Show Gist options
  • Save kuntashov/cc1956860fab5a889da48d916ed71ffd to your computer and use it in GitHub Desktop.
Save kuntashov/cc1956860fab5a889da48d916ed71ffd to your computer and use it in GitHub Desktop.
<?php
protected function onFieldModify($name, $oldValue, $value)
{
$result = new Result();
if ($name == "QUANTITY" && $value != 0)
{
$value = (float)$value;
$oldValue = (float)$oldValue;
$deltaQuantity = $value - $oldValue;
/** @var Basket $basket */
$basket = $this->getCollection();
$context = $basket->getContext();
/** @var Result $r */
$r = Internals\Catalog\Provider::getAvailableQuantityAndPriceByBasketItem($this, $context);
if (!$r->isSuccess())
{
$result->addErrors($r->getErrors());
$result->setData($r->getData());
return $result;
}
$providerData = $r->getData();
$bImportMode = \Bitrix\Main\Context::getCurrent()->getRequest()->get('mode') == 'import';
if ($this->getField('SUBSCRIBE') !== 'Y' && !$bImportMode)
{
if (array_key_exists('AVAILABLE_QUANTITY', $providerData) && $providerData['AVAILABLE_QUANTITY'] > 0)
{
$availableQuantity = $providerData['AVAILABLE_QUANTITY'];
}
else
{
$result->addError(
new ResultError(
Localization\Loc::getMessage(
'SALE_BASKET_ITEM_WRONG_AVAILABLE_QUANTITY',
array('#PRODUCT_NAME#' => $this->getField('NAME'))
),
'SALE_BASKET_ITEM_WRONG_AVAILABLE_QUANTITY'
)
);
return $result;
}
}
else
{
$availableQuantity = $value;
}
if (!empty($providerData['PRICE_DATA']))
{
if (isset($providerData['PRICE_DATA']['CUSTOM_PRICE']))
{
$this->setField('CUSTOM_PRICE', $providerData['PRICE_DATA']['CUSTOM_PRICE']);
}
}
if ($value != 0
&& (($deltaQuantity > 0) && (roundEx($availableQuantity, SALE_VALUE_PRECISION) < roundEx($value, SALE_VALUE_PRECISION)) // plus
|| ($deltaQuantity < 0) && (roundEx($availableQuantity, SALE_VALUE_PRECISION) > roundEx($value, SALE_VALUE_PRECISION)))
) // minus
{
if ($deltaQuantity > 0)
{
$mess = Localization\Loc::getMessage(
'SALE_BASKET_AVAILABLE_FOR_PURCHASE_QUANTITY',
array(
'#PRODUCT_NAME#' => $this->getField('NAME'),
'#AVAILABLE_QUANTITY#' => $availableQuantity
)
);
}
else
{
$mess = Localization\Loc::getMessage(
'SALE_BASKET_AVAILABLE_FOR_DECREASE_QUANTITY',
array(
'#PRODUCT_NAME#' => $this->getField('NAME'),
'#AVAILABLE_QUANTITY#' => $availableQuantity
)
);
}
$result->addError(new ResultError($mess, "SALE_BASKET_AVAILABLE_QUANTITY"));
$result->setData(array("AVAILABLE_QUANTITY" => $availableQuantity, "REQUIRED_QUANTITY" => $deltaQuantity));
return $result;
}
/** @var BasketItemCollection $collection */
$collection = $this->getCollection();
/** @var BasketBase $basket */
$basket = $collection->getBasket();
if ((!$basket->getOrder() || $basket->getOrderId() == 0) && !($collection instanceof BundleCollection))
{
if ($this->getField("CUSTOM_PRICE") != "Y" && $value > 0)
{
$r = $basket->refresh(RefreshFactory::createSingle($this->getBasketCode()));
if (!$r->isSuccess())
{
$result->addErrors($r->getErrors());
return $result;
}
}
}
if ($this->getField("CUSTOM_PRICE") != "Y")
{
$providerName = $this->getProviderName();
if (strval($providerName) == '')
{
$providerName = $this->getCallbackFunction();
}
if (!empty($providerData['PRICE_DATA']))
{
if (isset($providerData['PRICE_DATA']['PRICE']))
{
$this->setField('PRICE', $providerData['PRICE_DATA']['PRICE']);
}
if (isset($providerData['PRICE_DATA']['BASE_PRICE']))
{
$this->setField('BASE_PRICE', $providerData['PRICE_DATA']['BASE_PRICE']);
}
if (isset($providerData['PRICE_DATA']['DISCOUNT_PRICE']))
{
$this->setField('DISCOUNT_PRICE', $providerData['PRICE_DATA']['DISCOUNT_PRICE']);
}
}
elseif ($providerName && !$this->isCustom())
{
$result->addError(
new ResultError(
Localization\Loc::getMessage(
'SALE_BASKET_ITEM_WRONG_PRICE',
array('#PRODUCT_NAME#' => $this->getField('NAME'))
),
'SALE_BASKET_ITEM_WRONG_PRICE'
)
);
return $result;
}
}
}
$r = parent::onFieldModify($name, $oldValue, $value);
if ($r->isSuccess())
{
if (($name === 'BASE_PRICE') || ($name === 'DISCOUNT_PRICE'))
{
if ($this->getField('CUSTOM_PRICE') !== 'Y')
{
$price = $this->getField('BASE_PRICE') - $this->getField('DISCOUNT_PRICE');
$r = $this->setField('PRICE', $price);
if (!$r->isSuccess())
$result->addErrors($r->getErrors());
}
}
}
else
{
$result->addErrors($r->getErrors());
}
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment