Sunday, 15 March 2015

magento Delete all Products Programmatically

Hello Friends

In this article, we’ll be using a simple method using our Mage class, and removing products via the Magento Mage calls. Using this will result in removal of every product in your Magento catalog, so be careful ! Be sure to modify store id because we may have multiple stores in magento at a time.

This method works 10 times faster than basic products delete from admin.
1) Create new directory “custom” in root magento directory and create new file : deleteProducts.php
2) Paste the following code into deleteProducts.php
3) Run file using url : http://yourmagentostore.com/custom/deleteProducts.php


set_time_limit(0);
ini_set('memory_limit','512M');
   
require_once '../app/Mage.php';
Mage :: app("default")->setCurrentStore( Mage_Core_Model_App :: ADMIN_STORE_ID );
       
    $products = Mage :: getResourceModel('catalog/product_collection')->setStoreId(1)->getAllIds();
    if(is_array($products))
    {
        foreach ($products as $key => $pId)
        {
            try
            {
                $product = Mage::getModel('catalog/product')->load($pId)->delete();
                echo "successfully deleted product with ID: ". $pId ."
";
            }
            catch (Exception $e)
            {
                echo "Could not delete product with ID: ". $pId ."
";
            }
        }
    }


Let me know if you have any query

Enjoy

magento enable WYSIWYG editor in Magento Admin

Hello Friends

Most of the times developers have requirements by their clients to use WYSIWYG editor in magento admin in custom module.  WYSIWYG editor gives end user easy interface to draft their content with nice formatting.

Here we will see that how to enable WYSIWYG editor in Magento admin for our custom module


1)  Loading the TinyMCE library in Head section :

Add the following code in to your module Edit.php block file located  here ( Namespace / Modulename /Block / Adminhtml / Modulename / Edit.php )

protected function _prepareLayout() {
    parent::_prepareLayout();
    if (Mage::getSingleton('cms/wysiwyg_config')->isEnabled()) {
        $this->getLayout()->getBlock('head')->setCanLoadTinyMce(true);
    }
}


2) Enable WYSIWYG in Form field:

 Add the following code in your Adminhtml Form.php class, located here (Namespace / Modulename / Block / Adminhtml / Edit / Tab / Form.php):

$fieldset->addField('content', 'editor', array(
    'name'      => 'content',
    'label'     => Mage::helper('modulename')->__('Content'),
    'title'     => Mage::helper('modulename')->__('Content'),
    'wysiwyg'   => true,
    'required'  => false,
));


3) Its Done:

check screenshot






Let me know if you have any query

Enjoy.



magento stop caching for a block

Hello Friends

Have you ever want to disable cache for a block? When do we need it? - Yes sometime, you may have some custom code (spaghetti style) in a HTML block and you will need to disable caching in this case, otherwise your footer will be cached on first visitor.

Here is an example which show you how to disable caching for footer in a Magento site
In \magento\app\code\core\Mage\Page\Block\Html\Footer.php file
remove the cache informations of the construct function :

protected function _construct()
{
$this->addData(array(
‘cache_lifetime’=> false,
‘cache_tags’ => array(Mage_Core_Model_Store::CACHE_TAG, Mage_Cms_Model_Block::CACHE_TAG)
));
}

become :

protected function _construct()
{

}

Let me know if you have any query

Enjoy

magento check product image added or place holder image is used

Hello Friends

Well, in Magento place holder image are always used when you get product URL or using Catalog_Helper_Image. But you know sometime, you don't like this behavior and you need to be able to check if there is an image associated with the product or if the placeholder image will be used.

OK here is the solution
if ($_product->getSmallImage()=='no_selection' ) { //small_image is not set
// do your custom code
} else {
//display normal image?
}

This solution is used in many problem, for example when you need to handle images between product type Simple Product, Grouped Product and Configuration Product.


Let me know if you have any query

Enjoy

magento image not display wysiwyg editior

Hello Friends

I worked on a magento project on which cms images were not appearing in editor and also on front end.

CMS editor was getting image urls like http://your-websitelink.com/media//imagename.ext because of double slashes (//) images weren’t appear on front end as well as in cms editor.

for that I searched on internet but didn’t get any useful info that can solve this issue so I came up with a solution to use php functions to handle this.


Open file /httpdocs/app/code/core/Mage/Adminhtml/controllers/Cms/WysiwygController.php


Change $url variable from


$url = Mage::getModel('core/email_template_filter')->filter($directive); 

to

$url = substr_replace(Mage::getModel('core/email_template_filter')->filter($directive),"",strrpos(Mage::getModel('core/email_template_filter')->filter($directive),"/"),1);

 Save it.

For front end I made static blocks for different images for different pages in left column those had same issue I fixed it same as above but used static block code as following. 


$block = substr_replace($this->getLayout()->createBlock('cms/block')->setBlockId('block-name')->toHtml(),"",strrpos($this->getLayout()->createBlock('cms/block')->setBlockId('block-name')->toHtml(),"//"),1);
 

echo $block;

 By above statements image url will not have double slashes.

Let me know if you have any query


magento get add to cart url

Hello Friends

If you want to get add to cart url then use below code

<input type="button" title="<?php echo $this->__('Add to Cart') ?>" class="addtocart" onclick="setLocation('<?php echo Mage::getUrl('checkout/cart/add', array('product'=>$product_id,'qty'=>1)) ?>')" />;

Where  $product_id is product id


Magento contact form email issue

Hello Friends

I was working on a magento site and setup contact form but that wasn’t sending email, just posted on contact us page and not showing any message whether email sent or not. I check form action in source code and it was empty. To resolve this issue follow this step

Opened app/design/frontend/YOUR_PACKAGE/YOUR_THEME/template/contacts/form.phtml file and change form action as following

<form action="<?php echo $this->getFormAction(); ?>" id="contactForm" method="post">

to this

<form action="<?php echo $this->getUrl('contacts/index/post'); ?>" id="contactForm" method="post">

And save this file and don't forgot to flush cache.

Let me know if you have any query
 
Enjoy