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
Solved

PHP Adding IF statment to class

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP Echo with auto submit 8 29
IP 10.0.1.2 / 255.0.0.0 61 56
Correct syntax for subdirectories 9 19
Wordpress Query 5 27
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…
This article discusses how to create an extensible mechanism for linked drop downs.
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.

829 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