Best way to link to categories from static blocks - magento

What is the best way to link to categories and products from static blocks.
I ask because sometimes we will update URL-keys for categories, etc, which will break links in static blocks to given categories and products.
Is there a better way to link using ids etc so the links will be active regardless of seo-friendly urls?
Currently, I use links such as:
<a href="store.com/generators/generators.html?"generator_package=6>link</a>
When the urls for the categories change the above link would be broken.

How about this? Let me know the result, maybe we can handle other ways.
+++ EDIT - New Code Sample +++
First create a static block which is this block should reference a phtml file that you can put your logic into this file.
{{block type="catalog/navigation" name="catalog.category" template="catalog/category/ListCategory.phtml"}}
Create ListCategory.phtml in the /app/design/frontend/[YOUR_THEME]/template/catalog/category/ directory.
<?php
// get current category id
$curCategory = $this->getCurrentCategory()->getId();
// instance of Mage_Catalog_Model_Category
$category = Mage::getModel(catalog/category)->load((int)$curCategory);
// child category id array
$childCategories = $category->getChildren();
?>

Related

Magento get stock quantity at related products, upsells, crosssells in frontend

I need to get the stock quantity for each product in the section where "Related products" is showed in frontend. Using Magento 1.9.
This function will not help me show the actual qty in my related products section:
Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty();
The function shows stock qty in the product view page and the catalog collection but does not work for related products.
What to do?
I am not sure if you are actually calling the above piece of code inside a PHTML file.
Assuming you are using the RWD theme and calling it inside a PHTML directly (which is not as per Magento Standards), the file location would be app/design/frontend/rwd/default/template/catalog/product/list/related.phtml
Inside, the FOREACH loop, call the below piece of code (screenshot attached for reference - frontend display):
<?php echo Mage::getModel('cataloginventory/stock_item')->loadByProduct($_item)->getQty(); ?>
The ideal solution would be overriding the CORE file:
app/code/core/Mage/CatalogInventory/Model/Stock/Item.php
like below:
app/code/local/Namespace/Modulename/Model/Stock/Item.php
in order to add a new function:
<?php
class Namespace_Modulename_Model_CatalogInventory_Stock_Item extends Mage_CatalogInventory_Model_Stock_Item
{
public function getItemStockQty($product)
{
return $this->loadByProduct($product)->getQty();
}
}
And inside the PHTML file, under the FOREACH LOOP call this function as below:
<?php echo $this->getItemStockQty($_item); ?>
Screenshot
Hope this helps.
Happy Coding...

Magento: Load products from specific category to product page

I have a custom made menu, that contains products from specific category (category id=3). In my menu there is a block:
{{block type="catalog/product_list" name="product_list" mode="grid" template="catalog/product/category_list_1.phtml"}}
I can normally see the products on main site and inside the category, but on product page the getBlockTemplateProcessor() doesn't return any data.
The code inside file is the same as for list.phtml, i just made some html modifications, located in:
\catalog\product\list.phtml
Why exactly is my code not rendering? Is it a problem that the files can't be located from product site?
Why all is good inside a category site and on product site not?
I suggest you use your own block extending the product list block, so that you can properly set the product collection, something like:
class YourNamespace_YourModule_Block_List extends Mage_Catalog_Block_Product_List
{
protected function _getProductCollection()
{
if (is_null($this->_productCollection)) {
$collection = Mage::getResourceModel('catalog/product_collection');
Mage::getModel('catalog/layer')->prepareProductCollection($collection);
$prodIdArray = array(1,2,3,4,5);
$collection->addAttributeToFilter('entity_id', $prodIdArray);
$collection->addStoreFilter();
$this->_productCollection = $collection;
}
return $this->_productCollection;
}
}
You can filter to add or remove any products that you want from the listing.

magento tracking pixel

