Is it possible to do a a LIKE operation on a multiselect attribute on a magento product collection?
For example below I am doing a find in set to locate an exact match:
$_productCollection = Mage::getResourceModel('catalog/product_collection')
->addAttributeToFilter('makemodel',
array('finset' => Mage::getResourceModel('catalog/product')
->getAttribute('makemodel')
->getSource()
->getOptionId('#Massey Ferguson##350#')
)
)
->addAttributeToSelect('*');
if($_productCollection){
foreach($_productCollection as $product){
var_dump($product->getData());
}
}
What I want to be able to do is get the option ID's for attribute options that contain #Massey Ferguson# and then find any products with the returned option ID's..
Any suggestions?
Ok so I came up with a method for doing a like against a multiselect attribute option...
$make = $make.'%';
/** #var $attribute Mage_Eav_Model_Entity_Attribute */
$valuesCollection = Mage::getResourceModel('eav/entity_attribute_option_collection')
->setAttributeFilter($attribute->getId())
->addFieldToFilter('value', array ('like' => $make))
->addFieldToSelect('option_id')
->setStoreFilter(0, false);
$set = array();
foreach($valuesCollection as $option){
$set[] = $option->getData('option_id');
}
$_productCollection = Mage::getResourceModel('catalog/product_collection')
->addAttributeToFilter('makemodel',
array('in' => $set
)
)
->addAttributeToSelect('*');
This seems to work quite well! If anybody else has a solution that might work better please let me know.
Related
I have created a product attribute in Magento 2 that is a dropdown.
I have given this attribute many options. How can I output all these possible options attached to the attribute code/name? I can only find examples for Magento 1 or those that are assigned to a product. I want to get all the options assigned to the attribute.
This is what I tried before realising it wasnt for Magento 2.
<?php
$attributeId = Mage::getResourceModel(‘eav/entity_attribute’)->getIdByCode(‘catalog_product’,’color’);
$collection = Mage::getResourceModel(‘eav/entity_attribute_option_collection’)
->setPositionOrder(‘asc’)
->setAttributeFilter($attributeId)
->setStoreFilter(0)
->load();
print_r($collection->getData());
?>
you can try below code
function getAttrAllOptions() {
$eavConfig = $this->objectManager->get('\Magento\Eav\Model\Config');
$attribute = $eavConfig->getAttribute('catalog_product', 'color');
$alloptions = $attribute->getSource()->getAllOptions();
foreach($alloptions as $option) {
$label = $option['label'];
$AllOptionsArr[$label][] = $option['value'];
}
return $AllOptionsArr;
}
hope this helps!
I used this code:-
$category_id = 5;
$products = Mage::getModel('catalog/category')
->load($category_id)
->getProductCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('status',array(
'eq' => Mage_Catalog_Model_Product_Status::STATUS_DISABLED)
);
it's doesn't work. it's show empty result.
Please let me know how to solve this problem.
Thanks in advance.
Here is my solution:
First get a product collection
Add a filter for disabled products
Join the category table
Filter the category table for specific category id's
This is a working example if you want to run the script outside magento:
require_once('app/Mage.php');
umask(0);
Mage::app();
// set category id
$categoryIds = [4]; // add child category id's if you want to support child categories also
$productCollection = Mage::getModel('catalog/product')->getCollection();
$productCollection->addAttributeToSelect('*');
$productCollection->addAttributeToFilter('status', Mage_Catalog_Model_Product_Status::STATUS_DISABLED);
$productCollection->joinField('category_id','catalog/category_product','category_id','product_id=entity_id',null,'left')
->addAttributeToFilter('category_id', array('in' => $categoryIds))
->addAttributeToSelect('*');
$filteredProducts = $productCollection->getItems();
foreach ($filteredProducts as $product) {
echo $product->getId() . PHP_EOL;
}
Inside magento:
// set category id
$categoryIds = [4]; // add child category id's if you want to support child categories also
$productCollection = Mage::getModel('catalog/product')->getCollection();
$productCollection->addAttributeToSelect('*');
$productCollection->addAttributeToFilter('status', Mage_Catalog_Model_Product_Status::STATUS_DISABLED);
$productCollection->joinField('category_id','catalog/category_product','category_id','product_id=entity_id',null,'left')
->addAttributeToFilter('category_id', array('in' => $categoryIds))
->addAttributeToSelect('*');
$filteredProducts = $productCollection->getItems();
If you want to support multiple categories just add the id's to the array $categoryIds
If you have questions feel free to add a comment. If you got it working please let me know and add you magento version aswell. Thanks
I have a collection of products in my magento catalog that are tagged as samplers. I need to pull back this specific collection. The attribute is part of an attribute set called candies.
When I go to load the collection, I add a filter to narrow by the attribute set, and then add another filter for the sampler attribute I created. No matter what I do with the filter, I always get back ALL the candies not just the ones with the attribute of sampler set to "yes" or 1.
Here is my code, how do I get the result I am looking for?
$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*') //select all fields available to product
->addFieldToFilter('attribute_set_id', 9) //filter the collection by the candies attribute set
->addAttributeToFilter('sampler','yes'); //filter the collection to only products that have the attribute sampler set to "yes" - this part doesnt work.
Thanks in advance.
See this: http://gielberkers.com/magento-attributes-missing-using-flat-catalog/
After considering the comments here, I would try this setting.
You'll have iterate over the collection. Load the id of each product, then use ->getAttributeText('').
$collection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('type_id', 'configurable')
;
foreach ($collection as $_product) {
$id = $_product->getId();
if($tncValue = $_product->load($id)->getAttributeText('terms_and_conditions')){
echo $id . ' => ' . $tncValue . "\n";
}
}
<?php
$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->addFieldToFilter('attribute_set_id', 9)
->addAttributeToFilter('sampler', array('eq' => '1'));
?>
I'm trying to filter a magento product collection by a multiselect attribute with the following...
$_productCollection = Mage::getResourceModel('catalog/product_collection')
->addAttributeToFilter('sparex_makemodel',
array('in' => $set
)
)
->addAttributeToSelect('*');
However when I echo out the query it is not being applied? anybody got a clue why? I'm sure this was working earlier... driving me mad now
Matthew, you should check attribute option 'Used in Product Listing' is set to 'Yes'.
Try like this :
$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('sparex_makemodel', array('in'=>$set));
foreach($products as $product)
{
var_dump($product);
}
I'm trying to add product_type to my Magento Google Base output based on the product's categories, but I seem to be unable to. I have the following code:
// Get categories from product to include as product_type
$categoryIds = $object->getCategoryIds();
foreach($categoryIds as $categoryId) {
$category = Mage::getModel('catalog/category')->load($categoryId);
$this->_setAttribute('product_type', $category->getName(), 'text' );
}
The issue is that it returns all of the categories, not just the ones the product is in. Anyone have a solution?
Using the source link dropped by Rao above I actually found a better answer:
$product = Mage::getModel('catalog/product')->load($productId);
$cats = $product->getCategoryIds();
foreach ($cats as $category_id) {
$_cat = Mage::getModel('catalog/category')->load($category_id) ;
echo $_cat->getName();
}
This is utterly not tested..
//load the product
$product = Mage::getModel('catalog/product')->load($productId);
//load the categories of this product
$categoryCollection = $product->getCategoryCollection();
You can then loop through $categoryCollection like through an array.
source
Want to point out that the solution by #Rao is the best if you have a product object to get category ID's from as it makes only one SQL call.
If you just have category id's, you can do the following:
$categories = Mage::getModel('catalog/category')
->getCollection()
->addAttributeToSelect('name') //you can add more attributes using this
->addAttributeToFilter('entity_id', array('in'=>array(1,2,3)));
foreach($categories as $_cat){
$holder[]= $_cat->getName();
}
Where array(1,2,3) are your categories. Note that the array has integers, not string values, I found that SQL can be picky about that.
Also wanted to point out that solutions pulling one category at a time are very inefficient as it makes an SQL call for every iteration of the loop e.g.:
foreach(..){
Mage::getModel('catalog/category')->load($categoryId);
}
Get all Categories of the Product
<?php
$_Product = Mage::getModel("catalog/product")->load( PRODUCT_ID );
$categoryIds = $_Product->getCategoryIds();//array of product categories
foreach($categoryIds as $categoryId) {
$category = Mage::getModel('catalog/category')->load($categoryId);
$this->_setAttribute('product_type', $category->getName(), 'text' );
}
?>
Rao's solution tries to load all the attributes which means lots of queries and joins but you only need a product_id to load it's categories. So you can do this:
$product = Mage::getModel("catalog/product")->setId($productId);
$categories = $product->getCategoryCollection();
This will not load the product and will give you the categories.
This code work in phtml file in Magento 2
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');//get current product
$categories = $product->getCategoryIds(); /*will return category ids array*/