0

I want to start work my cron job, but i have error

Mage_Core_Exception' with message 'Invalid model/method definition, expecting "model/class::method", got "DaniilKrok_Backorder_Model_Observer::backorderInStockByPriceWhereQteq" 

my config.xml

<?xml version="1.0" ?>
<config>
    <modules>
        <DaniilKrok_Backorder>
            <version>0.1.0</version>
        </DaniilKrok_Backorder>
    </modules>

    <global>
        <helpers>
            <backorder_daniilkrok>
                <class>DaniilKrok_Backorder_Helper</class>
            </backorder_daniilkrok>
        </helpers>
        <blocks>
            <backorder>
                <class>DaniilKrok_Backorder_Block</class>
            </backorder>
        </blocks>
        <resources>
            <backorder_setup>
                <setup>
                    <module>DaniilKrok_Backorder</module>
                </setup>
            </backorder_setup>
        </resources>
        <models>
            <backorder>
                <class>DaniilKrok_Backorder_Model</class>
                <resourceModel>backorder_resource</resourceModel>
            </backorder>
        </models>
        <events>
            <checkout_onepage_controller_success_action>
                <observers>
                    <backorder>
                        <class>DaniilKrok_Backorder_Model_Observer</class>
                        <method>checkout_onepage_controller_success_action</method>
                    </backorder>
                </observers>
            </checkout_onepage_controller_success_action>
            <catalog_product_save_after>
                <observers>
                    <backorder>
                        <class>DaniilKrok_Backorder_Model_Observer</class>
                        <method>catalog_product_save_after</method>
                    </backorder>
                </observers>
            </catalog_product_save_after>
            <sales_order_save_after>
                <observers>
                    <backorder>
                        <class>DaniilKrok_Backorder_Model_Observer</class>
                        <method>salesOrderSaveAfter</method>
                    </backorder>
                </observers>
            </sales_order_save_after>
        </events>
    </global>

    <crontab>
        <jobs>
            <backorder_price_in_stock>
                <schedule><cron_expr>5 8 * * Sun</cron_expr></schedule>
                <run><model>DaniilKrok_Backorder_Model_Observer::backorderInStockByPriceWhereQteq</model></run>
            </backorder_price_in_stock>
            <backorder_out_of_stock>
                <schedule><cron_expr>5 8 * * Sun</cron_expr></schedule>
                <run><model>DaniilKrok_Backorder_Model_Observer::backorderOutOfStockByPriceWherelteq</model></run>
            </backorder_out_of_stock>
        </jobs>
    </crontab>

    <!--путь до контроллера в файле -->
    <admin>
        <routers>
            <backorder>
                <use>admin</use>
                <args>
                    <module>DaniilKrok_Backorder</module>
                    <frontName>backorder</frontName>
                </args>
            </backorder>
        </routers>
    </admin>
</config>

my observer.php

<?php

class DaniilKrok_Backorder_Model_Observer
{
    private $saveBackorder = false;

    public function salesOrderSaveAfter($event)
    {
        if (Mage::registry('backorder_daniilkrok_observer_fires_twice') == true) {

            $order = $event->getOrder();
            $orderIncrementId = $order->getIncrementId();

            $productsArray = array();
            foreach ($order->getAllVisibleItems() as $item) {
                $oldQty = (int)$item->getProduct()->getStockItem()->getQty();
                $qtyOrdered = (int)$item->getQtyOrdered();

                $differenceQty = $oldQty - $qtyOrdered;

                //increase the price by 25% AND check for fires twice
                if ($differenceQty < 0) {
                    $product_model = Mage::getModel('catalog/product')->load($item->getProductId());
                    $price = $product_model->getPrice();
                    $newPrice = $price + (($price * 25) / 100);
                    $product_model->setPrice($newPrice);
                    if ($product_model->getStockItem()->getBackorders() == "2") {
                        $product_model->setStockData(array(
                                'use_config_manage_stock' => 1,
                                'manage_stock' => 1,
                                'is_in_stock' => 1,
                            )
                        );
                    }

                    $product_model->save();
                    $this->saveBackorder = true;
                    $productsArray[] = array($item->getProductId());
                }
            }

            if ($this->saveBackorder == true) {
                $createdOrder = Mage::getModel('sales/order')->loadByIncrementId($orderIncrementId);
                $status = 'backorder_status';
                $createdOrder->setData('status', $status);
                $createdOrder->getResource()->saveAttribute($createdOrder, 'status');
            }
        }

        if (!Mage::registry('backorder_daniilkrok_observer_fires_twice')) Mage::register('backorder_daniilkrok_observer_fires_twice', true);

    }

