T
T
tgarl2020-02-17 23:49:57
1C-Bitrix
tgarl, 2020-02-17 23:49:57

How to create a 10% discount in the cart only for products of added lines more than 2 pieces?

There are 200+ lines. If the client adds several products of the same line, you need to assign a discount to such products.
I decided through a custom discount condition. But in this case, it returns either true or false, and accordingly the discount is applied/not applied to the entire cart. How to make only those products that fit the condition?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
tgarl, 2020-02-20
@tgarl

I decided.
1. There is an event for actions and conditions. It is necessary to distinguish when when and what to use (this was my main mistake)
2. in my case, you need to use OnCondSaleActionsControlBuildList, not OnCondSaleControlBuildList
3. the solution itself:

public static function Generate($arConditions, $arParams, $control, $subs = false)
    {

        $resultValues = array();
        $arValues = false;

        if (is_string($arControl))
        {
            $arControl = static::GetControls($arControl);
        }
        $boolError = !is_array($arControl);

        if (!$boolError)
        {
            $arValues = static::Check($arOneCondition, $arOneCondition, $arControl, false);
            $boolError = (false === $arValues);
        }


        if($arOneCondition['logic']=='Equal')
        {
            $logic='true';
        }
        else
        {
            $logic='false';
        }
$strResult  = '(SaleActionDeactivateDiscount::qDiscAction('.$arParams['BASKET_ROW'].','.$arConditions['value'].'))=='.$logic;
        return  $strResult;

    }
 public static function qDiscAction($row,$arCond)
    {
//пришлось использовать \Bitrix\Sale\Basket::loadItemsForFUser, т.к. кроме $arParams['BASKET_ROW'] другие параметры по какой-то причине пустые и при попытке их передать в свою функцию напрочь ломают скидки
    $basket = \Bitrix\Sale\Basket::loadItemsForFUser(\Bitrix\Sale\Fuser::getId(), 's1');
    $linia=[];
    foreach ($basket as $basketItem) {
      $mxResult = \CCatalogSku::GetProductInfo($basketItem->getProductId())['ID'];
      $dbEl = \CIBlockElement::GetList(array('sort' => 'asc'), array('ID' => $mxResult), false, false, Array('ID','PROPERTY___2'));
      while($obEl = $dbEl->Fetch())	
      {
        $linia[$obEl['PROPERTY___2_VALUE']]['ID'][]=$basketItem->getProductId();
        $linia[$obEl['PROPERTY___2_VALUE']]['CNT']+=$basketItem->getQuantity();
      }			
    }
    
    foreach($linia as $ln=>$massa){
      if($massa['CNT']>=$arCond && in_array($row['PRODUCT_ID'],$massa['ID'])){
        return false;
      }
    }
    
       return true;
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question