?
Solved

PHP Adding IF statment to class

Posted on 2014-04-23
4
Medium Priority
?
321 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 1200 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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 800 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

765 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