Magento get all items in cart
Magento get all the items currently in cart programatically using below code. You can place it anywhere you wish to get information, phtml or php file. Instead of Mage::getSingleton(‘checkout/session’)->getQuote() you can also use Mage::getSingleton(‘checkout/cart’)->getQuote() to get same results. If you want to see what all product information is retrieved you can use $product->getData() inside the foreach loop to display in array format.
$cart = Mage::getSingleton('checkout/session')->getQuote(); | |
//$cart->getAllItems() to get ALL items, parent as well as child, configurable as well as it's simple associated item | |
foreach ($cart->getAllVisibleItems() as $item) { | |
$product = $item->getProduct(); | |
$name = $product->getName(); | |
$sku = $product->getSku(); | |
} |
If you want all the items in collection format, you can call below code instead:
$itemsCollection = Mage::getSingleton('checkout/cart')->getQuote()->getItemsCollection(); |
Magento add static block to cms page
You can add static block to CMS page in Magento in following 2 ways:
1.) By adding code in Layout Update XML of CMS page:
<reference name="left"> | |
<block type="cms/block" name="block_name_anything"> | |
<action method="setBlockId"> | |
<block_id>STATIC_BLOCK_ID_HERE</block_id> | |
</action> | |
</block> | |
</reference> |
2.) By putting below code directly into CMS Page content area:
{{block type="cms/block" block_id="STATIC_BLOCK_ID_HERE"}} |
Make sure you flush Blocks HTML Output cache if your changes do not reflect on website.
Magento bug – Checkout cart 500 error – Redirect loops
Magento checkout cart gives 500 error and redirect loops when there is a shopping cart rule with Category condition.
I found a bug in Magento which redirects shopping cart indefinitely causing it 500 internal server error. This can be a serious bug as customer will not able to shop if this happens. I noticed this happens when there is a shopping cart rule which have Category in conditions of the rule.
If total quantity equals or greater than 1 for a subselection of items in cart matching ALL of these conditions:
Category is 125
So for example you have a shopping cart rule where you want to give some discount or free product if at least one product is chosen from specific Category, this triggers the error in frontend shopping cart. Main reason here is Category condition. If you remove category condition then the error should go away. But if you want to keep the category condition and still want Magento to handle the shopping cart promotion rule, check the code changes below:
To solve this I copied below file to my local
app/code/core/Mage/SalesRule/Model/Rule/Condition/Product/Combine.php
and edited the function validate:
/** | |
* Validate a condition with the checking of the child value | |
* @param Varien_Object $object | |
* | |
* @return bool | |
*/ | |
public function validate(Varien_Object $object) | |
{ | |
/** @var Mage_Catalog_Model_Product $product */ | |
$product = $object->getProduct(); | |
if (!($product instanceof Mage_Catalog_Model_Product)) { | |
$product = Mage::getModel('catalog/product')->load($object->getProductId()); | |
} | |
$valid = parent::validate($object); | |
/* Kalpesh commented whole block, as it causes redirect loop and Segmentation fault in apache | |
if (!$valid && $product->getTypeId() == Mage_Catalog_Model_Product_Type_Configurable::TYPE_CODE) { | |
$children = $object->getChildren(); | |
//$valid = $children && $this->validate($children[0]); //Kalpesh commented, issue.... | |
}*/ | |
return $valid; | |
} |
Hope this helps to some troubled souls!
Magento: Change canonical URL for particular category only
In Magento, if you want to change canonical URL for just one category or quite a few categories, and don’t want to affect rest of the canonical URLs then do the following.
Note that this is helpful when you already have canonical URL in the page and want to REPLACE it with new url. If you don’t have canonical URL at all then you might want to ignore the first action tag in the below code.
– Go to the category page you want to change canonical URL in Magento Admin
– Click the tab “Custom Design”
– In the Custom Layout Update textbox, paste this:
<reference name="head"> | |
<action method="removeItem" block="head"> | |
<item>link_rel</item> | |
<name>http://loca.lho.st/old-canonical-url</name> | |
</action> | |
<action method="addLinkRel" translate="title"> | |
<rel>canonical</rel> | |
<href>http://loca.lho.st/new-canonical-url</href> | |
</action> | |
</reference> |
Change http://loca.lho.st/old-canonical-url and http://loca.lho.st/new-canonical-url with your desired URLs and save the category.
Basically first action tag in the code removes old canonical URL and second action tag adds the canonical URL with the new value you specify.
You may also need to clear cache.
HTH!
Magento: Get real IP address behind a proxy
Get real IP address if your server or customer is behind a proxy network. Magento have function Mage::helper(‘core/http’)->getRemoteAddr(); to get client IP address, but it gives proxy IP address instead of real IP if there is proxy network in between. Below code checks and returns both real IP and proxy IP address if it founds any.
if (!empty($_SERVER['HTTP_CLIENT_IP'])) { | |
echo $_SERVER['HTTP_CLIENT_IP']; | |
} else if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { | |
$ips = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']); | |
echo trim($ips[count($ips) - 1]); //real IP address behind proxy IP | |
// echo $_SERVER['REMOTE_ADDR']; //proxy IP address | |
} else { | |
echo $_SERVER['REMOTE_ADDR']; //no proxy found | |
} |
Welcome to my Blog
Certifications
Honor
Recognition
Contributions
Categories
- Apache (2)
- ChatGPT (1)
- Domain name (2)
- eCommerce (2)
- htaccess (1)
- Humor (3)
- Instagram API (1)
- jQuery (4)
- JSON (1)
- Linux (10)
- Magento (142)
- Magento admin (58)
- Magento Certification (5)
- Magento error (13)
- Magento frontend (68)
- Magento Imagine (2)
- Magento Interview (5)
- Magento Master (2)
- Magento2 (10)
- Mobile (1)
- MySQL (7)
- OpenAI (1)
- OroCRM (2)
- Performance (2)
- PHP (8)
- Prototype JS (3)
- Security (4)
- Wordpress (3)
- XML (2)