public function saveOrderProducts($d, $orderId)
{
$app = Factory::getApplication();
$db = Factory::getDbo();
$row = Table::getInstance('PhocacartOrderProducts', 'Table', array());
$checkP = PhocacartProduct::checkIfAccessPossible($d['id'], $d['catid'], $this->type);
if (!$checkP) {
$app->enqueueMessage(Text::_('COM_PHOCACART_PRODUCT_NOT_ACCESSIBLE') . ' - ' . Text::_('COM_PHOCACART_PRODUCT') . ': ' . $d['title'], 'error');
return false;
}
// Possible feature - remove this additional check based on mostly design parameters
// 1) canDisplayAddtocartAdvanced - Product on demand product cannot be ordered
// canDisplayAddtocart (is a part of 1) - Display add to cart disabled or only for specific access level or only for specific group (checked previously in checkIfAccessPossible)
// 2) canDisplayAddtocartPrice - Product with zero price cannot be added to cart (can be set in options)
// 3) canDisplayAddtocartStock - Product witz zero stock cannot be added to cart (can be set in options)
$itemP = PhocacartProduct::getProduct((int) $d['id'], $d['catid']);
$d['attributes'] = !empty($d['attributes']) ? $d['attributes'] : array();
if (!empty($itemP)) {
$price = new PhocacartPrice();
$priceP = $price->getPriceItems($itemP->price, $itemP->taxid, $itemP->taxrate, $itemP->taxcalculationtype, $itemP->taxtitle, 0, '', 1, 1, $itemP->group_price);
$aA = $d['attributes'];
// Sanitanized yet //PhocacartAttribute::sanitizeAttributeArray($d['attributes']);
$price->getPriceItemsChangedByAttributes($priceP, $aA, $price, $itemP, 1);
$price->correctMinusPrice($priceP);
$priceA = isset($priceP['brutto']) ? $priceP['brutto'] : 0;
// Stock (don't display add to cart when stock is zero)
$stockStatus = array();
$stock = PhocacartStock::getStockItemsChangedByAttributes($stockStatus, $aA, $itemP, 2);
$rights = new PhocacartAccessRights();
$can_display_addtocart = $rights->canDisplayAddtocartAdvanced($itemP);
$can_display_addtocart_price = $rights->canDisplayAddtocartPrice($itemP, $priceA);
$can_display_addtocart_stock = $rights->canDisplayAddtocartStock($itemP, $stock);
if (!$can_display_addtocart) {
$app->enqueueMessage(Text::_('COM_PHOCACART_PRODUCT_NOT_ACCESSIBLE') . ' - ' . Text::_('COM_PHOCACART_PRODUCT') . ': ' . $d['title'], 'error');
return false;
}
if (!$can_display_addtocart_price) {
$app->enqueueMessage(Text::_('COM_PHOCACART_PRICE_IS_ZERO') . ' - ' . Text::_('COM_PHOCACART_PRODUCT') . ': ' . $d['title'], 'error');
return false;
}
if (!$can_display_addtocart_stock) {
$app->enqueueMessage(Text::_('COM_PHOCACART_STOCK_IS_EMPTY') . ' - ' . Text::_('COM_PHOCACART_PRODUCT') . ': ' . $d['title'], 'error');
return false;
}
}
// Additional info
$d['default_price'] = $d['default_price'];
$d['default_tax_rate'] = $d['taxrate'];
$taxKeyA = PhocacartTax::getTaxIdsFromKey($d['taxkey']);
$d['default_tax_id'] = (int) $taxKeyA['id'];
$d['default_tax_id_c'] = (int) $taxKeyA['countryid'];
$d['default_tax_id_r'] = (int) $taxKeyA['regionid'];
$d['default_tax_calculation_rate'] = $d['taxcalctype'];
$d['default_points_received'] = $d['default_points_received'];
//$d['status_id'] = 1;// pending
$d['published'] = 1;
$d['order_id'] = (int) $orderId;
$d['product_id'] = (int) $d['id'];
$d['category_id'] = (int) $d['catid'];
$d['product_id_key'] = $d['idkey'];
$d['stock_calculation'] = $d['stockcalculation'];
unset($d['id']);
// we do new autoincrement
$d['tax'] = $d['tax'] / $d['quantity'];
// in database we store the items per item
//$d['dtax'] = $d['dtax']/$d['quantity'];// in database we store the items per item
// STOCK HANDLING
// will be set in order status administrator\components\com_phocacart\libraries\phocacart\order\status.php
//$stock = PhocacartStock::handleStockProduct($d['product_id'], $d['status_id'], $d['quantity'] );
if (!$row->bind($d)) {
//throw new Exception($row->getError());
$msg = Text::_($row->getError());
$app->enqueueMessage($msg, 'error');
return false;
}
if (!$row->check()) {
//throw new Exception($row->getError());
$msg = Text::_($row->getErrorMsg());
$app->enqueueMessage($msg, 'error');
return false;
}
if (!$row->store()) {
//throw new Exception($row->getError());
$msg = Text::_($row->getErrorMsg());
$app->enqueueMessage($msg, 'error');
return false;
}
//if ((int)$row->id > 0 && !empty($d['attributes'])) {
if ((int) $row->id > 0) {
//$this->cleanTable('phocacart_order_attributes', $orderId); NOT HERE, we are in foreach
if (empty($d['attributes'])) {
$d['attributes'] = array();
}
$checkedA = PhocacartAttribute::checkRequiredAttributes((int) $d['product_id'], $d['attributes']);
if (!$checkedA) {
return false;
}
foreach ($d['attributes'] as $k => $v) {
if (!empty($v)) {
foreach ($v as $k2 => $v2) {
$row2 = Table::getInstance('PhocacartOrderAttributes', 'Table', array());
$d2 = array();
$d2['order_id'] = (int) $orderId;
$d2['product_id'] = (int) $d['product_id'];
$d2['order_product_id'] = (int) $row->id;
$d2['attribute_id'] = (int) $v2['aid'];
$d2['option_id'] = (int) $v2['oid'];
$d2['attribute_title'] = $v2['atitle'];
$d2['type'] = $v2['atype'];
$d2['option_title'] = $v2['otitle'];
$d2['option_value'] = $v2['ovalue'];
// $d2['option_download_file'] = $v2['odownloadfile'];
// Will be set order status
// administrator\components\com_phocacart\libraries\phocacart\order\status.php
// $stockA = PhocacartStock::handleStockAttributeOption($d2['option_id'], $d['status_id'], $d['quantity'] );
if (!$row2->bind($d2)) {
//throw new Exception($row2->getError());
$msg = Text::_($row2->getError());
$app->enqueueMessage($msg, 'error');
return false;
}
if (!$row2->check()) {
//throw new Exception($row2->getError());
$msg = Text::_($row2->getError());
$app->enqueueMessage($msg, 'error');
return false;
}
if (!$row2->store()) {
//throw new Exception($row2->getError());
$msg = Text::_($row2->getErrorMsg());
$app->enqueueMessage($msg, 'error');
return false;
}
}
}
}
}
/*} else if ((int)$row->id > 0){
// Empty attributes - check if product include some required attribute
$checkA = PhocacartAttribute::checkIfExists AndRequired($d['product_id']);
if (!$checkedA) {
return false;
}
}*/
return $row->id;
}