Skip to content

Instantly share code, notes, and snippets.

@sukhikh18
Created December 19, 2018 12:23
Show Gist options
  • Save sukhikh18/66e553615514b95b0d09149abc01cf20 to your computer and use it in GitHub Desktop.
Save sukhikh18/66e553615514b95b0d09149abc01cf20 to your computer and use it in GitHub Desktop.
<?if (!defined("B_PROLOG_INCLUDED") || B_PROLOG_INCLUDED !== true)die();
/**
* New bitrix checkout component
*/
use \Bitrix\Main\Localization\Loc;
use \Bitrix\Main\Loader;
class customOrderComponent extends CBitrixComponent
{
/**
* @var \Bitrix\Sale\Order
*/
public $order;
/**
* @var array
*/
public $propMap = [];
/**
* @var array
*/
protected $errors = [];
function __construct($component = null)
{
parent::__construct($component);
if(!Loader::includeModule('sale')){
$this->errors[] = 'No sale module';
};
if(!Loader::includeModule('catalog')){
$this->errors[] = 'No catalog module';
};
}
function onPrepareComponentParams($arParams)
{
if (isset($arParams['PERSON_TYPE_ID']) && intval($arParams['PERSON_TYPE_ID']) > 0) {
$arParams['PERSON_TYPE_ID'] = intval($arParams['PERSON_TYPE_ID']);
} else {
if (intval($this->request['payer']['person_type_id']) > 0) {
$arParams['PERSON_TYPE_ID'] = intval($this->request['payer']['person_type_id']);
} else {
$arParams['PERSON_TYPE_ID'] = 1;
}
}
return $arParams;
}
public function getPropByCode($code)
{
$result = false;
$propId = 0;
if (isset($this->propMap[$code])) {
$propId = $this->propMap[$code];
}
if ($propId > 0) {
$result = $this->order
->getPropertyCollection()
->getItemByOrderPropertyId($propId);
}
return $result;
}
public function getPropDataByCode($code)
{
$result = [];
$propId = 0;
if (isset($this->propMap[$code])) {
$propId = $this->propMap[$code];
}
if ($propId > 0) {
$result = $this->order
->getPropertyCollection()
->getItemByOrderPropertyId($propId)
->getFieldValues();
}
return $result;
}
protected function createVirtualOrder()
{
global $USER;
try {
$siteId = \Bitrix\Main\Context::getCurrent()->getSite();
$basketItems = \Bitrix\Sale\Basket::loadItemsForFUser(
\CSaleBasket::GetBasketUserID(),
$siteId
)
->getOrderableItems();
/**
* Redirect if cart is empty
*/
// if (count($basketItems) == 0) {
// LocalRedirect(PATH_TO_BASKET);
// }
$this->order = \Bitrix\Sale\Order::create($siteId, $USER->GetID());
$this->order->setPersonTypeId($this->arParams['PERSON_TYPE_ID']);
$this->order->setBasket($basketItems);
$this->setOrderProps();
// $this->setOrderShipment();
$this->setOrderPayment();
}
catch (\Exception $e) {
$this->errors[] = $e->getMessage();
}
}
protected function setOrderProps()
{
global $USER;
$arUser = $USER->GetByID(intval($USER->GetID()))
->Fetch();
if (is_array($arUser)) {
$fio = $arUser['LAST_NAME'] . ' ' . $arUser['NAME'] . ' ' . $arUser['SECOND_NAME'];
$fio = trim($fio);
$arUser['FIO'] = $fio;
}
foreach ($this->order->getPropertyCollection() as $prop) {
/** @var \Bitrix\Sale\PropertyValue $prop */
$this->propMap[$prop->getField('CODE')] = $prop->getPropertyId();
/** @var \Bitrix\Sale\PropertyValue $prop */
$value = '';
// is built it
// $prop->isUtil()
/**
* Custom prefetch
*/
// switch ($prop->getField('CODE')) {
// case 'FIO':
// $value = $this->request['contact']['family'];
// $value .= ' ' . $this->request['contact']['name'];
// $value .= ' ' . $this->request['contact']['second_name'];
// $value = trim($value);
// if (empty($value)) {
// $value = $arUser['FIO'];
// }
// break;
// default:
// }
/**
* Get value from request by property code
*/
if (empty($value)) {
foreach ($this->request as $key => $val) {
if (strtolower($key) == strtolower($prop->getField('CODE'))) {
$value = $val;
}
}
}
if (empty($value)) {
$value = $prop->getProperty()['DEFAULT_VALUE'];
}
if (!empty($value)) {
$prop->setValue($value);
}
}
}
protected function setOrderShipment()
{
/* @var $shipmentCollection \Bitrix\Sale\ShipmentCollection */
$shipmentCollection = $this->order->getShipmentCollection();
if (intval($this->request['delivery_id']) > 0) {
$shipment = $shipmentCollection->createItem(
Bitrix\Sale\Delivery\Services\Manager::getObjectById(
intval($this->request['delivery_id'])
)
);
} else {
$shipment = $shipmentCollection->createItem();
}
/** @var $shipmentItemCollection \Bitrix\Sale\ShipmentItemCollection */
$shipmentItemCollection = $shipment->getShipmentItemCollection();
$shipment->setField('CURRENCY', $this->order->getCurrency());
foreach ($this->order->getBasket()->getOrderableItems() as $item) {
/**
* @var $item \Bitrix\Sale\BasketItem
* @var $shipmentItem \Bitrix\Sale\ShipmentItem
* @var $item \Bitrix\Sale\BasketItem
*/
$shipmentItem = $shipmentItemCollection->createItem($item);
$shipmentItem->setQuantity($item->getQuantity());
}
}
protected function setOrderPayment()
{
if (intval($this->request['payment_id']) > 0) {
$paymentCollection = $this->order->getPaymentCollection();
$payment = $paymentCollection->createItem(
Bitrix\Sale\PaySystem\Manager::getObjectById(
intval($this->request['payment_id'])
)
);
$payment->setField("SUM", $this->order->getPrice());
$payment->setField("CURRENCY", $this->order->getCurrency());
}
}
function executeComponent()
{
$this->createVirtualOrder();
if (isset($this->request['save']) && $this->request['save'] == 'Y') {
$this->order->save();
}
$this->includeComponentTemplate();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment