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>"; | |
} | |
?> |
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'); |
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.
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(); |
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(); | |
} | |
//} | |
} | |
} |
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)