I am working on a project which has a category.

And I want to add product buying option only for that category products. Other categories don't have product buying options.

I want to know the best way to implement this. Please explain in detail.

  • product buying option ??? – Yehuda Schwartz Feb 20 '19 at 15:34
  • Means add to cart functionality for category products. – Smita Kagwade Feb 20 '19 at 16:16

You can user observer the controller_action_predispatch_checkout_cart_add event.

<config> [...] <frontend> <events> <controller_action_predispatch_checkout_cart_add> <observers> <namespace_module_add_product> <class>namespace_module/observer</class> <method>logCartAdd</method> </namespace_module_add_product> </observers> </controller_action_predispatch_checkout_cart_add> </events> </frontend> [...] </config>

Create New observer class php file

class Namespace_Module_Model_Observer { public function logCartAdd($observer) { $categoryId = "YOUR_CATEGORY_ID"; $product = Mage::getModel('catalog/product') ->load(Mage::app()->getRequest()->getParam('product', 0)); if (!$product->getId()) { return; } $categories = $product->getCategoryIds(); if (!in_array($categoryId, $categories) { Mage::throwException('Your Error Message'); } } }


You can add condition in template file


<?php $_product = $this->getProduct(); $requirelogincategory = [ID of the category that requires login]; $buttonTitle = $this->__('Add to Cart'); $categoryId = "YOUR_CATEGORY_ID"; $categories = $product->getCategoryIds(); if (in_array($categoryId, $categories): if($_product->isSaleable()): ?> <div class="add-to-cart"> <?php if(!$_product->isGrouped()): ?> <label for="qty"><?php echo $this->__('Qty:') ?></label> <input type="text" name="qty" id="qty" maxlength="12" value="<?php echo $this->getProductDefaultQty() * 1 ?>" title="<?php echo $this->__('Qty') ?>" class="input-text qty" /> <?php endif; ?> <button type="button" title="<?php echo $buttonTitle ?>" class="button btn-cart" onclick="productAddToCartForm.submit(this)"><span><span><?php echo $buttonTitle ?></span></span></button> <?php echo $this->getChildHtml('', true, true) ?> </div> <?php endif; else: ?> <p><?php echo $this->__('Write Error Message Here');?></p> <?php endif; ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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