Z
Z
Zzpro2020-11-25 13:18:13
1C-Bitrix
Zzpro, 2020-11-25 13:18:13

How can I change a product after placing an order?

The task is to display how many people bought the product.

As far as it is necessary, so that it would be possible to set the default or change the quantity, I want to do it through the product property and add +1 after each order.

As far as I understand, it is required to write code in init.php that will change the product property after the order is placed. But I can not find how to make the code work after the order is placed.

If anyone has a similar code or can distribute it, I would be grateful.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton, 2020-11-25
@Zzpro

Keep a very bad porridge that you haven't checked. Refine.

AddEventHandler("sale", "OnSaleStatusOrder", Array("Zzpro", "habr")); 
CModule::IncludeModule('sale');
class Zzpro
{ 
function habr($ID, $val)
{if ($val == "F") { 
//F замени на статус заказа при котором должно происходить действие, в моем случае это доставлен
use Bitrix\Sale;
$order = Sale\Order::load($ID);
$basket = \Bitrix\Sale\Order::load($ID)->getBasket();
//загрузили заказ, смотрим его состав
foreach ($basket as $basketItem) {
$dbItem = \Bitrix\Iblock\ElementTable::getList(array(
    'select' => array('ID', 'IBLOCK_ID', 'NAME'),
    'filter' => array('ID' => $basketItem->getProductId()),
    'limit' => 10,
    'order' => array('TIMESTAMP_X' => 'ASC')
));
while ($arItem = $dbItem->fetch()) {
//echo $arItem[ID];
//echo $basketItem->getQuantity();
//получили ID товара и количество
$db_props = CIBlockElement::GetProperty(8, $arItem[ID], Array("CODE"=>"KUPLENO"));
//8 это твой инфоблок, KUPLENO это код твоего свойства, нам нужно получить кол-во ранее записанное
if($ar_props = $db_props->Fetch()) $KUPLENO = IntVal($ar_props["VALUE"]);
//Берем прежнее значение, и добавляем сколько куплено
//почему не +1? потому что могли же купить 10))
$property_value = $KUPLENO + $basketItem->getQuantity() ;
CIBlockElement::SetPropertyValuesEx($arItem[ID], false, array($property_id => $property_value));
//запишем в свойства товара новое значение
//$property_id это id свойства KUPLENO
}
}}};

A
Alexey, 2020-11-25
@AlexeyCaTHaR

Wouldn't it be easier then to hang up a handler to change the order status to Completed (we only need successful orders, not all completed ones)? Then look at the order basket and the quantity of goods and then update the property with the desired number.
Well, or as an option, hang a script on a schedule that will collect information on all sold goods and store somewhere to update the data for all tracked goods

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question