When you get a model like in this line:
$blogpost = Mage::getModel('weblog/blogpost');
You can not just assume that this works. You have to first of all check the post-condition of that call:
$blogpost = Mage::getModel('weblog/blogpost');
if (!$blogpost) {
throw new UnexpectedValueException('Expected Model not available.');
}
This will prevent that you run into the error you have as well. It should also give you a hint what has not been properly configured: The model you tried to get.
The solution then is to learn with a step debugger where the configuration for that model is normally taken from.
For example you could have a configuration cache enabled and the system is still running on older configuration where the model didn't exist. Or your configuration is up to date but the model was just not properly configured in it.
Next to verifying post-conditions you also need to verify pre-conditions in your case. I mixed the place where the error occured a bit, but thanks to your comment, a pre-condition test similar to the post-condition check I already outlined can be introduced in the load method, see the following example:
class Magentotutorial_Weblog_Model_Blogpost extends Mage_Core_Model_Abstract
{
protected function _construct()
{
$this->_init('weblog/blogpost');
}
/**
* Load object data
*
* @param integer $id
* @return Mage_Core_Model_Abstract
*/
public function load($id, $field=null)
{
$resource = $this->_getResource();
if (!$resource) {
throw new UnexpectedValueException('Resource instance is not available');
}
return parent::load($id, $field);
}
}
This in your concrete case does effectively prevent the error and will show you which model is missing. The rest I already answered exactly applies here. As this is resource related you could already assume that this is about the configuration of your resource model. But the better lesson to learn is to step through with a step debugger as the parsing of config files then will reveal which information is missing and more importantly why.
$productids=array(1,2,4,3);
$websiteId = Mage::app()->getWebsite()->getId();
$store = Mage::app()->getStore();
// Start New Sales Order Quote
$quote = Mage::getModel('sales/quote')->setStoreId($store->getId());
// Set Sales Order Quote Currency
$quote->setCurrency($order->AdjustmentAmount->currencyID);
$customer = Mage::getModel('customer/customer')
->setWebsiteId($websiteId)
->loadByEmail($email);
if($customer->getId()==""){
$customer = Mage::getModel('customer/customer');
$customer->setWebsiteId($websiteId)
->setStore($store)
->setFirstname('Jhon')
->setLastname('Deo')
->setEmail($email)
->setPassword("password");
$customer->save();
}
// Assign Customer To Sales Order Quote
$quote->assignCustomer($customer);
// Configure Notification
$quote->setSendCconfirmation(1);
foreach($productsids as $id){
$product=Mage::getModel('catalog/product')->load($id);
$quote->addProduct($product,new Varien_Object(array('qty' => 1)));
}
// Set Sales Order Billing Address
$billingAddress = $quote->getBillingAddress()->addData(array(
'customer_address_id' => '',
'prefix' => '',
'firstname' => 'john',
'middlename' => '',
'lastname' =>'Deo',
'suffix' => '',
'company' =>'',
'street' => array(
'0' => 'Noida',
'1' => 'Sector 64'
),
'city' => 'Noida',
'country_id' => 'IN',
'region' => 'UP',
'postcode' => '201301',
'telephone' => '78676789',
'fax' => 'gghlhu',
'vat_id' => '',
'save_in_address_book' => 1
));
// Set Sales Order Shipping Address
$shippingAddress = $quote->getShippingAddress()->addData(array(
'customer_address_id' => '',
'prefix' => '',
'firstname' => 'john',
'middlename' => '',
'lastname' =>'Deo',
'suffix' => '',
'company' =>'',
'street' => array(
'0' => 'Noida',
'1' => 'Sector 64'
),
'city' => 'Noida',
'country_id' => 'IN',
'region' => 'UP',
'postcode' => '201301',
'telephone' => '78676789',
'fax' => 'gghlhu',
'vat_id' => '',
'save_in_address_book' => 1
));
if($shipprice==0){
$shipmethod='freeshipping_freeshipping';
}
// Collect Rates and Set Shipping & Payment Method
$shippingAddress->setCollectShippingRates(true)
->collectShippingRates()
->setShippingMethod('flatrate_flatrate')
->setPaymentMethod('checkmo');
// Set Sales Order Payment
$quote->getPayment()->importData(array('method' => 'checkmo'));
// Collect Totals & Save Quote
$quote->collectTotals()->save();
// Create Order From Quote
$service = Mage::getModel('sales/service_quote', $quote);
$service->submitAll();
$increment_id = $service->getOrder()->getRealOrderId();
// Resource Clean-Up
$quote = $customer = $service = null;
// Finished
return $increment_id;
Best Answer
Instead of direct call of sales/quote_item model,use checkout/cart singleton model (
Mage::getSingleton('checkout/cart'))
and it will resolve your issue.try below code: