Can you Clone a collection row in Magento? - magento

I'm trying to build a custom grid in Magento, where I have a row for every product ordered including qty. For example, if a customer ordered 3 of a specific product - I need to have 3 rows that all have matching details. Is this possible?
This is for a custom product where each row represents a manufacturing run. I'm not necessarily looking for code examples, but I'm not sure if I can do this with the sql style collection process? Or if I need modify the query and loop through once I've colelcted all the data from the database?
Thanks

Instead of showing the product order item grid, I just added an export to xml button. Then, in the grid just modified the "_exportIterateCollection" method as the following:
foreach ($collection as $item) {
for($i = 0; $i < $item->getQtyOrdered(); $i++ ){
call_user_func_array(array($this, $callback), array_merge(array($item), $args));
}
}
Hope this helps future questions!

Related

Magento - Get total number of items of a category in view.phtml

How can I get the total number of Items, I want to show it in the category view.phtml file. Usually this value is in the Toolbar.phtml.
I have tried something like this, but I think I am pretty far away:
$this->helper('catalog/output')->$_productCollection->count()
Magento version 1.7.0.2
The expected result should be something like this:
Items in this category: 17
The 17 should be the total number. If possible should include subcategory items.
Assuming that you want to display it in view.phtml you already have the current category object there so you can use $_category->getId()
$products_count = Mage::getModel('catalog/category')->load($_category->getId())
->getProductCount();
echo($products_count);
If you want to use it in list.phtml you can use
echo($_productCollection->count());
Try this,
<?php $_helper = $this->helper('catalog/output');?>
<?php $_category_detail = Mage::registry('current_category');?>
<?php $_category_detail->getName();?>
<?php $_category_detail->getId(); ?>
<?php $products_count = Mage::getModel('catalog/category')->load($_category_detail->getId())
->getProductCount();
echo($products_count);
?>
Basically, you can't show the total amount of filtered items in your view.phtml.
the reason is, that the logic, which gets the total amount, is not present in the $this context of the view.phtml.
But the logic is available in the Mage_Catalog_Block_Product_List_Toolbar block which is a child block of Mage_Catalog_Block_Product_List, though.
that basically means that you can actually get the total amount of filtered items by instantiating a toolbar and list block.
After doing that, the collection of the toolbar block has to be set with the value of the collection of the list block.
the following code is used in the view.phtml file to get the filtered total amount of items from the toolbar block:
$toolbar = new Mage_Catalog_Block_Product_List_Toolbar();
$list = new Mage_Catalog_Block_Product_List();
$toolbar-> setCollection($list -> getLoadedProductCollection());
$products_count = $toolbar -> getTotalNum();
There can be two ways we can find product count of a category.
$collection = Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('name')
->addAttributeToSelect('level')
->addAttributeToSelect('entity_id');
foreach($collection as $cat)
$cat->getProductCount();
This will give you the product count for deepest category only. So for example, you have following categories. considering tree like structure.
Clothes(6)
->Cotton(3)
->Women(2)
The result returned from the piece of code given above.
Clothes(3)
Cotton(1)
Women(2)
There are three products directly associated with Clothes only, 1 with Cotton only and 2 with Women only. So it simply ignores the subcategories.
Another way is getting product count from products perspective.
$current_category = Mage::getModel('catalog/category')->load($cat->getEntityId());
$productCount = Mage::getModel('catalog/product')->getCollection()
->addFieldToFilter('manufacturer',$this->manufacturer["id"])
->addFieldToFilter('visibility',4)
->addFieldToFilter('status',1)
->addCategoryFilter($current_category)->getSize();
This gives us the added benefit of filtering product attributes. However in the above scenario, the count returned will be slightly different.
It will return Clothes (6), as it has 3 products associated to itself and 3 more products to its sub categories. Similarly
Cotton(3)
Women(2).
So for efficient results it would be nice to use mix of both.
It's not right to load additional model inside view because you already have model instance from which you can retrieve Product collection.
$this->getCurrentCategory()->getProductCollection()->count();
it is very simple and it worked well for me its just one line of code
<?php echo Mage::registry('current_category')->getProductCount();?>
It will display the count of the products of current category

How to show sort by most popular(most sold) products on product listing page in Magento?

