Sometimes, when refreshing the Magento cart page for example, the cart items will disappear. The problem here might be a missing image. Check if there are any missing images on the cart page and if so, upload them to your…
Creating a credit memo / refund programmatically (Magento 1)
The following code creates a Magento credit memo / refund.
1 2 3 4 5 6 7 8 9 10 11 |
$invoices = array(); foreach ($this->getInvoiceCollection() as $invoice) { $invoices[] = $invoice; } $service = Mage::getModel('sales/service_order', $this); foreach ($invoices as $invoice) { $creditmemo = $service->prepareInvoiceCreditmemo($invoice); $creditmemo->register(); $creditmemo->save(); } |
Getting the current date and time (Magento 1)
You can get the current date and time using the following code:
1 |
$dateAndTime = date("Y-m-d H:i:s", Mage::getModel('core/date')->timestamp(time())); |
Solving syntax error, unexpected ‘endif’ (T_ENDIF) problem (Magento 1)
Sometimes, when opening a Magento product page, the following error is shown: Parse error: syntax error, unexpected ‘endif’ (T_ENDIF) in … The problem here is not with the Magento code itself. Its a problem with the Apache server configuration. To…
Solving “Cannot send headers; headers already sent” problem (Magento 1)
Sometimes, when looking through your Magento log files, you might stumble upon the “Cannot send headers; headers already sent in …” message. One possible reason for that could be a space or a newline behind a closing PHP tag (?>).…
Adding a static block to a CMS page (Magento 1)
Use the below code to add a Magento block to your CMS page.
1 |
{{block type="cms/block" block_id="block_id"}} |
Unlock customer programmatically (Magento 1)
Use the below code to unlock a customer programmatically.
1 |
$customer->setFailedLogins(0)->setLastFailedLogin(0)->save(); |
Creating order including custom options programmatically (Magento 1)
Using the following code, you can create an order including custom options programmatically.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
$quote = Mage::getModel('sales/quote')->setStoreId(Mage::app()->getStore('default')->getId()); $quote->setCustomerEmail($mail); $product = Mage::getModel('catalog/product')->load($productId); $buyInfo = array( 'qty' => 1, 'options' => array($optionId => $optionValue) ); $quote->addProduct($product, new Varien_Object($buyInfo)); $addressData = array( 'firstname' => $firstName, 'lastname' => $lastName, 'street' => $street, 'city' => $city, 'postcode' => $postCode, 'telephone' => '-', 'country_id' => $country, 'region_id' => $regionId, ); $billingAddress = $quote->getBillingAddress()->addData($addressData); $shippingAddress = $quote->getShippingAddress()->addData($addressData); $shippingAddress->setCollectShippingRates(true)->collectShippingRates() ->setShippingMethod('flatrate_flatrate') ->setPaymentMethod('bankpayment'); $quote->getPayment()->importData(array('method' => 'bankpayment')); $quote->collectTotals()->save(); $service = Mage::getModel('sales/service_quote', $quote); $service->submitAll(); $order = $service->getOrder(); // ... |
Getting a list of all countries (Magento 1)
Use the code below to get a list of all countries. It can be used to create a country dropdown list for example.
1 |
$countryList = Mage::getModel('directory/country')->getResourceCollection()->loadByStore()->toOptionArray(true); |
Loading a model without using IDs (Magento 1)
Sometimes there is no id available for loading a model. If thats the case, the following code might help out. It loads a collection by an attribute like name or sku.
1 |
$collectionData = Mage::getModel('modelname')->getCollection()->addFieldToFilter('attribute', 'attribute value'); |
Afterwards, it is possible to iterate over the…