0

I have restricted add to cart for not logged customer, but need also funcionality to restrict it for General group. How to do it? Part off my code

<!--moj kod usun add to cart-->
                <?php if (Mage::getSingleton('customer/session')->isLoggedIn()) { ?>
                    <div class="add-to-box">
                          <?php if($_product->isSaleable()): ?>
                            <?php echo $this->getChildHtml('addtocart') ?>
                        <?php endif; ?>
                        <?php //echo $this->getChildHtml('addto') ?>
                    </div>
                    <?php } ?>
                    <!--koniec mojego kodu-->
3

Another option would be to create a module with an observer that is listening to the event catalog_product_is_salable_after.

Then in your observer you could do the following:

$salable = $oObserver->getEvent()->getSalable();
if (user is not logged in OR user is part of group general) {
    $salable->setIsSalable(false);
}

This way you would not have to update an template files and it should be fairly upgrade safe.

Note: That this simply hides the button and if the user guesses the url correctly they can still add the product to the cart for that I would recommend listening to catalog_product_type_prepare_full_options as well

0

When a user is logged in the group information can be found in the customer/session. To get more detailed information on the group, you can use the following snippet.

if (Mage::getSingleton('customer/session')->isLoggedIn()){
    $groupId = Mage::getSingleton('customer/session')->getCustomerGroupId();

    // it's best to load the group to get the group code for matching instead of the id
    $group = Mage::getModel('customer/group')->load($groupId);

    // do your thing here
}
  • I'm new in magento, sorry. How can I use this snippet? Can you explain me a bit more? – Zydol Feb 28 '14 at 9:40
  • To start with you could add the logic to the template you already changed in your question. There you should do a check on $group->getCode() whether the current user is part of the group you wish to enable to add products to the basket. So, like in the snippet, use the getCustomerGroupId() call on the session to get the ID of the group, and afterwards use that ID to fetch the group data. When you get that part working I suggest you move the logic to a helper to keep your templates cleaner. – Tim Hofman Feb 28 '14 at 9:45
  • Ok, I have it just to change first line for: <?php if (Mage::getSingleton('customer/session')->getCustomerGroupId()>1) { ?> – Zydol Feb 28 '14 at 12:59
  • That should do the trick. But code-wise its better to match against the code (see the snippet in my answer) of the group instead of the ID. The ID doesn't tell a lot when other developers look at your code. – Tim Hofman Feb 28 '14 at 13:45
  • Yeah, but I'm noob and don't understtod a lot of thing in Magento. I think, that all developer can look at there PA/Customers/Group Customers and then they will see all ID's :). One more time Thank you Tim. – Zydol Feb 28 '14 at 17:53
0

You can use event checkout_cart_product_add_after , put this code in yourmodule/etc/config.xml

         <checkout_cart_product_add_after>
                <observers>
                    <yourmodule>
                        <type>singleton</type>
                        <class>yourmodule/observer</class>
                        <method>restrictAddToCart</method>
                    </yourmodule>
                </observers>
        </checkout_cart_product_add_after>

Also in observer.php put this code

public function restrictAddToCart(Varien_Event_Observer $observer){    
  if(check for group condition){
    Mage::getSingleton('core/session')->addNotice("Set warning message here !");                            
                         $url =Mage::getModel('core/url')->getUrl('checkout/cart');
                        Mage::app()->getResponse()->setRedirect($url);
                        Mage::app()->getResponse()->sendResponse();
                        exit;   
   }
}

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.