2

I am trying to implement the card only payment option for specific products (configurable products) in this magento (1.9.1) store. The problem i am having with the code that i am using is that i need to go to each simple product and update the attribute value to take effect. What I would like to know if i can make changes to the code so instead of using the value of the simple product can the value of the parent product be used, regardless of what value the simple product is using. So i would just have to update the configurable product to say if this product is Card Only.

My Observer.php for this module

<?php

class JMAWD_CardOnly_Model_Observer
{
public function cardOnly(Varien_Event_Observer $observer)
{
   $event           = $observer->getEvent();
       $method          = $event->getMethodInstance();
       $result          = $event->getResult();
   $cardonly        = false;

    foreach (Mage::getSingleton('checkout/cart')->getQuote()->getAllVisibleItems() as $item)
    {
        if($item->getProduct()->getCardOnly()){
            $cardonly = true;
        }
    }

    if($method->getCode() == "cashondelivery" && $cardonly){
        $result->isAvailable = false;
    }

}
}

My config.xml for this module

<?xml version="1.0"?>
<config>
<modules>
    <JMAWD_CardOnly>
        <version>0.1.0</version>
    </JMAWD_CardOnly>
</modules>
<global>
    <events>
        <payment_method_is_active>
            <observers>
                <card_only>
                    <type>singleton</type>
                    <class>cardonly/observer</class>
                    <method>cardOnly</method>
                </card_only>
            </observers>
        </payment_method_is_active>
    </events> 
    <models>
        <cardonly>
            <class>JMAWD_CardOnly_Model</class>
            <resourceModel>cardonly_mysql4</resourceModel>
        </cardonly>
    </models>
    <sales>
        <quote>
            <item>
                <product_attributes>
                    <card_only/>
                </product_attributes>
            </item>
        </quote>
    </sales>
</global>
</config>

Help & advice appreciated. Thanks

AlphaOne
  • 123
  • 1
  • 3
  • 14

2 Answers2

0

If you have configurable childrens in your quote you can get the configurable parents and check for the card_only attribute. This way you don't need to care about the simple childs.

Something like this:

$quoteItemsCollection = Mage::getSingleton('checkout/cart')->getQuote()->getItemsCollection();
foreach ($quoteItemsCollection as $quoteItem) {
    $productType = $quoteItem->getProductType();
    // if productType is config and getCardOnly(), then $cardonly = true;
clinical
  • 587
  • 4
  • 17
0

This should not happen with getAllVisibleItems(). As explained in this answer:

Loads the item collection, then returns an array of all items which are not marked as deleted AND do not have a parent (i.e. you get items for bundled and configurable products but not their associated children). Each array item corresponds to a displayed row in the cart page.

I suspect that you are using an extension like Simple Configurable Products or Better Configurable Products, which change the configurable product type such that the attributes of the simple products are displayed and used (especially the prices), and the product in the cart is actually just the simple product without a parent.

If this is the case, you have to inspect the code of the extension to find out where and how it stores the information about the configurable product. You can also look at the sales_flat_quote_item_option table because that's usually where all additional information about quote items is stored.

Community
  • 1
  • 1
Fabian Schmengler
  • 24,155
  • 9
  • 79
  • 111
  • Thanks for your reply. Yes I am using the Simple Configurable Products extension. Thank you for your insight. I will look into this. – AlphaOne Dec 20 '15 at 21:27