How to sort products - magento

How to sort by position products.
I am making a slider and a want products to be sorted on slider. I want to use build in sorting like best match, which is on category -> product tab
Here is how I am doing but won't work:
$product_collection = Mage::getModel('catalog/category')->getProductCollection()->setOrder('position','ASC');
$selected = 0;
foreach($product_collection as $product) {
$full_product = Mage::getModel('catalog/product')->load($product->getId());
if($full_product->getTypeId() === 'configurable'){ ?>
<li>
<img>
</li>
<? }
} ?>

You need to load a category in the category model before pulling out its product collection
$product_collection = Mage::getModel('catalog/category')->getProductCollection()->setOrder('position','ASC');
should be replaced by
$product_collection = Mage::getModel('catalog/category')->load($somecategoryid)->getProductCollection()->setOrder('position','ASC');

Related

Show the stock for configurable Products MAGENTO

Im trying to show on the frontend the stock when we have less than 5 products.
For simple products is easy but when we are talking about configurable products it is not working.
This is the code I tried but is not working, the var $product is coming empty so I am always getting a 0 in $total_qty
Here is the code that I am using:
$product=Mage::getModel("catalog/product");
$prod=$product->load($prod_id);
$conf = Mage::getModel('catalog/product_type_configurable')->setProduct($prod);
$col = $conf->getUsedProductCollection()->addAttributeToSelect('*')->addFilterByRequiredOptions();
$total_qty=0;
foreach($col as $sprod)
{
$sprod=$product->load($sprod->getId());
$qty = intval(Mage::getModel('cataloginventory/stock_item')->loadByProduct($sprod)->getQty());
$total_qty+=$qty;
}
?>
---
<p class="availability in-stock">
<?php //echo $this->__('Availability:') ?> <span>
<?php
if (($__manStock >= 1) && ($__manStock < 5))
{
echo $this->__("ยก JUST $total_qty LEFT !");
}
?>
</span></p>
Any help will be appreciated, thanks.
Configurable Products don't have stock. The simple products do.
You will have to query the associated products.
You can get the associated products using getAllowProducts()

pass newly added attributes from product detail page to cart page (magento)

I am using Magento Version 1.7.0
I have added shirt size(newly added attributes) drop down box in product detail page.using below code
app\design\frontend\default{mytempalte}\template\catalog\product\view.phtml
<?php $product = $_product->getAttributeText('size_chart'); ?> shirt size
<select name="size_chart">
<option value="Select">Select</option>
<?php for($i = 0;$i < count($product);$i++) { ?>
<option value="<?php echo $product[$i]; ?>"><?php echo $product[$i]; ?></option>
<?php } ?>
</select>
customer can able to select the shirt size .After select the shirt size i need to show shirt size in cart and checkout page.
how to pass the shirt size value from product detail to other page?.
Thanks
Actually, in cart page you may get cart items in quote object.
$cart = Mage::getModel('checkout/cart')->getQuote();
$items = $cart->getAllVisibleItems();
foreach ($items as $item){
$options = $item->getProductOptions();
$superAttributes = $options['info_buyRequest']['super_attribute'];
if (!!$superAttributes){
$attributeObjSize = Mage::getModel('eav/config')->getAttribute(Mage_Catalog_Model_Product::ENTITY,'size_chart');
$attributeObjSizeId = $attributeObjSize->getAttributeId();
foreach ($superAttributes as $code=>$superAttribute){
if ($attributeObjSizeId == $code){
$sizeCode = $superAttribute;
$sizeLabel = $attributeObjSize->getSource()->getOptionText($sizeCode);
}
}
}
}
The $sizeLabel should be want you would like to have. Hope it works.

Joomla Virtuemar 2.x

im traying to show in the products detail page, the list of the other products by the same Manufacturer. I mean, if you are in a Shoes from Adidas, the page shows up the another adidas products. So i have this code:
http://codepad.org/pO8XF0OI (Dont Know how to write code here)
Is actually working but only shows the products by the Category no by the Manufacturer.
What i'm doing wrong?
Try this,
If you are using Vm2.x It will gives you full products under one manufacture.You can add or remove another tables based on your requirement.
$db = &JFactory::getDBO();
$sql = "SELECT N.product_name,M.virtuemart_product_id,MN.mf_name
FROM jos_virtuemart_product_manufacturers AS M
LEFT JOIN jos_virtuemart_products AS P ON M.virtuemart_product_id = P.virtuemart_product_id
LEFT JOIN jos_virtuemart_products_en_gb AS N ON N.virtuemart_product_id = M.virtuemart_product_id
LEFT JOIN jos_virtuemart_manufacturers_en_gb AS MN ON MN.virtuemart_manufacturer_id = M.virtuemart_manufacturer_id
WHERE M.virtuemart_manufacturer_id = '$manufature_id'";
$db->setQuery($sql);
$db->query();
$res = $db->loadAssocList();
echo "<pre/>";
print_r($res);
Hope this may help...
Is done, here is the code:
<?php
$db = &JFactory::getDBO();
$manufacturer = $this->product->virtuemart_manufacturer_id;
$sql = "SELECT N.product_name,I.virtuemart_media_id,M.virtuemart_product_id,MN.mf_name
FROM h3ls8_virtuemart_product_manufacturers AS M
LEFT JOIN h3ls8_virtuemart_products AS P ON M.virtuemart_product_id = P.virtuemart_product_id
LEFT JOIN h3ls8_virtuemart_products_es_es AS N ON N.virtuemart_product_id = M.virtuemart_product_id
LEFT JOIN h3ls8_virtuemart_product_medias AS I ON M.virtuemart_product_id = I.virtuemart_product_id
LEFT JOIN h3ls8_virtuemart_manufacturers_es_es AS MN ON MN.virtuemart_manufacturer_id = M.virtuemart_manufacturer_id
WHERE M.virtuemart_manufacturer_id = '$manufacturer'"; // h3ls8_ is your table Prefix
$db->setQuery($sql);
$db->query();
/* $res = $db->loadAssocList(); //This Load the info as a List */
$prod = $db->loadObjectList(); // This Load the info as a group of objects for loading it.
JRequest::setVar('virtuemart_manufacturer_id',$this->product->virtuemart_manufacturer_id,'GET');
$productModel = VmModel::getModel('product');
if ($this->product->virtuemart_manufacturer_id !=0 ) {
$products = $prod;
$productModel->addImages($products);
$this->assignRef('products', $products);
}
// Show Products
if (!empty($products)) { ?>
<div class="seven columns obras">
<h5> Obras </h5>
<div class="row">
<?php
// Start the Output
foreach ($products as $product) {
// Show Products
?>
<div class="four columns mobile-two end producto">
<div class="imagen">
<!-- <?php //if ($product->product_special == 1) { ?>
<div class="oferta">
<br />
</div>
<?php}?> *** If you wanna add some especial icon to a Special product uncommnent this *** -->
<?php
echo JHTML::_('link', JRoute::_('index.php?option=com_virtuemart&view=productdetails&virtuemart_product_id='.$product->virtuemart_product_id.'&virtuemart_category_id='.$product->virtuemart_category_id),$product->images[0]->displayMediaThumb('class="catImage"',false));
?>
</div>
</div> <!-- end of product -->
<?php } ?>
</div>
</div>
<?php } ?>

Magento current store category listing with single depth or parent only?

I want to retrieve the parent category only in current store of magento. I googled and get the result for all parent category with subcategories included. but want only parent top categories single depth only.
if(strlen(trim($primary_category_temp)) < 1)
{
$_categories = Mage::helper('catalog/category')->getStoreCategories();
if (count($_categories) > 0):
foreach($_categories as $_category):
$primary_category[] = $_category->getId();
endforeach;
endif;
}
$category = Mage::getModel('catalog/category')->getCollection()->addFieldToFilter('is_active',array('eq' => 1))->load();
from this i get whole category collection
foreach($category as $cat)
{
if($cat->getData('level')==2)
{
echo 'my code';
}
}
May be you need use:
Mage::app()->getStore($store)->getRootCategoryId()
or
Mage::app()->getStore()->getRootCategoryId()
for default store
Try below code
$categories=Mage::getModel('catalog/category')
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('level',2)
->addIsActiveFilter();
This will only retrieve parent top categories

how to display categories by level?

i want to display sub-categories (if any) of a current category on listing page, but if there is no sub-category and also the current category is not the main category, then display the same level categories.
<?php
//print_r($_category); exit;
$Curr_cat_id = $_category->getEntityId();
$children = Mage::getModel('catalog/category')->getCategories($Curr_cat_id);
if($children){
foreach($children as $sub_cat)
{
$subCat = Mage::getModel('catalog/category')->load($sub_cat->getId());
?>
<a href="<?php echo $this->getUrl().$sub_cat->getRequestPath(); ?>" >
<?php echo $sub_cat->getName(); ?>
</a><br />
<?php
} // end of foreach
} // end of if
//else{
// this is where i want the same level categories if in case there are no sub-categories and also the current category is not a main category.
//}
?>
You need to get the parent category id of your current category first. And then load the products ( like you are doing in your script ) with the parent category id instead.
To achieve that the following post should be of help
http://www.magentocommerce.com/boards/viewthread/16357/

Resources