1

I have two functions within my class and need to use information from one to make a decision in the other. I though I could change the value of a property just like it works in Javascript functions by just setting it equal to a new value, but that's a big misunderstanding. How can I change the value of a property throughout a class?

class Show_Or_Not {

    public $num;

    public function __construct() {

     add_action( 'woocommerce_cart_calculate_fees', array( $this, 'check_cart_for_condition'), 50 );
     add_filter( 'wc_add_to_cart_message', array( $this, 'use_the_cart_condition'), 100, 2 );

    }

    public function check_cart_for_condition() {

      // Ton of code checking how often a certain category occurs in the cart.

      if ( $cat_in_cart == 1 ) {
          // Trying to update value of class property in 
          // order to use it in the next function.
          $this->num = 1;
      } elseif ( $cat_in_cart == 2 ) {
          // Trying to update value of class property in 
          // order to use it in the next function.
          $this->num = 2;
      }

    }

    public function use_the_cart_condition() {

      // If condition determined in upper function is met. 
      if ( $this->num == 1 ) {
         // Do something
      } elseif ( $this->num == 2 ) {
         // Do something
      }

    }

}

$newClass = new Show_Or_Not();
5
  • Showing the class definition is a good start, but could you also please post some example code showing how you want to instantiate and use this class? Commented Apr 22, 2015 at 1:08
  • Seems that you have to pass a parameter in set_the_condition(), something like set_the_condition($condition ) Commented Apr 22, 2015 at 1:08
  • Where's the rest of your code? How are you calling the methods? Commented Apr 22, 2015 at 1:21
  • @Ja͢ck I've updated the context. The point is that I can't both set and use the condition in the same function (using WordPress actions and filters), that's why I though It could use a class property to do this. My question is more PHP than WordPress related though, just pointing out that I'm trying to update a property to use it further down. Commented Apr 22, 2015 at 1:32
  • I guess it depends on which method is called first; how about, inside use_the_cart_condition() you always call $this->check_cart_for_condition();? Commented Apr 22, 2015 at 2:05

1 Answer 1

2

When you call an action or a filter, the second value needs to be the name of a method that can be found in your theme's functions.php file. You can add custom methods to the file.

// In the functions.php file for the theme

function check_cart_for_condition() {
     // get the session
     global $session;

    // initialize the $num var
    $num = 0;
    // Ton of code checking how often a certain category occurs in the cart.
    if ( $cat_in_cart == 1 ) {
        // Trying to update value of class property in 
        // order to use it in the next function.
        $num = 1;
     } elseif ( $cat_in_cart == 2 ) {
        // Trying to update value of class property in 
        // order to use it in the next function.
        $num = 2;
    }

    // This only needs to be for the next request since the hooks
    // run back to back, add it to the session flash data
    $session->set_flashdata( 'num', $num );
}


public function use_the_cart_condition() {
     global $session;
     // Retrieve Flashdata
     $num = $session->flashdata( 'num' );

    // If condition determined in upper function is met. 
    if ( $this->num == 1 ) {
       // Do something
    } elseif ( $this->num == 2 ) {
       // Do something
    }
}

Now just add this to your code where you need it:

 add_action( 'woocommerce_cart_calculate_fees','check_cart_for_condition', 50 );
 add_filter( 'wc_add_to_cart_message','use_the_cart_condition', 100, 2 );
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for you reply, my bad I missed that. I'll provide more context to the issue right now. It's not that I'm experiencing a fatal error.
I've taken the liberty to tidy up the first part of your answer ;-)
Thanks Jack. I had considered making it one section but figured I'd try to be as verbose as possible.
Ok, so, going to modify my answer, but what I think you are trying to achieve within a class isn't possible. I could be wrong, but I will explain in my answer.
Thanks! This is an approach that works. WordPress does not support $session, but WooCommerce does happen to have to ability to set and get session data using WC()->session->set/get.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.