Solved

PHP Adding IF statment to class

Posted on 2014-04-23
4
315 Views
Last Modified: 2014-04-23
Hello,

I have the following class"

class Graffx_Autoorder_Helper_Data extends Mage_Core_Helper_Abstract
{
    const AUTO_PAYMENT_METHOD = 'banktransfer';
    const AUTO_SHIPPING_METHOD = 'fedex_GROUND_HOME_DELIVERY';
}

Open in new window


And I need to make it like this, but it is causing an error, not sure why but I am sure you will have the answer :-)

class Graffx_Autoorder_Helper_Data extends Mage_Core_Helper_Abstract
{
	const AUTO_PAYMENT_METHOD = 'banktransfer';

	$items = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();  
	$filter_cats = array(3,110,11,12,119,91);
	$found_cat = false;
	foreach($items as $item){  
		$categories_array = $item->getProduct()->getCategoryIds(); 
	foreach($categories_array as $cat){
		if( in_array($cat, $filter_cats) ){
			const AUTO_SHIPPING_METHOD = 'fedex_GROUND_HOME_DELIVERY';
		break;
	} else {
			const AUTO_SHIPPING_METHOD = 'flatrate_flatrate';
	}}

Open in new window

0
Comment
Question by:movieprodw
  • 2
4 Comments
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 300 total points
ID: 40018433
The answer is: you don't have to use classes if you don't know what they are! ;-)
I'm joking, but not completely: in a clas you can declare constant and variables, but hte rest must be nclosed in functions.

If you have to take decisions in a class, you have to do it in a function: depending on the type of decision, you can choose where (that is: when) take the decision, but it'll happen always within a class method.

For you, it could be the constructor:

class Graffx_Autoorder_Helper_Data extends Mage_Core_Helper_Abstract
{
	const AUTO_PAYMENT_METHOD = 'banktransfer';

        public function __constructor()
        {
    	      $items = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();  
	      $filter_cats = array(3,110,11,12,119,91);
	      $found_cat = false;
              foreach($items as $item)
             {  
		  $categories_array = $item->getProduct()->getCategoryIds(); 
	           foreach($categories_array as $cat)
                   {
		        if( in_array($cat, $filter_cats) )
                        {
			       const AUTO_SHIPPING_METHOD = 'fedex_GROUND_HOME_DELIVERY';
                               break;
                	 }
                         else 
                        {
		   	        const AUTO_SHIPPING_METHOD = 'flatrate_flatrate';
	                }
                    }
              }                         
         }//end constructor
...

Open in new window


But a constant is called this way because it can't change: yours are actually variables since they are subjected to some condition.

Don't be offended at all, but I suggest you to read some good manual about OOP before to proceed with your project in order to avoid to build something in the wrong way and have to return later and change it: it's harder than doing it the right way from the beginning.

Personally I would suggest these books:
http://www.sitepoint.com/books/phppro1/
http://www.apress.com/9781590599099/

You can also start here at EE with these two articles by our friend gr8gonzo:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_2626-Beginning-Object-Oriented-Programming-in-PHP.html
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_2631-Advanced-Object-Oriented-Programming-in-PHP.html

And oviously, always check the Php online manual: http://www.php.net/manual/en/language.oop5.php

Cheers
0
 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 200 total points
ID: 40018483
In the comment here, line five should refer to public function __construct()
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 40018491
Ooops! I'm sorry! I'm working with Delphi these days and I made confusion between two syntax. I apologize for that. :-)
0
 
LVL 1

Author Closing Comment

by:movieprodw
ID: 40018498
I do need to educate myself more and I appreciate the info!
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now