0

I can get information regarding orders using 3rd party API but when I want to save it into 3 tables, for sales_flat_order and sales_flat_order_adress the data has been saved and for sales_flat_order_grid I've gotten this error I think that I want to save the Parent_id in the entity_id as it should be same. But I don't know how.

<br />
<b>Fatal error</b>:  Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`magento`.`sales_flat_order_grid`, CONSTRAINT `FK_SALES_FLAT_ORDER_GRID_ENTITY_ID_SALES_FLAT_ORDER_ENTITY_ID` FOREIGN KEY (`entity_id`) REFERENCES `sales_flat_order` (`entity_id`) ON DELETE CA)' in D:\wamp\www\magento1.9\lib\Zend\Db\Statement\Pdo.php:228

this array that I want to save for 3 tables

Foreach($array as $value){
                    $array = array('status' => $value['status'],
                 'base_grand_total' => $value['price'],
                 'grand_total' => $value['price'],
                 'increment_id' => $value['number'],
                 'base_currency_code' => 'MXN',
                 'customer_email' => $value['email'],
                 'customer_firstname' => $value['name'],
                 'customer_lastname' => $value['apellido'],
                 'global_currency_code' => 'MXN',
                 'order_currency_code' => 'MXN',
                 'store_name' => 'linio marketplace',
                 'created_at' => $value['created'],
                 'updated_at' => $value['created'],
                 'total_item_count' => $value['item']);
                  $modele = Mage::getModel('thorleif/order')->setData($array);
                  $modele->save();
                 $id = $modele->getId();
                 $array = array(
                 'parent_id' => $id,
                 'region' => $value['region'],
                 'postcode' => $value['zip'],
                 'street' => $value['address'].'.'.$value['country'],
                 'city' => $value['city'],
                 'email' => $value['email'],
                 'telephone' => $value['tel'],
                 'firstname' => $value['name'],
                 'lastname' => $value['apellido'],
                 'address_type' => 'shipping');
                  $modele = Mage::getModel('thorleif/address')->setData($array);
                  $modele->save();
                  $array = array(
                 'parent_id' => $id,
                 'status' => $value['status'],
                 'base_grand_total' => $value['price'],
                 'grand_total' => $value['price'],
                 'increment_id' => $value['number'],
                 'base_currency_code' => 'MXN',
                 'shipping_name' => $value['name'],
                 'global_currency_code' => 'MXN',
                 'order_currency_code' => 'MXN',
                 'store_name' => 'linio marketplace',
                 'created_at' => $value['created'],
                 'updated_at' => $value['created']);
                  $modele = Mage::getModel('thorleif/grid')->setData($array);
                  $modele->save();
                 }
javaTodev
  • 97
  • 1
  • 10

2 Answers2

0

Please try this to check if you get well your paiement method code:

$paymentMethods = Mage::getModel('payment/config')->getAllMethods();

foreach($paymentMethods as $paymentMethod) {
    echo $paymentMethod->getCode();
}

Edit:

Check also this one and look if you get the payment method code :

$order->getPayment->getMethodInstance()->getData()

If you get your payment method :

$order->getPayment()->getMethod();

You can follow this inchoo tuto to create an order programmatically

PЯINCƎ
  • 11,669
  • 3
  • 25
  • 80
0

It's work to me using the insertion like this for sales_flat_order_grid

$entity_id =1; 
$entity_type_id =4; 
$attribute_id = 92;
$value = 'red'; 
$store_id  =1;
$resource     = Mage::getSingleton('core/resource');
$writeAdapter   = $resource->getConnection('core_write');
$table        = $resource->getTableName('some_table_name');
$query        = "INSERT INTO {$table} (`entity_id`,`entity_type_id`,`attribute_id`,`value`,`store_id`) VALUES ($entity_id, $entity_type_id, $attribute_id, $value, $store_id);";
$writeAdapter->query($query);
javaTodev
  • 97
  • 1
  • 10