Magento - Loading root collection loads all products - magento

Regarding using multiple stores, with different root categories:
I have 2 stores set up, with different roots. One has 14 products, the other 6.
If I use the following on my homepage (simply to show how many products are in the root category for that store - in this case with an ID of 8) I get 20 products - so all products in the store, from all roots:
$_testproductCollection = Mage::getModel('catalog/category')->load(8)
->getProductCollection()
->addAttributeToSelect('*')->load();
echo "NO. OF PRODUCTS IS ".$_testproductCollection->count();
However, if I change the ID to a sub-category, I get the correct amount of products. There are only 6 products in this root:
But the count shows 20 (as there's 20 in the whole store - or both roots).
Anyone know what's up? Is it a bug?
I also noticed that if you go to Manage Products and use the store view filter, it doesn't do anything, still showing 20 products in the store view whose root has only 6 products:

OK, I think this works, haven't tested too much but seems to have done the trick. You need to first get your stores root category id, then join some fields so you have access to the products "category_id", then filter using that:
$_rootcatID = Mage::app()->getStore()->getRootCategoryId();
$_testproductCollection = Mage::getResourceModel('catalog/product_collection')
->joinField('category_id','catalog/category_product','category_id','product_id=entity_id',null,'left')
->addAttributeToFilter('category_id', array('in' => $_rootcatID))
->addAttributeToSelect('*');
$_testproductCollection->load();
foreach($_testproductCollection as $_testproduct){
echo $this->htmlEscape($_testproduct->getName())."<br/>";
};

Related

setPostedProducts with flat catalog category / product

I'm using setPostedProducts to maintain a sale/discount category.
On my dev machine, it's working (flat catalog category and product is off).
On my stage machine (flat catalog category and product is on), it's not working anymore.
How can I fix this?
category = Mage::getModel('catalog/category')->load($categoryId); // category id of my sales category
$category->setPostedProducts(array(123 => 1)); // product 123 should be the only one in my sales category for testing purposes
$category->save();
Don't really know why, but what solved my problem was adding
Mage::app('admin');
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
before executing setPostedProduct().
I suppose that in admin the _getResource() function translates into Mage_Catalog_Model_Resource_Category instead of Mage_Catalog_Model_Resource_Category_Flat

Using Magento API to add all products to root category

I want to set all the products to the root category (next to the current category they are in).
I know the best way to do this is using the Magento API, but can somebody get me started on this?
Just to be clear, i don't want to change the category id, i just want to add another category id to the product.
EDIT
It takes a lot time to check all 3000+ products to see in what other category it is. Example: Root_catId = 175 Product_1 cat = 3 (needs to be: 3, 175) Product_2 cat = 9 (needs to be: 9, 175)
You can do this without using the API but if you want to use the API try following.
You can use following first you need to create a user with permissions to access soap requests in Magento admin
<?php
$proxy = new SoapClient('http://yourmagento/api/v2_soap/?wsdl');
$sessionId = $proxy->login('apiUser', 'apiKey');
$result = $proxy->catalogCategoryAssignProduct($sessionId, '3', '10'); // 3 is category id and 10 is product id
You can loop through every product to assign the product to a specific category.
Replace the root category id with the category id which you require.

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

Magento Bundle Product - Multiple Options with Same products. How to stop Magento from hiding the repetitive products?

I am trying to setup a bundle product within Magento. This product should allow the customer to select 4 free products to include with the bundle. These products can be all different or 4 of the same product.
For example
Free Product 1
Free Product 2
Free Product 3
A customer could select four of Free Product 1, or one of Free Product 1 & 2, with two of Free Product 3.
I am using 4 drop-down input types which each have all three Free products as options. So a customer can choose any of the three products for each Free Gift line item.
Magento is only displaying one of the drop-down select lists, I believe due to the fact that each drop-down contains the same product list.
Where would I need to look to stop Magento from checking if the product options are already listed in a previous selection?
Unless you're doing this programmatically (that is writing the code), there's no way to do this.
When Magento adds a product, it first looks into the quote / shopping cart to see if one already exists. If one does, it pulls that one and adds to the quantity. There is no way to turn this off.
Programmatically, you very manually add an item to a shopping cart. This is how...
$cart = Mage::getSingleton("checkout/cart");
foreach ($products_to_add as $product_id => $custom_options) {
$product = Mage::getModel("catalog/product")->load($product_id);
$options = new Varien_Object(array("options" => $custom_options,
"qty" => 1));
// some products may result in multiple products getting added to cart
// I beleive this pulls them all and sets the custom options accordingly
$add_all = $product->getTypeInstance(true)
->prepareForCartAdvanced($options, $product, Mage_Catalog_Model_Product_Type_Abstract::PROCESS_MODE_FULL);
foreach ($add_all as $add_me) {
$item = Mage::getModel('sales/quote_item');
$item->setStoreId(Mage::app()->getStore()->getId());
$item->setOptions($add_me->getCustomOptions())
->setProduct($add_me);
$item->setQty(1);
$cart->getQuote()->addItem($item);
}
}
// when done adding all the items, finally call save on the cart
$cart->save();

Magento - Using getPriceHtml on custom product collection doesn't return correct tax price

I have a product collection called using the following (set to show 6 items):
$_testproductCollection = Mage::getResourceModel('catalog/product_collection')
->addAttributeToSelect('*')
->setPageSize(6);
$_testproductCollection->load();
then i get the 6 products details with a foreach:
foreach($_testproductCollection as $_testproduct){
echo "Price is ".$this->htmlEscape($this->getPriceHtml($_product, true))."<br/>";
};
this works ok until I set my store to show prices inclusive of tax. Instead of showing 2 different prices, for example:
Excl. Tax: $138.56
Incl. Tax: $149.99
it shows the same price for both. If I add a call to the loaded product collection again immediately after it's loaded:
$_productCollection=$this->getLoadedProductCollection();
...it works fine, the prices are correct, but then it's skipping the setPageSize function and returning the full store collection of products.
How can I get the correct tax prices to show, and what is it in the getLoadedProductCollection that corrects this? The function is in Mage/Catalog/Block/Product/List.php
See Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection::addTaxPercents
Also, the getLoadedProductCollection calls (via a catalog layer) the addMinimalPrice and addFinalPrice methods. From those docs you can see there are methods for adding tiered pricing and URL rewrites. That is nice to know.

Resources