Your IP : 3.142.55.204


Current Path : /home/bitrix/ext_www/klimatlend.ua/ajax/
Upload File :
Current File : /home/bitrix/ext_www/klimatlend.ua/ajax/onecklick.php

<?require($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_before.php");
?>

<!---->
<?//
Bitrix\Main\Loader::includeModule('sale');
Bitrix\Main\Loader::includeModule('catalog');

use Bitrix\Main\Context,
    Bitrix\Currency\CurrencyManager,
    Bitrix\Sale\Order,
    Bitrix\Sale\Basket,
    Bitrix\Sale\Delivery,
    Bitrix\Sale\PaySystem;



$basket = \Bitrix\Sale\Basket::loadItemsForFUser(
    \Bitrix\Sale\Fuser::getId(),
    \Bitrix\Main\Context::getCurrent()->getSite()
);

$items = array();

$orderBasket = $basket->getOrderableItems();
foreach ($basket as $basketItem) {
    $items [] = array("ID"=> $basketItem->getProductId(), "Q"=> $basketItem->getQuantity()) ;
}

$product_id = $_GET["prod_id"];

$ID = "";
if ($USER->IsAuthorized()){
    global $USER;
    $ID = $USER->GetID();
}else{
    // create user
    global $USER;
    $user = new CUser;

    $name = $_GET["name0"];
    $phone = $_GET["phone0"];
    if($name == "" || $phone == "") die();

    $arFields = Array(
        "NAME"              => $name,
        "PERSONAL_PHONE"    => $phone,
        "EMAIL"             => $phone."@"."noemail.com",
        "LOGIN"             => $phone,
        "LID"               => "ru",
        "ACTIVE"            => "Y",
        "PASSWORD"          => $phone,
        "CONFIRM_PASSWORD"  => $phone,
    );

    $ID = $user->Add($arFields);
//    if (intval($ID) > 0)
////        echo "Пользователь успешно добавлен.".$ID;
//    else
////        echo $user->LAST_ERROR;

    $user->Authorize($ID); // авторизуем



}


$basket = Bitrix\Sale\Basket::create(SITE_ID);
$order = Bitrix\Sale\Order::create(SITE_ID,$ID);
$order->setPersonTypeId($ID);
if ($product_id != "") {

    $currencyCode = CurrencyManager::getBaseCurrency();
    $siteId = Context::getCurrent()->getSite();

    $item = $basket->createItem('catalog', $product_id);
    $item->setFields(array(
        'QUANTITY' => 1,
        'CURRENCY' => $currencyCode,
        'LID' => $siteId,
        'PRODUCT_PROVIDER_CLASS' => '\CCatalogProductProvider',
    ));
}else{
    if (!empty($items)) {

        $currencyCode = CurrencyManager::getBaseCurrency();
        $siteId = Context::getCurrent()->getSite();

        foreach ($items as $item0){
            $item = $basket->createItem('catalog', $item0["ID"]);
            $item->setFields(array(
                'QUANTITY' => $item0["Q"],
                'CURRENCY' => $currencyCode,
                'LID' => $siteId,
                'PRODUCT_PROVIDER_CLASS' => '\CCatalogProductProvider',
            ));
        }


    }
}


$order->setBasket($basket);

if( $_GET["type"] == 1 )
    $order->setField('COMMENTS', 'Заказ оформлен через ФОРМУ.  "Купить в один клик в всплывающей корзине" ');

if( $_GET["type"] == 2 )
    $order->setField('COMMENTS', 'Заказ оформлен через ФОРМУ.  "Купить в один клик" ');

if( $_GET["type"] == 3 ) {
    $order->setField('COMMENTS', 'Заказ оформлен через ФОРМУ.  "Нашел дешевле" URL товара, которий дешевле: ' . $_GET["prod_url"]);
}

if( $_GET["type"] == 4 ) {
    $order->setField('COMMENTS', 'Заказ оформлен через ФОРМУ.  "Акция - получите скидку"');
}


$result = $order->save();
if (!$result->isSuccess())
{
    $result->getErrors();
}else{
    echo "ok";
}