How can i join 2 product collections (Magento) - magento

I have configurable products that are placed in categories.
example:
configurable product: shirt in category with id 6.
this product has multiple simple products: green shirt with attribute 'size' = XL, L and M.
together this is one product in the shop.
no i want a collection so i can select all simple products with 'size' = XL and where the configurable product of the simple product is in category with id 6.
i think i need to join 2 collections (simple + configurable).
But i can't find info about it on the web.
somebody a solution?

You dont need to join 2 collections for this, you can simpy get the childproducts based on the parent.
//loop trough configurable products
$simpleProducts = Mage::getModel('catalog/product_type_configurable')->getUsedProducts(null, $product);
edit or simplified : $_product->getTypeInstance()->getUsedProducts(); this would be the best solution

Related

Magento order products in cart by category

I would like to change the default order of the products in the cart. By default it sorts by the sequence the products are added to the cart.
But it would be way easier for me if I can sort them by category.
So let's say default way is this:
Product a (Snack)
Product b (Vegetables)
Product c (Snack)
Product d (Fruit)
I want it to be:
Product a (Snack)
Product c (Snack)
Product b (Vegetables)
Product d (Fruit)
Can someone guide me here in how to accomplish this?

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 How to get The Parent Category of A Sub-Category from A Product

Root Category (id: 1)
- Apparel (id: 2)
-- Shirts (id:4)
-- Pants (id:5)
- Accessories (id: 3)
-- Handbags (id:6)
-- Jewelry (id:7)
On Magento we can get the category Ids of a product by using $productObj->getCategoryIds()
$productObj = Mage::getModel('catalog/product')->load($product_id);
$categoryIds = $productObj->getCategoryIds();
Which will return an array of category Ids of the product. I have a specific need to get the first level parent of category of a product. Takes for example the category tree above, if a product is categorized in Pants category, I want to get the first level category which is Apparel (in this case, the product only tagged under Pants category but not tagged in Apparel category).
Question: what method can I use to get the parent category of a sub category, or is it possible to get the first level category from a product?
You can use the catalog/category model and its method getParentCategory():
foreach ($categoryIds as $iCategoryId) {
$m = Mage::getModel('catalog/category')
->load($iCategoryId)
->getParentCategory();
var_dump($m->debug());
}

How to get Product id using Super attribute in Magento?

I am working on ajax module for Shopping cart in Magento. Consider i have a configurable product with 2 simple products configured as its two sizes (Small an Medium). When user selects and adds the item to cart, i cannot see the specific product id (small) in the url.
But instead supper_attribute is posted to my controller.
Is it possible for me to get the actual product id of size "Small" with the super attribute.
Below is my supper attribute array
[super_attribute] => Array
(
[129] => 128
)
129 = attribute_id (Size)
128 = attribute value (Small)
Please suggest me in this scenario. Please let me know if my question is not clear.
Thanks
Try this:
$childProduct = Mage::getModel('catalog/product_type_configurable')->getProductByAttributes($request->getData('super_attribute'), $product);
Where $product is the configurable product object.
For the Class Reference

How to get a list of categories by product number?

I need to sort categoies by product number that means which category has large number of products is shown at the top of category list. So catgories list should be descending order by product number.
any help would be appreciated!
I have categories and its product as following as:
category no of products
cell phones 5
cameras 8
computers 3
I would like to sort categories cameras,cell phones,computers by product number .For this sorting result i would like to use join in collection class of category.
The following code will fetch the product per category:-
$productCollection = Mage::getResourceModel('catalog/product_collection')
->addCategoryFilter($category);
$productCount = $productCollection->count();
You can do in this way:-
Get an array of all categories
Loop through them
Fetch product collection with product count for each category (from the above code).
Sort categories with the product count.
Hope this helps. Thanks.
If you have a collection of categories called $collection then you can do this:
$collection->setOrder('children_count', 'desc');
Edit:
To make it the default in an overridden collection use this method:
protected function _initSelect()
{
parent::_initSelect();
$this->setOrder('children_count', 'desc');
return $this;
}

Resources