Answer the question
In order to leave comments, you need to log in
How do I apply a coupon at checkout?
there is a certain hardcode that turns the data from the form into an order for the goods.
Everything works and everyone is happy with everything.
You need to add the ability to add a coupon to it.
I added the following:
//купон
$coupon = "testtest";
\Bitrix\Sale\DiscountCouponsManager::add($coupon);
$oBasket = \Bitrix\Sale\Basket::loadItemsForFUser(
\Bitrix\Sale\Fuser::getId(),
\Bitrix\Main\Context::getCurrent()->getSite()
);
$discounts = $order->getDiscount();
$discounts->calculate();
// // купон
$formId = 10;
if ($WEB_FORM_ID != $formId) return;
$orderIdStr = trim($arrVALUES['form_hidden_52']);
if (!$orderIdStr) return;
$matches = null;
$pattern = '/\\[(\\d+)\\].*/';
$res = preg_match($pattern, $orderIdStr, $matches);
if (!$matches[1]) return;
\Bitrix\Main\Loader::includeModule('sale');
\Bitrix\Main\Loader::includeModule('catalog');
\Bitrix\Main\Loader::includeModule('iblock');
$productId = $matches[1];
$name = $arrVALUES['form_text_49'];
$phone = $arrVALUES['form_text_50'];
$email = $arrVALUES['form_email_51'];
$location = $arrVALUES['LOCATION'];
$location_id = $arrVALUES['LOCATION_ID'];
global $siteId;
$prod = \Bitrix\Iblock\ElementTable::getList(array(
'filter' => ['ID' => $productId],
'select' => ['NAME'],
))->Fetch();
$product = [
'NAME' => $prod['NAME'],
'CURRENCY' => 'RUB',
'QUANTITY' => 1,
'PRODUCT_PROVIDER_CLASS' => '\Bitrix\Catalog\Product\CatalogProvider',
];
$basket = Bitrix\Sale\Basket::create($siteId);
$item = $basket->createItem("catalog", $productId);
$item->setFields($product);
$currencyCode = \Bitrix\Currency\CurrencyManager::getBaseCurrency();
if ($arFields['USER_AUTH'] == "'Y'") {
$registeredUserID = $arFields['USER_ID'];
} else {
$registeredUserID = Site::createUser([
'NAME' => $name,
'PERSONAL_PHONE' => $phone,
'EMAIL' => $email,
]);
}
if (!$registeredUserID) {
array("STATUS" => "ERROR", "MESSAGE" => "Ошибка при оформлении заказа");
}
$order = \Bitrix\Sale\Order::create($siteId, $registeredUserID);
$order->setPersonTypeId(1);
$order->setBasket($basket);
$order->setField('CURRENCY', $currencyCode);
$order->setField('STATUS_ID', 'PR'); //предзаказ
$order->setField('COMMENTS', 'ГОРОД ЗАКАЗА: (' . $location_id . ")" . $location);
// Устанавливаем свойства
$info = ['YOUR_FAMILY' => $name, 'YOUR_PHONE' => $phone, 'YOUR_EMAIL' => $email, 'LOCATION' => $location];
$c = $order->getPropertyCollection();
foreach ($c->getGroups() as $g) foreach ($c->getGroupProperties($g['ID']) as $p) {
$a = $p->getProperty()['CODE'];
if (isset($info[$a])) $p->setValue($info[$a]);
}
$shipmentCollection = $order->getShipmentCollection();
$shipment = $shipmentCollection->createItem(
Bitrix\Sale\Delivery\Services\Manager::getObjectById(1)
);
$shipmentItemCollection = $shipment->getShipmentItemCollection();
foreach ($basket as $basketItem) {
$item = $shipmentItemCollection->createItem($basketItem);
$item->setQuantity($basketItem->getQuantity());
}
$paymentCollection = $order->getPaymentCollection();
$payment = $paymentCollection->createItem(
Bitrix\Sale\PaySystem\Manager::getObjectById(6) //Банковской картой на сайте
);
$payment->setField("SUM", $order->getPrice());
$payment->setField("CURRENCY", $order->getCurrency());
//купон
$coupon = "testtest";
\Bitrix\Sale\DiscountCouponsManager::add($coupon);
$oBasket = \Bitrix\Sale\Basket::loadItemsForFUser(
\Bitrix\Sale\Fuser::getId(),
\Bitrix\Main\Context::getCurrent()->getSite()
);
$discounts = $order->getDiscount();
$discounts->calculate();
// // купон
$result = $order->save();
global $APPLICATION;
$APPLICATION->RestartBuffer();
if ($result->getId()) {
$accountNum = $order->getField("ACCOUNT_NUMBER");
echo "Ваш заказ {$accountNum} оформлен";
} else {
echo 'Ошибка создания предзаказа';
}
die();
Answer the question
In order to leave comments, you need to log in
Here is the coupon block earned
//купон
\Bitrix\Sale\DiscountCouponsManager::add($coupon);
$discounts = $order->getDiscount();
$discounts->setOrderRefresh(true);
$discounts->calculate();
$basket->refreshData(["PRICE", "COUPONS"]);
$order->doFinalAction(true);
// // купон
See an example of adding https://dev.1c-bitrix.ru/api_d7/bitrix/sale/classe... , I think just for your task.
You are using a coupon, without specifying where, without recalculation, then for some reason getting a new basket object for the current user, which is not used anywhere. And check the coupon is working, the product in the cart matches the conditions
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question