C
C
countZer02020-08-13 11:11:13
1C-Bitrix
countZer0, 2020-08-13 11:11:13

How to edit a property when creating an order?

Hello !

I'm trying to edit a property when creating an order:

use Bitrix\Main; 
Main\EventManager::getInstance()->addEventHandler(
    'sale',
    'OnSaleOrderBeforeSaved',
    'onBeforeOrderSaveHandler'
);

function onBeforeOrderSaveHandler(Main\Event $event)
{

    /** @var \Bitrix\Sale\Order $order */
    $order = $event->getParameter('ENTITY');
    $oldPrice = $order->getField('PRICE');
    $newval = CCurrencyRates::ConvertCurrency($oldPrice, "EUR", "RUB");
    $newPrice = round($newval);
    $propertyCode = 'new_price';

    /** @var \Bitrix\Sale\PropertyValue $property */
    foreach ($order->getPropertyCollection() as $property) {
        if ($property->getField('CODE') === $propertyCode) {
            $property->setValue($newPrice);
        }
    }

    $saveResult = $order->getPropertyCollection()->save();
}


But when creating a new order, I get an error:
5f34f495c130e792486188.jpeg

A new order is not created, it just hangs in an endless loading. An error in the console complains about this function:
function submitOrder(form) {
$('.js_ord').hide();
        $('#preloader').show();
        var pric = $("#price_delivery_id").val();
        if($("#deliveryTypesBlock .selected input[name='DELIVERY_ID']").data("ourdelivery") != 1){
          $("#price_delivery_id").val(0);
        }
        if($('[name="DELIVERY_ID"]:checked').val() == 2){

            $("#price_delivery_id").val(pric);
            $("input[name='ORDER_PROP_22']").val(pric)

        }
        var fname = $("input[name='ORDER_PROP_8']").val();
        var lname = $("input[name='ORDER_PROP_9']").val();
        var email = $("input[name='ORDER_PROP_2']").val();

        $.ajax({
            method: "POST",
            url: "/ajax/mailchump_integrate.php",
            data: { email: email, fname: fname, lname: lname }
        });

            var err=false;
            $('.pane.last').find('.pane-errors ul').empty()
            $('.pane').each(function(){
                var $pane = $(this);
                errors = validatePane($pane, "required")
                if(errors.length) {
                    for(var i = 0, l = errors.length; i < l; ++i) {
                        $('.pane.last').find('.pane-errors ul').append($("<li>" + errors[i] + "</li>"));
                        err =true;
                    }
                }
            })
            if(err)return false;
        $( '#price_delivery_id' ).val( 0 );
        var data = $(form).serialize();

        $.ajax({
            url: $(form).attr('action'),
            data: data,
            type: "post",
            success: function(response) {
                console.log(response);

                 if(response['order']["REDIRECT_URL"]) {
                    location.href = response['order']["REDIRECT_URL"];
                 }

                var propertyErrors = response['order']["ERROR"]["PROPERTY"];

                if(propertyErrors) {
                    for(var i in propertyErrors) {
                        $('.errors ul').append($("<li>" + propertyErrors[i] + "</li>"))
                    }
                }
            }
        })
    }

Tell me what's the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
countZer0, 2020-08-14
@countZer0

Understood, can be useful to someone. In the settings of the online store module, it was necessary to enable the processing of obsolete events. After that everything worked.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question