Magento: Get all products with 0 quantity and In Stock
Magento get all products which have zero quantity and are still In Stock in inventory. Below script will show you all such simple products.
<?php | |
require_once('app/Mage.php'); | |
umask(0); | |
Mage::app('admin'); | |
set_time_limit(0); | |
$productCollection = Mage::getModel('catalog/product') | |
->getCollection() | |
->addAttributeToSelect('*') | |
->joinField('qty', | |
'cataloginventory/stock_item', | |
'qty', | |
'product_id=entity_id', | |
'{{table}}.is_in_stock=1', | |
'left') | |
->addAttributeToFilter('qty', array("eq" => 0)); | |
echo "<h2>Simple Products with 0 quantity and In Stock</h2>"; | |
foreach($productCollection as $product) { //print_r($product->getData());exit; | |
if($product->getTypeId() == 'simple') | |
echo $product->getName() . " | " . $product->getSku() . "<br>"; | |
} | |
echo 'Done'; | |
?> |
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 fix for error “Code already exists.”
Fix for Magento error Code already exists. in Sales > Tax > Manage Tax Rules, when saving the rule.
When trying to save Tax rules in Magento, you may get “Code already exists.” error if there are lots of tax rates passing in POST. Magento error is not making sense here, as the issue is something different. Basically when you post the data in PHP, it limits maximum post variables which should pass to the server. For me, that limit was 1000 in max_input_vars, which by changing to 20000 solved this issue and Magento successfully accepted my changes without any error.
To change max_input_vars to higher value, you need to edit in PHP.ini file for max_input_vars like this:
max_input_vars 20000 |
But as editing PHP.ini requires restarting apache to reflect the changes, I did the change in .htaccess file just for Tax rule modification and later reverted it.
You can edit .htaccess to make this change by copy-pasting below line at the end of the file:
php_value max_input_vars 20000 |
Now you should be able to change Tax rules even if you have lots of tax rates posting through for that change! I prefered to revert .htaccess back to what it was as I didn’t need to change tax rules frequently.
Note: If the above does not solve the issue for you, make sure your Tax Rule name is not duplicating with another tax rule name.
Magento: Show gift card details in admin order page
Magento display gift card details (code and amount) in Order View page of admin panel. When customer uses gift card in their order Magento does not display gift card code and amount details in admin panel which makes it tough to know. Customer can pay partially through gift card which makes even tougher as Magento only shows payment information of other method and not gift card amount deductions.
Below code will show you an additional block in magento admin panel with Gift Card details. If gift card is not used in an order, the block will simply no shown.
Open the file app/design/adminhtml/default/default/template/sales/order/view/edit.phtml
Add the below lines of code anywhere you want the gift card block to appear. I have used it after Account Information block.
<?php $cards = unserialize($_order->getGiftCards()); | |
if($cards!='' && count($cards)>0) { ?> | |
<div class="box-left"> | |
<div class="entry-edit"> | |
<div class="entry-edit-head"> | |
<h4 class="icon-head head-account"><?php echo Mage::helper('sales')->__('Gift Cards') ?></h4> | |
</div> | |
<div class="fieldset"> | |
<div class="hor-scroll"> | |
<table cellspacing="0" class="form-list"> | |
<?php foreach ($cards as $card) { ?> | |
<tr> | |
<td class="label"><label><?php echo Mage::helper('sales')->__('Giftcard Code') ?></label></td> | |
<td class="value"> | |
<div style="font-weight:bold"><?php echo $card['c'];?></div> | |
</td> | |
</tr> | |
<tr> | |
<td class="label"><label><?php echo Mage::helper('sales')->__('Giftcard Amount') ?></label></td> | |
<td class="value"> | |
<div style="font-weight:bold"><?php echo $card['a'];?></div> | |
</td> | |
</tr> | |
<?php }?> | |
</table> | |
</div> | |
</div> | |
</div> | |
</div> | |
<?php }?> |
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'); |
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)