- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
How to do selected single product should be purchase alone?
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: How to do selected single product should be purchase alone?
Hello @Aveeva
Code looking good.
Have you checked disabled compile and cache flush after module install?
One thing can you please change it
<frontend> <events> <controller_action_predispatch_checkout_cart_add> <observers> <brst_test_log_cart_add> <class>brst_test/observer</class> <method>logCartAdd</method> </brst_test_log_cart_add> </observers> </controller_action_predispatch_checkout_cart_add> </events> </frontend> to <frontend> <events> <controller_action_predispatch_checkout_cart_add> <observers> <brst_test_log_cart_add> <class>Brst_Test_Model_Observer</class> <method>logCartAdd</method> </brst_test_log_cart_add> </observers> </controller_action_predispatch_checkout_cart_add> </events> </frontend>
Hope it will help you.
If works then mark as a solution.
Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: How to do selected single product should be purchase alone?
@Sunil Patel Still adding other products, anything is missed?
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: How to do selected single product should be purchase alone?
Hello @Aveeva
can you change version something else like 0.1.0 as per magento all module three dot and into your module four.
if still not work then move events to global area and check it.
Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: How to do selected single product should be purchase alone?
@Sunil Patel Version changing not working,
<?xml version="1.0"?> <config> <modules> <Brst_Test> <version>0.1.0</version> </Brst_Test> </modules> <global> <models> <brst_test> <class>Brst_Test_Model</class> </brst_test> </models> </global> <frontend> <events> <controller_action_predispatch_checkout_cart_add> <observers> <brst_test_log_cart_add> <class>Brst_Test_Model_Observer</class> <method>logCartAdd</method> </brst_test_log_cart_add> </observers> </controller_action_predispatch_checkout_cart_add> </events> </frontend> </config>
After move events to a global area, still not working
<?xml version="1.0"?> <config> <modules> <Brst_Test> <version>0.1.0</version> </Brst_Test> </modules> <global> <models> <brst_test> <class>Brst_Test_Model</class> </brst_test> </models> <events> <controller_action_predispatch_checkout_cart_add> <observers> <brst_test_log_cart_add> <class>Brst_Test_Model_Observer</class> <method>logCartAdd</method> </brst_test_log_cart_add> </observers> </controller_action_predispatch_checkout_cart_add> </events> </global> <!-- <frontend> <events> <controller_action_predispatch_checkout_cart_add> <observers> <brst_test_log_cart_add> <class>Brst_Test_Model_Observer</class> <method>logCartAdd</method> </brst_test_log_cart_add> </observers> </controller_action_predispatch_checkout_cart_add> </events> </frontend> --> </config>
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: How to do selected single product should be purchase alone?
Hello @Aveeva
First check is your compiler on?
is yes then disabled it.
Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: How to do selected single product should be purchase alone?
@Sunil Patel My compiler status disabled,
caps and the small case name is okay for below code,
<?xml version="1.0"?> <config> <modules> <Brst_Test> <version>0.1.0</version> </Brst_Test> </modules> <global> <models> <brst_test> <class>Brst_Test_Model</class> </brst_test> </models> </global> <frontend> <events> <controller_action_predispatch_checkout_cart_add> <observers> <brst_test_log_cart_add> <class>Brst_Test_Model_Observer</class> <method>logCartAdd</method> </brst_test_log_cart_add> </observers> </controller_action_predispatch_checkout_cart_add> </events> </frontend> </config>
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: How to do selected single product should be purchase alone?
How to use the log and how to find out where is the error presented?
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: How to do selected single product should be purchase alone?
@Sunil Patel FYI
app/code/community/Brst/Test/Model/Observer.php
<?php // Mage::log('Hy observer called', null, 'logfile.log'); class Brst_Test_Model_Observer { //Put any event as per your requirement public function logCartAdd() { $product = Mage::getModel('catalog/product') ->load(Mage::app()->getRequest()->getParam('product', 0)); $cart_qty = (int) Mage::getModel('checkout/cart')->getQuote()->getItemsQty(); if ($product->getId()==31588 && cart_qty > 0) { Mage::throwException("You can not add This special Product, empty cart before add it"); } // $quote = Mage::getSingleton('checkout/session')->getQuote(); // if ($quote->hasProductId(2)) { // Mage::getSingleton("core/session")->addError("Cart has Special Product you can not add another"); // return; // } $quote = Mage::getModel('checkout/cart')->getQuote(); foreach ($quote->getAllItems() as $item) { $productId = $item->getProductId(); if($productId==31588){ Mage::throwException("Cart has Special Product you can not add another"); } } } } ?>
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: How to do selected single product should be purchase alone?
@Sunil Patel After long googling, getting some relevant information, now restricted products not added to cart also not adding if no products available in cart, i know finally logic working but still error, can i get help,
app/etc/modules/Brst_Test.xml
<?xml version="1.0" encoding="UTF-8"?> <config> <modules> <Brst_Test> <active>true</active> <codePool>community</codePool> </Brst_Test> </modules> </config>
app/code/community/Brst/Test/Model/Observer.php
<?php ini_set('display_errors', '1'); // Mage::log('fine dude', null, 'logfile.log'); class Brst_Test_Model_Observer { //Put any event as per your requirement public function logCartAdd($observer) { $product = Mage::getModel('catalog/product') ->load(Mage::app()->getRequest()->getParam('product', 0)); $cart_qty = (int) Mage::getModel('checkout/cart')->getQuote()->getItemsQty(); if ($product->getId()==31588 && $cart_qty > 0) { Mage::throwException("You can not add This special Product, empty cart before add it"); } $quote = Mage::getModel('checkout/cart')->getQuote(); foreach ($quote->getAllItems() as $item) { $productId = $item->getProductId(); // $productId = $observer->getEvent()->getProduct()->getId(); if($productId==31588){ Mage::throwException("Cart has Special Product you can not add another"); } } } } ?>
app/code/community/Brst/Test/etc/config.xml
<?xml version="1.0"?> <config> <modules> <Brst_Test> <version>1.0.0</version> </Brst_Test> </modules> <global> <models> <brst_test> <class>Brst_Test_Model</class> </brst_test> </models> </global> <frontend> <events> <checkout_cart_product_add_after> <observers> <Brst_Test_Model_Observer> <type>singleton</type> <class>Brst_Test_Model_Observer</class> <method>logCartAdd</method> </Brst_Test_Model_Observer> </observers> </checkout_cart_product_add_after> </events> </frontend> </config>
Can i get help?