Kailash Yadav

Just another script samurai !!

Archive for the 'PHP' Category

Refresh Magento Cache Programmatically

When writing processes to automate some of Magento’s normally laborious tasks, perhaps for a product-import script, a mass attribute update script, category import, or inventory adjustment, keep in mind that you may have to refresh some kind of cache — especially when working with attributes. If you are encountering quirks or inconsistencies with your data, try these:

  • Rebuild Catalog Index Mage::getSingleton('catalog/index')->rebuild();
  • Rebuild Flat Catalog Product Mage::getResourceModel('catalog/product_flat_indexer')->rebuild();
  • Inventory Stock Mage::getSingleton('cataloginventory/stock_status')->rebuild()
posted by Kailash Yadav in e-commerce,Magento,PHP and have No Comments

Regular expression for HTML tags in string

Here is one of most powerful and used regular expression which was used by every developer once in their life for sure.

How to select content or string inside a HTML tag using regular expression in all languages.

I am sharing this regular expression with you. This regular expression is useful when you fetching content from Other Website using cURL.
]*>(.*?)} ?>

Here is one small example:
//Example to get text inside given tag
$content = file_get_contents("http://www.example.com");
$tag= 'h1'; //we will extract page heading

preg_match("{<'.$tag.'[^>]*>(.*?)}", $content, $match);
$data = $match[0];
echo $data;
?>

Note: This only returns text(string or HTML) inside the Tag you have given. It doesn’t include that tag.

posted by Kailash Yadav in Javascript,PHP and have No Comments

PHP Fatal error: Call to a member function toHtml() on a non-object in …/Layout.php on line 529

If you’ve just upgraded your Magento Commerce site to the 1.4.x release from a 1.3.x release or you using old theme on magneto 1.4.x + , and you forgot to change the theme/template to the default one before you performed the upgrade, you’ll probably end up with a blank white screen.

If we look at line 529 of the “Layout.php” file we can see that it’s itterating over several objects ($this->_output) and producing a coalesced output which can then be displayed on the screen.

/**
* Get all blocks marked for output
*
* @return string
*/
public function getOutput()
{
$out = '';
if (!empty($this->_output)) {
foreach ($this->_output as $callback) {
$out .= $this->getBlock($callback[0])->$callback[1]();
}
}

return $out;
}

In 1.4.x Magento overhauled the templates and themes code so your 1.3.x template/theme isn’t going to work in a 1.4.x environment without some re-coding.  To get over this particular problem, find and edit the ‘page.xml’ file in your template/theme directory.  It should be in ./app/design/frontend/default/{your_theme}/layout/page.xml

Change the line
<block type=”core/profiler” output=”toHtml”/>

to
<block type=”core/profiler” output=”toHtml” name=”core_profiler”/>

Clear the cache directory using “rm -rf magento/var/cache/*” or using admin panel configuration > cache management and refresh your page.  All being well your site should spring back in to life.

If this didn’t work for you, or your template/theme directory doesn’t have a local copy of the ‘page.xml’ file, look at the default files and ensure that they were updated when you upgraded Magento.  You’ll find the files in:

./app/design/frontend/default/default/layout/page.xml
./app/design/frontend/default/modern/layout/page.xml
./app/design/frontend/base/default/layout/page.xml

Hope this helps.

posted by Kailash Yadav in e-commerce,Magento,Open Source,PHP and have No Comments