PHP – Change value of property throughout class

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();

Related posts

Leave a Reply

1 comment

  1. 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 );