Construct a method name for customer object dynamically
-
13-12-2019 - |
Question
I have a Magento website with four stores. For each customer different cut off time will be assigned for each store.
User is restricted to add product from only one store at a time. When user come to check out page I have to get the cut off time for the store from which he added the product.
I managed to get the store code.
Customer attributes for store cut off time are as below
`delivery_cutoff_time_abc` // here abc is the store code
`delivery_cutoff_time_def` // here def is the store code
`delivery_cutoff_time_ghi` // here ghi is the store code
`delivery_cutoff_time_jkl` // here jkl is the store code
Based on products added to cart I got the Store code whether it itsabc
or def
or so.
Now I have to get the value from customer object as below
$customer = Mage::getSingleton('customer/session')->getCustomer();
$customer->getDeliveryCutoffTimeAbc();
For doing this I have store code as below
$storeCode = "abc";
I have to use dynamic as below
$customer->getDeliveryCutoffTime.$storeCode();
It doesn't get the value. How may I make it dynamic?
Solution
Try
$storeCode = ucfirst("abc");
$function = "getDeliveryCutoffTime".$storeCode;
$customer->$function();
OTHER TIPS
In PHP, this can also be done like this:
$storeCode = "abc";
$customer->{"getDeliveryCutoffTime" . ucfirst($storeCode)}();
These Magento "magic" getter methods call the method getData()
eventually. So getDeliveryCutoffTimeAbc()
will result in a call to getData("delivery_cutoff_time_abc")
.
So you could also call this right away:
$storeCode = "abc";
$customer->getData("delivery_cutoff_time_" . $storeCode);