0

I want to get the custom customer attribute in the module and see if it is empty or not but this code doesn't work for me, can we get customer attribute in our module? should I use the products to get the customer attribute?

class X_MyCarrier_Model_Carrier extends Mage_Shipping_Model_Carrier_Abstract implements Mage_Shipping_Model_Carrier_Interface {

protected $_code = 'x_mycarrier';

public function collectRates(Mage_Shipping_Model_Rate_Request $request) {
   if(Mage::getSingleton('customer/session')->isLoggedIn()) {

        $customerData = Mage::getSingleton('customer/session')->getCustomer();
        $cust_id= $customerData->getId();
        $_customer = Mage::getModel('customer/customer')->load($cust_id);
        $_attr=$_customer->getCustomAttribute();
    }

and to check the attribute I used this:

   if(!empty($_attr)&& $_attr=='my_attribute')
            $result->append($this->_getExpressShippingRate());

also because of $request I can have :

  if ($request->getAllItems()) {

            foreach ($request->getAllItems() as $item) {
                if ($item->getProduct()

but I don't know how to get the customer here if I want to get the attribute with this way:

5
  • It's hard (for me at least) to follow your code. What are you trying to accomplish with the $_product instance that would involve the customer?
    – benmarks
    Feb 10, 2014 at 20:49
  • @benmarks it is just one other option to get customer attribute from the product id because I couldn't do anything to get it done
    – Nickool
    Feb 10, 2014 at 20:53
  • I can't understand the connection between product and customer attributes. Attributes are specific to one entity. Perhaps you could provide background on the business problem you are trying to solve?
    – benmarks
    Feb 10, 2014 at 20:55
  • I have multi shipping methods in check out and it has been written in module I want to show the shipping method if my customer attribute is not empty
    – Nickool
    Feb 10, 2014 at 20:58
  • forget about my $_products it was just my guess to use the current product and get the customer's attribute
    – Nickool
    Feb 10, 2014 at 21:02

2 Answers 2

0

I believe you can do it something along the lines of this:

echo $customer->getResource()
    ->getAttribute('my_attribute')
    ->getFrontend()
    ->getValue($customer);
4
  • thank you would you please tell me about $customer? you mean it is like my $_customer?
    – Nickool
    Feb 10, 2014 at 20:53
  • Yes, given the variables you have $customer would be $_customer. It just needs to be an instance of the customer model with the ID set on it (which yours has, since it was loaded by it). Feb 10, 2014 at 20:59
  • I tried your code it doesn't show me anything I don't know what is wrong here
    – Nickool
    Feb 10, 2014 at 21:00
  • I used your code in the front end thnx
    – Nickool
    Feb 11, 2014 at 16:24
0

It's not ideal (singletons rarely are used in an ideal way) but the same code snippet should work. However I'd suggest a modification, as it shouldn't be necessary to reload the customer instance:

public function collectRates(Mage_Shipping_Model_Rate_Request $request)
{
    if (Mage::getSingleton('customer/session')->isLoggedIn()) {
        /**
         * the following methods could be chained into the above conditional expression,
         * but it's a bit long to do this, so I'm just assigning to a local variable and
         * adding the hint for your IDE / reference.
         */
        $customer = Mage::getSingleton('customer/session')->getCustomer();
        /* @var $customer Mage_Customer_Model_Customer */

        //The getter will always map, no need to test if it is set
        if ($customer->getCustomAttribute() == 'my_attribute') {
            $result->append($this->_getExpressShippingRate());
        }
    }
}
1
  • I'm not sure but I think it is not working I am switching to write code in the front end
    – Nickool
    Feb 10, 2014 at 22:00

Your Answer

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

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