Solved

PHP Adding IF statment to class

Posted on 2014-04-23
4
312 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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

920 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

16 Experts available now in Live!

Get 1:1 Help Now