There are two variables which are required in the tracking pixel which needs to be placed on category, product info, cart and confirmation page.
I've managed to get the Prod list and Prod working, however, the second two are causing me problems.
I can echo out the sku in the cart, however, the products are configurable products so it's duplicating the sku in the output. The code I'm using is below:
<?php
// $items = Mage::getModel('checkout/cart')->getQuote()->getAllItems();
$items = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();foreach($items as $item) { echo ''.$item->getSku().' ';}
?>
How do i change this to just display the one configurable SKU?
The second element is the Category name that the product exists in, Any one got any ideas on that? I've tried multiple variations but they've either broke the page or returned nothing.
Any help would be appreciated. If someone could also give me examples of how these would work on the confirmation page as well, that would be great.
Thanks for your help.
Check for the products visibility (simple products attached to a configurable would not be visible):
$items = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();
foreach($items as $item) {
if ($item->getProduct()->isVisibleInSiteVisibility()) {
echo ''.$item->getSku().' ';
}
}
With regards to the category name, a product can appear in multiple categories so im not sure how you want to handle that. Also, there is a concern that you are beginning to duplicate code across several template files. You will want to consider moving this all out to a block.
Anyway, to get the category names that the product belongs to here is at least one method of doing this...
$categoryCollection = $item->getProduct()->getCategoryCollection()
->addAttributeToSelect('name');
foreach($categoryCollection as $category) {
echo $category->getData('name') . "<br/>";
}

Magento: Random "Featured Product"

My client needs a a small box on CMS pages and on Category landing pages that will show thumbnail/price/short description of a random item related to that category (separate from the grid view)
Any thoughts on what would be the best way to accomplish this?
Thanks,
-Sam
go to template/catalog/product/view/ and make a new phtml file random_product.phtml with the following code
<?php
$catId = $this->getCat_id();
$cat=Mage::getModel("catalog/category")->load($catId);
$prodCollection = $cat->getProductCollection();
$pids=array();
foreach($prodCollection as $product)
{
array_push($pids,$product->getId());
}
$randProductId=array_rand($pids);
echo $randProductId;
?>
now if your category id is for example 10, make a static block and paste the following code in the contents
{{block type="catalog/product" cat_id="10" template="catalog/product/view/random_product.phtml"}}
now when you will view the static block, you will see a random product id every time you refresh.
THen,you can write your own custom html in the phtml file after loading the product.
To load your product from here you can do $product = Mage::getModel('catalog/product')->load($randProductId); then call methods such as $product->getName() etc to get the details you need to output.

Magento - Display Same Block Multiple Times on CMS Page

I'm using the method described here to display an individual product on a cms page:
http://www.molotovbliss.com/magento-commerce/magento-display-a-product-image-within-static-pages-and-blocks/
The problem is that if I try to display 2 products using this method both blocks show the same product even if I specify a different id in each block ie:
{{block type="catalog/product_new" product_id="1" template="catalog/product/view/your_new_page.phtml"}}
{{block type="catalog/product_new" product_id="2" template="catalog/product/view/your_new_page.phtml"}}
Adding the above code to my cms page results in product 1 being displayed twice.
Here is the solution change product_new to product_view
like this:
type="catalog/product_view"
have fun :)
Without knowing exactly what version of Magento you are running, I chose to look at 1.3.2.4 for you.
Oddly, I'm suprised you get any good result using 'catalog/product_new'. It has no provision for passing in the product id as a block arg. 'catalog/product_view' will allow you to pass in the product id as an arg, but only once. If you notice below, it's sets the product as a registry object the first time and will not use the passed in product id after that.
public function getProduct()
{
if (!Mage::registry('product') && $this->getProductId()) {
$product = Mage::getModel('catalog/product')->load($this->getProductId());
Mage::register('product', $product);
}
return Mage::registry('product');
}
My 2 cents (not beeing sure though...) : did you try to set a name to each block ?
Like :
{{block type="catalog/product_new" product_id="1" name="first.product" template="catalog/product/view/your_new_page.phtml"}}
{{block type="catalog/product_new" product_id="2" name="second.product" template="catalog/product/view/your_new_page.phtml"}}

Resources