Browsing articles in "Magento frontend"
Nov 6, 2014
kalpesh

Magento: Get all products without categories (orphaned products)

Magento get all the configurable/simple products which are not associated with any categories, which are orphaned products. To get all the products (regardless of their type), simply ignore the condition where it checks for type_id in the below query and comment the condition line in foreach loop.

Below script will get all such products, you can create new PHP file at the root of Magento installation and paste the code:

<?php
require_once('app/Mage.php');
umask(0);
Mage::app('admin');
set_time_limit(0);

$i=0;
$sql = "select
    type_id,sku
 from catalog_product_entity a
 left join catalog_category_product cp on cp.`product_id` = a.entity_id
 left join catalog_product_relation cpr on cpr.child_id = a.entity_id
 where
       cp.product_id is null
   and cpr.parent_id is null
   and a.type_id = 'configurable'";
$connection = Mage::getSingleton('core/resource')->getConnection('core_read');
//echo count($connection->fetchAll($sql));exit;

foreach ($connection->fetchAll($sql) as $arr_row) {
        $pid = $arr_row['sku'];
        $product = Mage::getModel('catalog/product')->loadByAttribute('sku',$pid);
        if($product->getTypeId()!='configurable') continue;
        $i++;
        echo $i .") ". $product->getName() . " - " . $product->getSku() . "<br>";
}
?>
Sep 4, 2014
kalpesh

Magento add/remove product attribute programatically

Magento add product attribute using sql setup file in your module. Also assign your custom attribute to attribute set Default and group General programatically.

Below code will add your new attribute in Manage Products edit screen at the end of General tab with dropdown values Yes/No. It will not display in frontend website but you can change visibible on front to 1 if you wish. Note that it will assign to Default attribute set only but you can change it to whatever as per your requirement.

$model = Mage::getResourceModel('catalog/setup','catalog_setup');

$data=array(
'type'=>'int',
'input'=>'boolean', //for Yes/No dropdown
'sort_order'=>50,
'label'=>'CUSTOM ATTRIBUTE CODE LABEL',
'global'=>Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'required'=>'0',
'comparable'=>'0',
'searchable'=>'0',
'is_configurable'=>'1',
'user_defined'=>'1',
'visible_on_front' => 0, //want to show on frontend?
'visible_in_advanced_search' => 0,
'is_html_allowed_on_front' => 0,
'required'=> 0,
'unique'=>false,
'apply_to' => 'configurable', //simple,configurable,bundled,grouped,virtual,downloadable
'is_configurable' => false
);

$model->addAttribute('catalog_product','CUSTOM_ATTRIBUTE_CODE',$data);

$model->addAttributeToSet(
    'catalog_product', 'Default', 'General', 'CUSTOM_ATTRIBUTE_CODE'
); //Default = attribute set, General = attribute group

To remove the product attribute using the setup file, use below code instead:

$model = Mage::getResourceModel('catalog/setup','catalog_setup');
$model->removeAttribute('catalog_product','CUSTOM_ATTRIBUTE_CODE');
Sep 2, 2014
kalpesh

Magento set session cookie lifetime to 1 day

Is your Magento shopping cart kicking out all the items after 24 minutes? Are you frustrated because your customer’s cart items are getting flushed every few hours even though you have correct setting in Magento? Do you want to increase your shopping cart sessions to last for 1 day (or anything you wish)?

Here are the things to look for to increase/decrease Magento’s session cookie lifetime.

In your php.ini file:

session.gc_maxlifetime 86400

If you have not changed this, it should be by default 1440 i.e. 24 minutes. Change it to 86400 for one day session lifetime

In your Magento admin:

System -> Configuration -> Checkout -> Shopping Cart
Quote Lifetime (days) -> 1
System -> Configuration -> Web -> Session Cookie Management
Cookie Lifetime -> 86400

Make sure to check if you are in correct website/store if using multi-website/multi-store Magento setup. You can change the scope by changing the website/store from the upper left Store Switcher drop down in System -> Configuration screen.

If you are changing php.ini value, make sure you reload apache to reflect the changes.

Aug 22, 2014
kalpesh

Magento reload top mini cart programatically

Magento reload/refresh top mini cart programatically. This can be useful when you are using ajax to add/remove product to cart and want to reflect that item changes in the top cart immediately. Simply put below code where you are modifying cart programatically and it will start working.

Right now it’s only coded for simple and configurable products, but you can add bundle, group, virtual and downloader product item renderers too if you are using them in your website.

$b = $this->getLayout()
->createBlock('checkout/cart_sidebar')
->addItemRender('simple', 'checkout/cart_item_renderer', 'checkout/cart/sidebar/default.phtml')
->addItemRender('configurable', 'checkout/cart_item_renderer_configurable', 'checkout/cart/sidebar/default.phtml')
->setTemplate('checkout/cart/cartheader.phtml')
->toHtml();
Aug 22, 2014
kalpesh

Magento auto remove out of stock items from shopping cart

Magento automatically remove product items from shopping cart page which are out of stock. You may need this feature when the situation aries where product goes out of stock and that particular product is already there in other customer’s cart who have not yet checked it out. With this script Magento will auto-check if all the items in the cart are available and in-stock before proceeding for checkout page.

In config.xml file:

<events>
  <controller_action_predispatch_checkout_cart_index>
    <observers>
      <namespace_module_autoremove_outofstock>
        <type>singleton</type>
        <class>namespace_module/observer</class>
        <method>autoRemoveOutOfStockItems</method>
      </namespace_module_autoremove_outofstock>
    </observers>
  </controller_action_predispatch_checkout_cart_index>
</events>

In Observer.php file:

public function autoRemoveOutOfStockItems($observer) {
    $quote = Mage::getModel('checkout/session')->getQuote();
    $cartItems = $quote->getAllItems();
    foreach ($cartItems as $item)
    {
        //$productType = $item->getProduct()->getTypeId();
        //if($productType!='configurable') {
        $productId = $item->getProductId();
        $product = Mage::getModel('catalog/product')->load($productId);
        $stockItem = $product->getStockItem();
        if(!$stockItem->getIsInStock())
        {
                Mage::helper('checkout/cart')->getCart()->removeItem($item->getId())->save();
        }
        //}
    }

}
Pages:«1234567...14»

Welcome to my Blog

Kalpesh MehtaHelping Magento developers in their day-to-day development problems since 2011. Most of the problems and solutions here are my own experiences while working on different projects. Enjoy the blog and don't forget to throw comments and likes/+1's/tweets on posts you like. Thanks for visiting!

Certifications

Honor

Recognition

Magento top 50 contributors

Magento top 50 contributors

Contributions