HostCMS начисление или отмена бонусов за покупку в магазине

Иногда бывает необходимость поощрять клиентов бонусами за покупку в интернет-магазине. Для этого создал модуль с таблицей где будут указаны диапазоны общей стоимости заказа и бонусы за них. Например на картинке:

Как показано имеется таблица с диапазоном суммы покупок и начисляемый за такие покупки бонусные деньги.

Для того что бы все это работала:

1. Пишем класс наблюдателя, размещаем его в modules/shop/order/observer.php

class Shop_Order_Observer
{
    static public function onBeforePaid($object, $args)
    {
        if (!$object->paid)
        {

            $oShop = Core_Entity::factory('Shop', $object->shop_id);
            $oSiteuser = Core_Entity::factory('Siteuser', $object->siteuser_id);

            $sum = $object->getSum();
            // Извлекаем все активные скидки из собственного модуля Скидки
            $Discounts = Core_Entity::factory('Discount');
            $Discounts->queryBuilder()
                ->where('shop_id', '=', $oShop->id)
                ->where('active', '=', 1);

            $aDiscounts = $Discounts->findAll();

            foreach ($aDiscounts as $Discount)
            {
                // Нижний предел скидки
                $min_amount = $Discount->min_amount;
                // Верхний предел скидки
                $max_amount = $Discount->max_amount;

                if ($Discount->mode == 2)
                {
                    if(!is_null($oSiteuser))
                    {
                        $bCheckOrdersSum = $sum >= $min_amount && ($sum < $max_amount || $max_amount == 0);
                        if ($bCheckOrdersSum) {

                            Core_Message::show('Начиляемый бонус: ' . $Discount->value);

                            // Проведение/стронирование транзакции
                            $oShop_Siteuser_Transaction = Core_Entity::factory('Shop_Siteuser_Transaction');
                            $oShop_Siteuser_Transaction->queryBuilder()
                                ->where('siteuser_id', '=', $object->siteuser_id)
                                ->where('shop_order_id', '=', $object->id)
                                ->where('active', '=', 1)
                                ->where('type', '=', 2)
                                ->where('shop_id', '=', $oShop->id)
                                ->limit(1);
                            $oTransaction = $oShop_Siteuser_Transaction->findAll(FALSE);

                            if(count($oTransaction))
                            {
                                $oTransaction[0]->amount = $Discount->value;
                                $oTransaction[0]->amount_base_currency = $Discount->value;
                                $oTransaction[0]->save();
                            }
                            else
                            {
                                $oShop_Siteuser_Transaction->shop_id = $oShop->id;
                                $oShop_Siteuser_Transaction->siteuser_id = $object->siteuser_id;
                                $oShop_Siteuser_Transaction->active = 1;
                                $oShop_Siteuser_Transaction->amount = $Discount->value;
                                $oShop_Siteuser_Transaction->shop_currency_id = $object->shop_currency_id;
                                $oShop_Siteuser_Transaction->amount_base_currency = $Discount->value;
                                $oShop_Siteuser_Transaction->shop_order_id = $object->id;
                                $oShop_Siteuser_Transaction->type = 2;
                                $oShop_Siteuser_Transaction->description = Core::_('Shop_Order_Item.replenishment_account', $oSiteuser->login, $oShop->name);
                                $oShop_Siteuser_Transaction->save();
                            }

                            break;
                        }
                    }
                }
            }
        }
    }

    static public function onBeforeCancelPaid($object, $args)
    {
        // Проведение/стронирование транзакции
        $oShop_Siteuser_Transaction = Core_Entity::factory('Shop_Siteuser_Transaction');
        $oShop_Siteuser_Transaction->queryBuilder()
            ->where('siteuser_id', '=', $object->siteuser_id)
            ->where('shop_order_id', '=', $object->id)
            ->where('active', '=', 1)
            ->where('type', '=', 2)
            ->where('shop_id', '=', $object->shop_id)
            ->limit(1);
        $oTransaction = $oShop_Siteuser_Transaction->findAll(FALSE);

        if(count($oTransaction))
        {
            Core_Message::show('Отключен бонус: ' . $oTransaction[0]->amount);
            $oTransaction[0]->active = 0;
            $oTransaction[0]->save();
        }
    }

}​

2. Добавляем наблюдателя в bootstrap.php

/* Начисление бонусов */
Core_Event::attach('shop_order.onBeforePaid', array('Shop_Order_Observer', 'onBeforePaid'));
/* Отмена оплаты */
Core_Event::attach('shop_order.onBeforeCancelPaid', array('Shop_Order_Observer', 'onBeforeCancelPaid'));​
  • 3 331 просмотр