0

I have a problem with the order creation on Magento via direct PHP

I use Magento version 1.9.4.3

This is the code I made:

<?php

require_once '../../app/Mage.php';
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

$resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');
$writeConnection = $resource->getConnection('core_write');

$_customer = Mage::getModel('customer/customer')->load(1);

$firstName = $_customer->getFirstname(); //get customers first name
$lastName = $_customer->getLastname(); //get customers last name
$customerAddressId = $_customer->getDefaultBilling(); //get default billing address from session

$address = Mage::getModel('customer/address')->load($customerAddressId);
$street = $address->getStreet();
$city = $address->getCity();
$postcode = $address->getPostcode();
$phoneNumber = $address->getTelephone();
$countryId = $address->getCountryId();
$regionId = $address->getRegionId();

$quote = Mage::getModel('sales/quote')->setStoreId(1);
$quote->assignCustomer($_customer);

$addressData = array(
    'firstname' => $firstName,
    'lastname' => $lastName,
    'street' => $street,
    'city' => $city,
    'postcode' => $postcode,
    'telephone' => $phoneNumber,
    'country_id' => $countryId,
    'region_id' => $regionId
);


$product = Mage::getModel('catalog/product')->load(5525);
$quoteItem = $quote->addProduct($product, 1);

$quote->save();


$quote->getBillingAddress()->addData($addressData);
$shippingAddress = $quote->getShippingAddress()->addData($addressData);

$shippingAddress->setCollectShippingRates(true)->collectShippingRates()->setShippingMethod('freeshipping_freeshipping')->setPaymentMethod('banktransfer');

$quote->getPayment()->importData(array('method' => 'banktransfer'));

$quote->collectTotals()->save();

On Magento, I have already created a customer with ID 1 who has both the billing address and the shipping address

There is also a product with id 5525

Free shipping and bank transfer payment methods are enabled

The store is associated with the Italian state with a percentage tax of 22%

When I execute this code I noticed that in the table "sales_flat_quote_item" some wrong data are saved

Practically the fields "tax_amount", "base_tax_amount", "price_incl_tax", "base_price_incl_tax", "row_total_incl_tax", "base_row_total_incl_tax" are saved with the value "0"

Instead the "price","row_total","base_row_total" field are correct and the "tax_percent" field is correctly "22"

These are the screens of the table "sales_flat_quote_item":

enter image description here

enter image description here

If, on the other hand, I create an order from the Magento frontend, in the "sales_flat_quote_item" table, the data is saved correctly and so the order is correct

I have this problem since I updated the version of Magento from 1.9.3.7 to 1.9.4.3

Can you figure out what the problem maybe?

  • Replace Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); With simply Mage::app(); and try again please. You declared an admin store and trying to save in quote table which is a frontend table using frontend models. – pontikos Nov 9 at 9:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.