UPDATED:
Ok, I just found out my first if in my method isn't working. This one:
if ($codigo == null) {
# Stuff
} else {
#other stuff
}
But I also don't know whats wrong.
PREVIOUS:
I have an observer watching the event sales_quote_item_qty_set_after
. But when I try to call a method from my helper Data.php, it returns null.
This is my observer:
public function stockUpdate(Varien_Event_Observer $observer)
{
# Produto
$event = $observer->getEvent()->getItem()->getProduct();
if ($event->getAllnationsSync() == 1) {
$control = Mage::getStoreConfig('allnations/general/id_allnations', Mage::app()->getStore());
$id = $event->getData($control);
Mage::helper('novapc_allnations')->updateStock($id);
}
}
And this is my method:
public static function updateStock($codigo = null)
{
try {
# Força o load da página em modo ADMIN
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
# Pega usuario e senha colocados na config
$apiUser = Mage::getStoreConfig('allnations/general/api_user', Mage::app()->getStore());
$apiPassword = Mage::getStoreConfig('allnations/general/api_password', Mage::app()->getStore());
# Model da integração, para pegar a data da ultima atualização
$integration = Mage::getModel('novapc_allnations/integration');
$productIntegration = $integration->load(2);
$integration = $integration->load(3);
# Checa qual é maior, se é a data da integração dos produtos ou da atualização
if ($integration->getUpdatedAt() < $productIntegration->getUpdatedAt()) {
$updatedAt = substr($integration->getUpdatedAt(), 0, 9);
} else {
$updatedAt = substr($productIntegration->getUpdatedAt(), 0, 9);
}
# Verifica qual o atributo que representa o ID da All Nations
$control = Mage::getStoreConfig('allnations/general/id_allnations', Mage::app()->getStore());
# Instancia o model de estoque
$stockItem = Mage::getModel('cataloginventory/stock_item');
# Variavel de controle
$success = 0;
# Se for chamado sem o parametro $codigo, atualiza todos os produtos
if ($codigo == null) {
$ANProducts = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('allnations_sync', ['eq' => 1]);
foreach ($ANProducts as $single) {
$id = $single->getData($control); // Pega o ID da All Nations
$url = 'url' .
'?CodigoCliente=' . $apiUser .
'&Senha=' . $apiPassword .
'&CodigoProduto=' . $id .
'&Data=' . $updatedAt;
$return = Mage::helper('novapc_allnations')->callCurl('GET', $url);
$httpCode = $return['httpCode']['httpCode'];
$return = $return['response']['diffgrdiffgram']['NewDataSet']['Estoques'];
if ($httpCode == '200') {
if ($return['CODIGO'] == $single->getData($control)) {
if ($single->getData('allnations_promo') == 0) {
$single->setPrice
($return['PRECOREVENDA']);
} else {
$single->setSpecialPrice
($return['PRECOREVENDA']);
}
$single->save();
$stock = $stockItem->loadByProduct($single->getId());
$stock->setQty($return['ESTOQUEDISPONIVEL']);
$stock->setIsInStock($return['DISPONIVEL']);
$stock->save();
if ($success != 1) {
$success = 1;
};
}
} else {
return $httpCode;
}
}
if ($success == 1) {
$firstUpdate = $integration->getData('first_update');
# Checa se ja foi atualizado antes
if (!$firstUpdate) {
$integration->setFirstUpdate(Mage::getModel('core/date')->gmtDate('Y-m-d H:i:s'));
$integration->setUpdatedAt(Mage::getModel('core/date')->gmtDate('Y-m-d H:i:s'));
$integration->save();
} else {
$integration->setUpdatedAt(Mage::getModel('core/date')->gmtDate('Y-m-d H:i:s'));
$integration->save();
}
return $httpCode;
}
} else {
$url = 'url' .
'?CodigoCliente=' . $apiUser .
'&Senha=' . $apiPassword .
'&CodigoProduto=' . $codigo .
'&Data=' . $updatedAt;
$return = Mage::helper('novapc_allnations')->callCurl('GET', $url);
$httpCode = $return['httpCode']['httpCode'];
if ($httpCode == '200') {
$product = Mage::getModel('catalog/product')
->loadByAttribute($control, $codigo);
if ($return['CODIGO'] == $product->getData($control)) {
if ($product->getData('allnations_promo') == 0) {
$product->setPrice
($return['PRECOREVENDA']);
} else {
$product->setSpecialPrice
($return['PRECOREVENDA']);
}
$product->save();
$stock = $stockItem->loadByProduct($product->getId());
$stock->setQty($return['ESTOQUEDISPONIVEL']);
$stock->setIsInStock($return['DISPONIVEL']);
$stock->save();
return $httpCode;
}
}
}
} catch (Exception $e) {
return $e->getMessage();
}
}
My config.xml:
<?xml version="1.0"?>
<config>
<modules>
<Novapc_Allnations>
<version>0.1.0</version>
</Novapc_Allnations>
</modules>
<global>
<events>
<sales_order_save_after>
<observers>
<novapc_allnations>
<class>Novapc_Allnations_Model_Observer</class>
<method>confirmReserve</method>
<type>singleton</type>
</novapc_allnations>
</observers>
</sales_order_save_after>
<sales_quote_item_qty_set_after>
<observers>
<novapc_allnations>
<class>Novapc_Allnations_Model_Observer</class>
<method>stockUpdate</method>
<type>singleton</type>
</novapc_allnations>
</observers>
</sales_quote_item_qty_set_after>
<admin_system_config_section_save_after>
<observers>
<novapc_allnations>
<class>Novapc_Allnations_Model_Observer</class>
<method>setAttributes</method>
<type>singleton</type>
</novapc_allnations>
</observers>
</admin_system_config_section_save_after>
<adminhtml_block_html_before>
<observers>
<novapc_allnations>
<class>Novapc_Allnations_Model_Observer</class>
<method>updatePromotions</method>
<type>singleton</type>
</novapc_allnations>
</observers>
</adminhtml_block_html_before>
<adminhtml_block_html_before>
<observers>
<novapc_allnations_order>
<class>Novapc_Allnations_Model_Observer</class>
<method>updateOrderStatus</method>
<type>singleton</type>
</novapc_allnations_order>
</observers>
</adminhtml_block_html_before>
<sales_order_place_after>
<observers>
<novapc_allnations>
<class>Novapc_Allnations_Model_Observer</class>
<method>checkOrder</method>
<type>singleton</type>
</novapc_allnations>
</observers>
</sales_order_place_after>
</events>
<resources>
<novapc_allnations_setup>
<setup>
<module>Novapc_Allnations</module>
<class>Novapc_Allnations_Model_Resource_Setup</class>
</setup>
</novapc_allnations_setup>
</resources>
<blocks>
<novapc_allnations>
<class>Novapc_Allnations_Block</class>
</novapc_allnations>
</blocks>
<helpers>
<novapc_allnations>
<class>Novapc_Allnations_Helper</class>
</novapc_allnations>
</helpers>
<models>
<novapc_allnations>
<class>Novapc_Allnations_Model</class>
<resourceModel>novapc_allnations_resource</resourceModel>
</novapc_allnations>
<novapc_allnations_resource>
<class>Novapc_Allnations_Model_Resource</class>
<entities>
<order>
<table>npcallnations_orders</table>
</order>
<integration>
<table>npcallnations_integrations</table>
</integration>
<promotion>
<table>npcallnations_promotions</table>
</promotion>
<attr>
<table>npcallnations_attributes</table>
</attr>
</entities>
</novapc_allnations_resource>
</models>
</global>
<default>
<novapc_allnations>
<general>
<status>1</status>
</general>
</novapc_allnations>
</default>
<adminhtml>
<layout>
<updates>
<novapc_allnations>
<file>novapc_allnations.xml</file>
</novapc_allnations>
</updates>
</layout>
<translate>
<modules>
<Novapc_Allnations>
<files>
<default>Novapc_Allnations.csv</default>
</files>
</Novapc_Allnations>
</modules>
</translate>
<global_search>
<promotion>
<class>novapc_allnations/adminhtml_search_promotion</class>
<acl>novapc_allnations</acl>
</promotion>
</global_search>
<acl>
<resources>
<all>
<title>Allow Everything</title>
</all>
<admin>
<children>
<system>
<children>
<config>
<children>
<allnations>
<title>Allnations - All</title>
</allnations>
</children>
</config>
</children>
</system>
</children>
</admin>
</resources>
</acl>
</adminhtml>
<admin>
<routers>
<adminhtml>
<args>
<modules>
<Novapc_Allnations before="Mage_Adminhtml">Novapc_Allnations_Adminhtml</Novapc_Allnations>
</modules>
</args>
</adminhtml>
</routers>
</admin>
<crontab>
<jobs>
<allnations_promot>
<schedule>
<cron_expr>0 */4 * * *</cron_expr>
</schedule>
<run>
<model>novapc_allnations/observer::deletePromotions</model>
</run>
</allnations_promot>
<allnations_stockupdate>
<schedule>
<cron_expr></cron_expr>
</schedule>
<run>
<model>novapc_allnations/observer::updateStock</model>
</run>
</allnations_stockupdate>
</jobs>
</crontab>
</config>
$id = $event->getData($control);
did you check ID?var_dump($id);
in observer?$control
and$id
?