Skip to content

Instantly share code, notes, and snippets.

@maximzasorin
Created November 23, 2015 15:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maximzasorin/ec871e4a73da3571cd64 to your computer and use it in GitHub Desktop.
Save maximzasorin/ec871e4a73da3571cd64 to your computer and use it in GitHub Desktop.
Наблюдатель, который выводит цены товаров для текущего склада (HostCMS)
<?php
/*
*
* В bootstrap.php:
*
// Подменяем цену товара в зависимости от текущего склада
Core_Event::attach('Shop_Item_Controller.onBeforeCalculatePrice', array('Shop_Item_Observers_Regionprices', 'onBeforeCalculatePrice'));
Core_Event::attach('Shop_Item_Controller.onBeforeCalculatePriceInItemCurrency', array('Shop_Item_Observers_Regionprices', 'onBeforeCalculatePrice'));
*
*/
defined('HOSTCMS') || exit('HostCMS: access denied.');
class Kad_Shop_Item_Observers_Regionprices
{
static public function onBeforeCalculatePrice($object, $args)
{
if (
defined('PRICES_FOR_DEFAULT_WAREHOUSE_SHOP_ID')
&&
PRICES_FOR_DEFAULT_WAREHOUSE_SHOP_ID == $args[0]->shop_id
)
{
// Товар
$oShopItem = $args[0];
// Текущий склад
$oShop = Core_Entity::factory('Shop', PRICES_FOR_DEFAULT_WAREHOUSE_SHOP_ID);
$oCurrentWarehouse = $oShop->Shop_Warehouses->getDefault();
// Цена для текущего склада
$oShopPrice = Core_Entity::factory('shop_price');
$oShopPrice
->queryBuilder()
->where('name', '=', $oCurrentWarehouse->name);
$aoShopPrices = $oShopPrice->findAll();
if (count($aoShopPrices) > 0)
{
$oPriceForWarehouse = $aoShopPrices[0];
// Цена товара для текущего склада
$oShopItemPrices = $oShopItem->Shop_Item_Prices;
$oShopItemPrices->queryBuilder()
->where('shop_price_id', '=', $oPriceForWarehouse->id);
$aoShopItemPrice = $oShopItemPrices->findAll();
// Если цена для товара не установлена, то возможно это модификация,
// поэтому просматриваем цены для родительского товара
// if (count($aoShopItemPrice) == 0 && $oShopItem->modification_id != 0)
// {
// $oParentShopItem = Core_Entity::factory('shop_item', $oShopItem->modification_id);
// $oShopItemPrices = $oParentShopItem->Shop_Item_Prices;
// $oShopItemPrices->queryBuilder()
// ->where('shop_price_id', '=', $oPriceForWarehouse->id);
// $aoShopItemPrice = $oShopItemPrices->findAll();
// }
// Цена для склада все-таки есть
if (count($aoShopItemPrice) > 0)
{
$oShopItemPrice = $aoShopItemPrice[0];
$price = $oShopItemPrice->value;
// Устанавливаем цену склада
$object->setAPrice(
array(
'tax' => 0,
'rate' => 0,
'price' => $price,
'price_discount' => $price,
'price_tax' => $price,
'discount' => 0,
'discounts' => array()
)
);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment