Magento reindexing table - magento

I am updating the product information like quantity,shippingindays,outofstock values etc through mysql and the value gets updated but unless reindexing is done, it is not reflecting in the frontend.What to do to make the reflection in front end instantaneously or can i update the reindexing table directly, is it a good practise to do that? and also if the product goes out of stock how this reflection is instantaneous??

Try this code when you load the frontend to reindex everything. You can check the run method inside Mage file for getSingleton and all.
require_once 'app/Mage.php';
$app = Mage::app('default');
$indexingProcesses = Mage::getSingleton('index/indexer')->getProcessesCollection();
foreach ($indexingProcesses as $process) {
$process->reindexEverything();
}
Hope this may help.

Related

Magento : Taking 35 Second to load 4 product's

I have created a extension that filters product collection based on a attribute.
Below is the controllers, block and view template code.
Controller
$url = Mage::getUrl('no-route');
if(Mage::app()->getRequest()->getParam('ajax')){
echo $this->getLayout()->createBlock('catalogextensions/bestsellers_home_list')
->setTemplate('catalog/landings/bestseller.phtml')
->toHtml();
}
else{
$this->loadLayout();
$this->getLayout()->getBlock('head')->setTitle('Besesellers');
$this->renderLayout();
}
Block product collection function
$storeId = Mage::app()->getStore()->getId();
$products = Mage::getResourceModel('catalog/product_collection')
->addAttributeToSelect('*')
->addAttributeToFilter(array( array( 'attribute'=>'top_seller', 'eq' => '1' )));
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products);
Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($products);
$products->getSelect()->limit(4,$this->get_cur_page());
return $products ;
Product collection is iterated on view.phtml template.
Now, it is taking 35 seconds to get the output, below are the statistics of execution that i was able to get by making use of PHP's microtime() function
For
Block(product collection ) it taking around 0.01 second
Template Rendering its taking around 0.12 second
But for controller function it is taking around 35 second
I am not able to find what to check, because controller function just create a Block at runtime.
*Note:I am making use of a Paid full page cache Extension "Mirasvit FPC".
how can i find it what is taking time
Server configuration is
30GB Ram with 4 vCPU.
Application server : Nginx + php5-fpm.
Version: Magento CE 1.8.0.1
Thanks.
You use a bit deprecated way to call the filters. I don't know if that can really influate the time of loading but using addFieldToFilter() would limit the calls to database and can be usefull. You can use :
Magento: Filter products by Status
filtering product collection on "is_salable"
The block is maybe not cached correctly. I mean the products are load each time the page is called.
Make sure your flat product is set to on and all attributes needed for your filters are set as filter.
If a listing comes from EAv tables it might consume lots of resources thus slowing down.
Additionally apply optimization tricks.
First, are you sure you need to add all attributes to select?
You can replace the
addAttributeToSelect('*')
with
addAttributeToSelect(array('attribute_code', 'attribute_code'))
Next, what exactly is that
$this->get_cur_page()
doing? Rest of code shouldn't make disastrous performance issues.
PS
This should have been a comment, but I'm unable to comment yet.

I duplicated products magento and my urls are product-1.html product-2.html

I have made my magento store (community)
I have used duplicate product on a lot of products and I didn't get that the URL key field had to be changed for every product i thought i would solve it self.
Now i have about 100 products and unfortunately they are now named example:
/HaircolorBlack.html , /haircolorblack-1.html, /haircolorblack-2.html and so on.
I wonder if there are any way to easy make magento re create the url after what the products meta title is?
This would be so helpful.
I saw this two year old post about approx the same thing but I didn't wanted to use that script since I don't know if it might break anything in my magento since its been so much updates. Here is link: Clearing URL keys in Magento
Also I need total moron instructions on where to put the script to. Sorry ;)
Thanks a lot.
S
You can create a one off script using magento itself.. add this script somewhere in your magento root folder (beside index.php):
<?php
require_once "app/Mage.php";
Mage::app('admin'); // must be admin to do changes
function convertToUrlKey($string) {
return $string; // I WILL LEAVE YOU TO SOLVE THIS PART
}
foreach(Mage::getModel('catalog/product')->getCollection() as $product):
$metaTitle = $product->getMetaTitle();
$urlKey = convertToUrlKey($metaTitle);
try {
$product->setUrlKey($urlKey);
$product->save();
echo "Assigned url key {$urlKey} to {$product->getName()}.\n";
} catch(Exception $ex) {
echo "ERROR: {$ex->getMessage()}";
}
endforeach;
Obviously this is not a complete solution as you will have to re-create the convertToUrlKey function for the correct behavior of converting 'Black Hair Color' to 'black-hair-color' and also ensure that there will be no duplicate url keys generated..
You can run the script by browsing to it or running through SSH:
$ cd magentoProject
$ php -f changeUrlkeys.php
Good luck!

Magento needs a storeview set to update attribute on website scope

It seems like I have to set a store view before I can update an attribute on website scope – is that correct?
My code:
Mage::app('admin');
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$product = Mage::getModel('catalog/product');
$product->load(123);
$product->setStoreId('1'); // without this line the attribute is not updated
$product->setSomeattribute("abc");
$product->save();
Yes. that's correct. This is for performance reasons on the frontend. Usually you don't save products from frontend. See a detailed explanation of why is this needed.
But you don't need to do that. I's slow and resource consuming. Try to save it like this:
Mage::getSingleton('catalog/product_action')
->updateAttributes(array(123), array('somattribute'=>'abc'), 1);
The first parameter is and array with the product ids.
The second is an array with the attribute codes and values to be updated.
The third is the store id for which the update is done.
This method is faster.

How to save a collection for later use in Magento?

I want to write a cronjob in Magento that loads a product collection following certain parameters and saves it somewhere I can use in a cms/page.
My first approach was to use Magento's registry, but that doesn't work, ie a simple
Mage::register('label',$product_collection);
... doesn't work, as it seems "label" is not available in Mage::registry in my PHTML file...
Can someone point me in the right direction? Is this the correct approach? If so, how to make it work; if not, how to do it?
Thanks in advance!
Unfortunately, Mage::register will not get you where you want to go. The Mage registry keys are saved in the memory of the running PHP script, so it is scoped to the page request that is running the PHP code and therefor not shared between cron and your PHTML file.
In order to accomplish what you're looking for, you would need to cache the collection to persistent storage, such as hard-disk or Memcache. You may have to call the load() function specifically before caching, like so:
<?php
// ...
// ... Somewhere in your cron script
$product_collection = Mage::getModel('catalog/product')->getCollection()
->addFieldToFilter('some_field', 'some_value');
$product_collection->load(); // Magento kind of "lazy-loads" its data, so
// without this, you might not save yourself
// from executing MySQL code in the PHTML
// Serialize the data so it can be saved to cache as a string
$cacheData = serialize($product_collection);
$cacheKey = 'some sort of unique cache key';
// Save the serialized collection to the cache (defined in app/etc/local.xml)
Mage::app()->getCacheInstance()->save($cacheData, $cacheKey);
Then, in your PHTML file try:
<?php
// ...
$cacheKey = 'same unique cache key set in the cron script';
// Load the collection from cache
$product_collection = Mage::app()->getCacheInstance()->load($cacheKey);
// I'm not sure if Magento will auto-unserialize your object, so if
// the cache gives us a string, then we will do it ourselves
if ( is_string($product_collection) ) {
$product_collection = unserialize($product_collectoin);
}
// ...
See http://www.magentocommerce.com/boards/viewthread/240836

Set Special Price Programmatically In Magento

I am trying to write a script that will set a special price on a product with a start and an end date. When I run my script it does successfully set the special price, but the start and end date do not populate in the admin panel.
The code I am running is as follows:
$product = Mage::getModel('catalog/product')->load(114912);
$product->setSpecialPrice( ($product->getPrice() * .90) );
$product->setSpecialFromDate('2010-11-01');
$product->setSpecialFromDateIsFormated(true);
$product->setSpecialToDate('2010-11-30');
$product->setSpecialToDateIsFormated(true);
$product->save();
Does anyone know what I am doing wrong here?
I have just tried your code on my catalog and it worked with a little adjustement.
You should pay attention to the loaded store;
it is not allowed to update certain product fields if the ADMIN store is not the currently loaded (Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);).
<?php
require_once('app/Mage.php');
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
...
$product->save();
?>
This is just a guess but have you tried passing a timestamp like from time() and leaving out the setSpecialFromDateIsFormated(true)? That should cause the backend model to reformat it appropriately for you.

Resources