i am fetching a product collection and i want to use like condition on custom attribute but the problem is that while using like condition i want to remove all white space contain in my custom attribute value.
i have already tried
$psku = 'some_sku';
$_product = Mage::getModel('catalog/product')->getCollection();
$_product->addFieldToFilter(str_replace(' ', '', 'simple_skus_map'),
array(array('like' => '%,'.$psku.',%'),
array('like' => '%,'.$psku),
array('like' => $psku.',%'),
array('like' => $psku)
));
// simple_skus_map : (my custom attribute has data like one, two, three ,four). and i want the following code should fetch all the product which simple_skus_map contains any of the above mentioned word(i.e one/two/three/four)
NOTE: noticed? i have spaces in my custom attribute.
3
Answers
for custom attribute you can do as following, hence it will remove white space from your custom attribute value and match the given/post data
The query as you supplied can perform slow, as it uses like with wildcards. You can use the find_in_set functionality.
Luckily magento supports this as well:
I have another solution, with direct SQL queries.
here is the php code:
this is simply, not very high performance, but works.
Just remember when you write data make sure to set the correct value in the correct fields (you can maybe creating a temporary support table, with |field|id| ).
This will remove your blanks from the selected field, and replace them with a non blanks value(or whatever you want to implode them with, just check the implode function).
“test field blanks” => “testfieldsblanks”