Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PHP Adding IF statment to class

Posted on 2014-04-23
4
Medium Priority
?
322 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
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.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

618 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