3

I need to put a check whether the product have the particular custom option or not?

I have product ID and the custom option title is Custom Size now I want to check that for the product with this I'd have any custom option with title size or not.

I hope anyone came across this.

4 Answers 4

6

You could load the options collection and filter by product id and title.

$options = Mage::getModel('catalog/product_option')
    ->getCollection()
    ->addTitleToResult(Mage::app()->getStore()->getId())
    ->addPriceToResult(Mage::app()->getStore()->getId())
    ->addProductToFilter(array('product_id'))
    ->addValuesToResult()
    ->addFieldToFilter('default_option_title.title', 'Option title');

With the title there is a default option and also a store title so you should us either of the following lines.

->addFieldToFilter('default_option_title.title', 'Option title')
->addFieldToFilter('store_option_title.title', 'Option title')
2
  • ,good work..it is good logic,i have give you upvote
    – Amit Bera
    Commented Jul 20, 2014 at 5:11
  • @David, is it feasible to filter by 'title'? Commented Mar 10, 2015 at 6:18
1

I have tried things but as it was very urgent, i did it with a code patch. i would like to share it.

$product_has_size = 0; 
foreach ($productCollection->getOptions() as $value) { 
if ( $value->getTitle() == 'Size' ) {   $product_has_size = 1;  }
}

/* Value 0 of this variable states that the product doesn't have size option. and 1 means it has the size option. */
1

If by custom option you mean a product attribute, you could simply define a function in your helper like this

public function isCustomSize($product) 
    {
        $attributeSetModel = Mage::getModel("eav/entity_attribute_set");
        $attributeSetModel->load($product->getAttributeSetId());
        $attributeSetName  = $attributeSetModel->getAttributeSetName();

        if($attributeSetName == 'Custom Size')
            return true;
        else
            return false;
    }

EDIT:

To check the the product option

$product = Mage::getModel('catalog/product')->load($productId);

if ($product->hasOptions()) {
    foreach ($product->getOptions() as $option) {
        if ($option->getTitle() === 'Custom Size') {
            echo 'Product '. $product->getName() . ' has a custom size option!';
        }
}
3
  • 1
    I don't think he means 'Product attribute' by 'custom option'. I think he means this: magentocommerce.com/knowledge-base/entry/….
    – Marius
    Commented Oct 4, 2013 at 8:43
  • Yes Marius you are right i exactly mean this.
    – Sarvagya
    Commented Oct 4, 2013 at 11:06
  • Ok, my bad. I'll try to edit it as soon as i have time
    – VladH
    Commented Oct 7, 2013 at 7:17
0

use the method $product->getHasOptions()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.