Answer the question
In order to leave comments, you need to log in
How to hide online payment by card for bulk goods?
Make a condition in the ordering process if there are goods by weight in the order, i.e. goods with a quantitative measurement coefficient (all except pcs) - kg, l, gram, etc., then Online payment by card http://joxi.ru/YmEjgOdtMQRkL2?d=1
must be deactivated. Unfortunately, I didn’t find such a condition in the restrictions - http://joxi.ru/bmowRGeHyZJLkm?d=1
or how to make a weight restriction,
so I started writing a class, but it doesn’t work $weight is not transmitted
<?php
//php_interface/classes/restrictions/payments.php
use Bitrix\Sale\Delivery\Restrictions;
use Bitrix\Sale\Services\Base;
use Bitrix\Sale\Internals\CollectableEntity;
use Bitrix\Sale\Internals\Entity;
use Bitrix\Sale\Order;
use Bitrix\Main\Localization\Loc;
use Bitrix\Sale\Shipment;
* Class ByWeight
* Restricts delivery by weight
* @package Bitrix\Sale\Delivery\Restrictions
*/
class checkvesovie extends Base\Restriction
{
public static function getClassTitle()
{
return Loc::getMessage("SALE_DLVR_RSTR_BY_WEIGHT_NAME");
}
public static function getClassDescription()
{
return Loc::getMessage("SALE_DLVR_RSTR_BY_WEIGHT_DESCRIPT");
}
public static function check($weight, array $restrictionParams, $deliveryId = 0)
{
if(empty($restrictionParams))
return true;
$weight = floatval($weight);
if(isset($restrictionParams["MIN_WEIGHT"]) && floatval($restrictionParams["MIN_WEIGHT"]) > 0 && $weight < floatval($restrictionParams["MIN_WEIGHT"]))
return false;
if(isset($restrictionParams["MAX_WEIGHT"]) && floatval($restrictionParams["MAX_WEIGHT"]) > 0 && $weight > floatval($restrictionParams["MAX_WEIGHT"]))
return false;
return true;
}
protected static function extractParams(Entity $entity)
{
if (!($entity instanceof Shipment))
return false;
return $entity->getWeight();
}
public static function getParamsStructure($entityId = 0)
{
return array(
"MIN_WEIGHT" => array(
'TYPE' => 'NUMBER',
'DEFAULT' => "0",
'MIN' => 0,
'LABEL' => "минимальный вес"
),
"MAX_WEIGHT" => array(
'TYPE' => 'NUMBER',
'DEFAULT' => "0",
'MIN' => 0,
'LABEL' => "максимальный вес"
)
);
}
}
Answer the question
In order to leave comments, you need to log in
You went in the right direction - the implementation of the constraint.
Here is the code. Written but not verified. In theory it should work. I redid it quickly according to the model from another restriction.
Connecting an event handler:
$bxEventManager = \Bitrix\Main\EventManager::getInstance();
$bxEventManager->addEventHandler(
'sale',
'onSalePaySystemRestrictionsClassNamesBuildList',
array(
\Gricuk\Sale\Payment\Restrictions\ByMeasure::class,
"onSalePaySystemRestrictionsClassNamesBuildList"
)
);
<?php
namespace Gricuk\Sale\Payment\Restrictions;
use Bitrix\Catalog\MeasureTable;
use Bitrix\Sale\Internals\Entity;
use Bitrix\Sale\Payment;
/** Ограничение по единицам измерения
* Class ByMeasure
* @package Gricuk\Sale\Payment\Restrictions
*/
class ByMeasure extends \Bitrix\Sale\Services\Base\Restriction
{
public static function onSalePaySystemRestrictionsClassNamesBuildList()
{
return new \Bitrix\Main\EventResult(
\Bitrix\Main\EventResult::SUCCESS,
array(
self::class => '/local/php_interface/lib/Gricuk/Sale/Payment/Restrictions/ByMeasure.php',
)
);
}
public static function getClassTitle()
{
return 'По единицам измерения товаров в корзине';
}
public static function getClassDescription()
{
return 'Оплата будет НЕ доступна для указанных единиц измерения';
}
/** Выполняет проверку на наличие в массиве $measures единиц измерения, указанных в настройках ограничения
* @param $userId
* @param array $restrictionParams
* @param int $serviceId
* @return bool - true нет ограничения, единиц измерения из настроек нет у товаров в корзине.
* false - ограничение сработало, в корзине есть товары с указанными единицами измерения
*/
public static function check($measures, array $restrictionParams, $serviceId = 0)
{
if (empty($restrictionParams["MEASURES"]) || empty($measures)) {
return true;
}
if (count(array_intersect($measures, $restrictionParams["MEASURES"])) > 0) {
return false;
} else {
return true;
}
}
/**Преобразует $entity в то что будет передано в первый параметр метода self::check
* @param Entity $paysystem
* @return mixed|string|null
* @throws \Bitrix\Main\ArgumentException
* @throws \Bitrix\Main\NotImplementedException
*/
protected static function extractParams(Entity $entity)
{
$measures = [];
if ($entity instanceof Payment) {
$order = $entity->getOrder();
if (!$order) {
return $measures;
}
$products = [];
/** @var \Bitrix\Sale\BasketItem $basketItem */
foreach ($order->getBasket() as $basketItem) {
$products[] = $basketItem->getProductId();
}
if (!empty($products)) {
$measures = array_column(\Bitrix\Catalog\ProductTable::getList([
"select" => ["MEASURE"],
"filter" => [
"ID" => $products
]
])->fetchAll(), "MEASURE");
}
}
return array_unique($measures);
}
/** Возвращает массив параметров, описывающий ограничение
* @param int $entityId
* @return array
*/
public static function getParamsStructure($entityId = 0)
{
$result = array(
"MEASURES" => array(
"TYPE" => "ENUM",
'MULTIPLE' => 'Y',
"LABEL" => "Единицы измерения",
"OPTIONS" => self::getMeasures()
)
);
return $result;
}
/**Возвращает массив единиц измерения
* @return array|mixed
* @throws \Bitrix\Main\ArgumentException
* @throws \Bitrix\Main\ObjectPropertyException
* @throws \Bitrix\Main\SystemException
*/
public static function getMeasures()
{
static $result = null;
if ($result !== null)
return $result;
$result = [];
$dbResultList = MeasureTable::getList(array(
'select' => array("ID", "MEASURE_TITLE"),
'order' => array("ID" => "ASC")
));
while ($measure = $dbResultList->fetch()) {
$result[$measure["ID"]] = $measure["MEASURE_TITLE"];
}
return $result;
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question