I am using following tutorial to display the most sold product sort by option(Filtering) to display on product listing page in magento?
Tutorial
/app/code/local/Mage/Catalog/Model/Resource/Product/collection.php
<?php
public function sortByReview($dir){
$table = $this->getTable('review/review');
$entity_code_id = Mage::getModel('review/review')->getEntityIdByCode(Mage_Rating_Model_Rating::ENTITY_PRODUCT_CODE);
$cond = $this->getConnection()->quoteInto('t2.entity_pk_value = e.entity_id and ','').$this->getConnection()->quoteInto('t2.entity_id = ? ',$entity_code_id);
$this->getSelect()->joinLeft(array('t2'=>$table), $cond,array('review' => new Zend_Db_Expr('count(review_id)')))
->group('e.entity_id')->order("review $dir");
}
?>
But I want to sort products which are most sold from every category.
How can I do this?
Is there any free extension available for this?
I did the following thing as the client did not want automatically filter most sold product.
I created an attribute "popular" as drop down and give values from 1 to 5.Then marked "Used for Sorting in Product Listing" to Yes.
After this the attribute was visible under sorting options.

Magento Collection Issue. Determining Order In Iteration

I have a category titled 'top 100 products' and , for now, the collection works fine. But what I want to do is dynamically deteremine what # each product is, in relation to the 100 products being listed. In other words, if product one is listed, I can grab the value so I can add "#1" next to the product title.
When product #74 is listed, it will dynamically say "#74". I can calculate the total number of listings using the getSize function but I don't know how to work with that varible to spit out the proper 'sequence' number of each listed product. The reason I use getSize is so the count can be preserved through paging.
To understand the code, please reference the /catalog/product/list.phtml template. In list or grid mode, you will see the following:
Edit. I can't post this code here. It's maddening. Just need to know how to get a while loop to surround the 'foreach' product contorller loop so I can use while (x) < variable_with_total_products_counted and let x++ reside within the product controller for loop. Every time I try to do this, I get syntax errors. As if it won't let the product controller loop (the for each loop) reside inside my while loop which iterates from 0 --> 99 (top 100 products).
How about something like this:
$count = $collection->getSize();
$counter = 1;
foreach($collection as $item) {
// Process item
echo '<p>' . $item->getName . ' (' . $counter . ')</p>';
$counter++;
}
To ensure that number is persistent, you can assign it to the products and it will stay assigned for the duration of the script / page:
$count = $collection->getSize();
$counter = 1;
foreach($collection as $item) {
$item->setData('tmp_number', $counter);
$counter++;
}
// Work with products

How can I add "Quantity" to a VirtueMart "Browse" page?

My VirtueMart shop.browse page has the typical columns of SKU, Name, Price, and "Update" (which contains the Add to Cart button). I would like to add a Quantity column between Price and Update so that a buyer can choose a quantity prior to pressing the "Add to Cart" button.
Though I don't know HOW to do this, I think I know WHERE to do it:
/public_html/components/com_virtuemart/themes/default/templates/browse/includes/browse_listtable.tpl.php
Lines 67-72 of that file tell the program how to build the table, but what I DON'T know how to do is modify the code source to tell it to include quantity as an element for the table. Here's the code:
// Loop through each row and build the table
foreach($data as $key => $value) {
$table->addRow( $data[$key], 'class="sectiontableentry'.$i.'"', 'td', true );
$i = $i == 1 ? 2 : 1;
}
Which include file is actually being called in this foreach loop, and what code would I add to reference quantity data?
You should not modify your core code. If you just need to put quantity box inside your browse template use variable $form_addtocart
It will give you quantity box and add to cart button.
All variables for browse page and for flypage template you can find here:
http://virtuemart.net/documentation/Developer_Manual/Modifying_the_Layout.html
If this is not what you are trying to get, please be more specific, or show your web page.

Magento - Find Out of Stock Products With Inventory

In my Magento store I sometimes forget to select 'In Stock' from the dropdown after adding new inventory to an out of stock item.
Is it possible to somehow get a list of all products which have inventory but as tagged as "Out of Stock"?
If you are able to script something real quick.
$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToFilter('is_in_stock', 0)
->addAttributeToFilter('qty', array("gt" => 0));
Sadly I can't seem to remember how to put in the >0 in a way that is supposed to work. Maybe someone can comment on that.
What you can do with $products is run it through a foreach loop and then set the is_in_stock to the value of 1 and you should be in business.
Easiest way (I think)
Admin -> System -> Import/Export -> Profiles
Add new Profile
Change to export, give the file a name and a location. Download the file and open in your favorite spreadsheet program. Look for "is_in_stock" - 1 = in stock, 0 = out of stock. Filter by 0 and you'll have a list of all of your OOS items.
You can also check the RSS list for low stock alerts at http://shop.com/index.php/rss/catalog/notifystock/
You have to put
Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty()
code in
app/design/frontend/default/[yourtemplate]/template/catalog/product/list.phtml
file to check your product inventory.
The shortest way is:
Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($_collection);
Load Products order by Stock And Descending Order
$products
->joinField(
'inventory_in_stock',
'cataloginventory_stock_item',
'is_in_stock',
'product_id=entity_id',
'is_in_stock>=0',
'left'
)
->setOrder('inventory_in_stock', 'desc');

Resources