    public function catalog_product_save_after($observer)
    {
        $product = $observer->getProduct();

        if (!is_null($product->getStockItem())) {
            $oldProductData = $product->getStockItem()->getOrigData();
            $oldQty = $oldProductData['qty'];
            $qty = $product->getStockItem()->getQty();

            //take away price from product 25%
            if ($oldQty <= 0 AND $qty > 0) {
                $product_model = Mage::getModel('catalog/product')->load($product->getId());
                $price = $product_model->getPrice();
                $newPrice = $price - (($price / 100) * 20);
                $product_model->setData('price', $newPrice);
                $product_model->getResource()->saveAttribute($product_model, 'price');
            }

            if ($oldQty > 0 AND $qty <= 0) {
                $product_model = Mage::getModel('catalog/product')->load($product->getId());
                $price = $product_model->getPrice();
                $newPrice = $price + (($price * 25) / 100);
                $product_model->setData('price', $newPrice);
                $product_model->getResource()->saveAttribute($product_model, 'price');
            }
        }
    }

    public function checkout_onepage_controller_success_action($observer)
    {
        $orders = $observer->getEvent()->getOrderIds(); // get event of orderIds
        if (!empty($orders)) {
            foreach ($orders as $order) {
                $order = Mage::getModel('sales/order')->load($order);
                foreach ($order->getAllItems() as $item) {
                    $product_model = Mage::getModel('catalog/product')->load($item->getProductId());
                    if ($product_model->getStockItem()->getQty() < 0) {
                        $resource = Mage::getSingleton('core/resource');
                        $writeConnection = $resource->getConnection('core_write');

                        $paramArray = ['qty' => 0];
                        $writeConnection->update('cataloginventory_stock_item', $paramArray, "product_id=" . $item->getProductId());
                    }

                    unset($product_model);
                    unset($resource);
                    unset($writeConnection);
                }
            }
        }
    }

    public function backorderInStockByPriceWhereQteq()
    {
        try {
            $productCollection = Mage::getModel('catalog/product')->getCollection()
                ->setOrder('entity_id', 'DESC')
                ->addAttributeToFilter('attribute_set_id', array('in' => array('4', '12')))
                ->addAttributeToFilter('price', array('gteq' => 80))
                ->joinField(
                    'is_in_stock',
                    'cataloginventory/stock_item',
                    'is_in_stock',
                    'product_id=entity_id',
                    '{{table}}.stock_id=1',
                    'left'
                )
                ->addAttributeToFilter('is_in_stock', array('eq' => 0));

            foreach ($productCollection->getData() as $data) {
                $product_model = Mage::getModel('catalog/product')->load($data['entity_id']);
                $product_model->setStockData(array(
                        'use_config_manage_stock' => 1,
                        'manage_stock' => 1,
                        'is_in_stock' => 1,
                    )
                );

                $product_model->save();
                unset($product_model);

                sleep(2);
            }
        } catch (Exception $e) {
            Mage::log('backorder function backorderInStockByPriceWhereQteq error message : ' . $e->getMessage());
        }
    }

    public function backorderOutOfStockByPriceWherelteq()
    {
        try {
            $productCollection = Mage::getModel('catalog/product')->getCollection()
                ->setOrder('entity_id', 'DESC')
                ->addAttributeToFilter('attribute_set_id', array('in' => array('4', '12')))
                ->addAttributeToFilter('price', array('lteq' => 80))
                ->joinField(
                    'qty',
                    'cataloginventory/stock_item',
                    'qty',
                    'product_id=entity_id',
                    '{{table}}.stock_id=1',
                    'left'
                )
                ->addAttributeToFilter('qty', array('eq' => 0))
                ->joinField(
                    'is_in_stock',
                    'cataloginventory/stock_item',
                    'is_in_stock',
                    'product_id=entity_id',
                    '{{table}}.stock_id=1',
                    'left'
                )
                ->addAttributeToFilter('is_in_stock', array('eq' => 1));

            foreach ($productCollection->getData() as $data) {
                $product_model = Mage::getModel('catalog/product')->load($data['entity_id']);
                $product_model->setStockData(array(
                        'use_config_manage_stock' => 1,
                        'manage_stock' => 1,
                        'is_in_stock' => 0,
                    )
                );

                $product_model->save();
                unset($product_model);

                sleep(2);
            }

        } catch (Exception $e) {
            Mage::log('backorder function backorderOutOfStockByPriceWherelteq error message : ' . $e->getMessage());
        }
    }
}

?>

1 Answer 1

0

replace

<run><model>DaniilKrok_Backorder_Model_Observer::backorderInStockByPriceWhereQteq</model></run>

with

<run><model>backorder/observer::backorderInStockByPriceWhereQteq</model></run>

and

<run><model>DaniilKrok_Backorder_Model_Observer::backorderOutOfStockByPriceWherelteq</model></run>

with

<run><model>backorder/observer::backorderOutOfStockByPriceWherelteq</model></run>

in your config.xml file

2
  • but i have this error 'Mage_Core_Exception' with message 'Invalid callback: Method for backorder/observer::backorderInStockByPriceWhereQteq does not exist' Commented Feb 18, 2020 at 16:01
  • i think backorder name magento used on custom module Commented Feb 18, 2020 at 16:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.