When the event sales_order_save_after is triggered the new quantity value is not inserted:
Autosynch\Sale\etc\event.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="sales_order_save_after">
<observer name="autosynch_sales_order_save_after" instance="Autosynch\Sale\Observer\OrderPlaceAfter" />
</event>
</config>
Autosynch\Sale\etc\module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Autosynch_Sale" setup_version="0.0.1">
</module>
</config>
Autosynch\Saleegistration.php
<?php /**
* Copyright © 2016 Magento. All rights reserved. * See COPYING.txt for license details.
*/
use Magento\Framework\Component\ComponentRegistrar;
ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Autosynch_sale', __DIR__);
Autosynch\Sale\Observer\OrderPlaceAfter.php
<?php
namespace Autosynch\Sale\Observer;
use Magento\Framework\Event\ObserverInterface;
class Observer implements ObserverInterface {
protected $connector; public function __construct() {
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
}
public function execute(\Magento\Framework\Event\Observer $observer) {
$order = $observer->getEvent()->getOrder();
$customerId = $order->getCustomerId();
$order_status = $order->getStatus();
if($customerId){ // Order Criada
define("getHost", "100.100.100.100");
define("getUser", "username");
define("getPassword", "password");
define("getDB", "dbname");
$mysqli = new mysqli(getHost, getUser, getPassword, getDB);
$mysqli->select_db("dbname");
if($order_status == 'processing'){
mysqli_query($mysqli, "INSERT INTO `test` (`id`, `test`) VALUES (NULL, '1')") or die(mysqli_error($mysqli));
} else{
mysqli_query($mysqli, "INSERT INTO `test` (`id`, `test`) VALUES (NULL, '2')") or die(mysqli_error($mysqli));
}
} else{
mysqli_query($mysqli, "INSERT INTO `test` (`id`, `test`) VALUES (NULL, '3')") or die(mysqli_error($mysqli));
}
}
}
Is this code throwing any error? If so an you provide it? Also, since you are using magento, if always better to use its own ORM rather than raw queries. In your module you can create your table inside magento app database and models to access these tables. This is the best approach!