skip to Main Content

I completely edited my question.

I use the YITH WooCommerce Compare plugin, I will put the file that contains the codes that are related to my question : https://file.io/StHr7KBJBdxF

$current_cat variable has been set equal to : public $current_cat = array();, $this->current_cat has been used in some parts of the code, How to call the $this->current_cat outside the file and in the function file?

class YITH_Woocompare_Frontend_Premium extends YITH_Woocompare_Frontend {

    …

    /**
     * The list of current cat inside the comparison table
     *
     * @since 1.0.0
     * @var array
     */
    public $current_cat = array();

For example: used a $this->products_list;, If I want to call it outside the file and in the function file, it goes like this : $products_list = isset( $_COOKIE[ get_cookie_name() ] ) ? json_decode( wp_unslash( $_COOKIE[ get_cookie_name() ] ) ) : array();

I hope you understand what I mean. If this is a beginner’s question, please don’t diss because not everyone is a professional like you. Also, I spent a lot of time solving the problem. It’s not like I want to quickly ask my question here to quickly find an answer.

2

Answers


  1. Chosen as BEST ANSWER

    After much effort, I was able to call $current_cat variable outside the plugin and in the function file without the YITH WooCommerce Compare plugin installed.

    function get_product_categories( $product_id ) {
        $cat        = array();
        $categories = array();
        if ( ! is_array( $product_id ) ) {
            $categories = get_the_terms( $product_id, 'product_cat' );
        } else {
            foreach ( $product_id as $id ) {
                $single_cat = get_the_terms( $id, 'product_cat' );
                if ( empty( $single_cat ) ) {
                    continue;
                }
                $single_values = array_values( $single_cat );
                $categories = array_merge( $categories, $single_values );
            }
        }
        if ( empty( $categories ) ) {
            return $cat;
        }
        foreach ( $categories as $category ) {
            if ( ! $category ) {
                continue;
            }
            $cat[ $category->term_id ] = $category->name;
        }
        return apply_filters( 'yith_woocompare_get_product_categories', $cat, $categories, $product_id );
    }
    
    function get_cookie_name() {
        $suffix = '';
        if ( is_multisite() ) {
            $suffix = '_' . get_current_blog_id();
        } elseif ( '/' !== COOKIEPATH ) {
            $suffix = '_' . sanitize_title( COOKIEPATH );
        }
        return 'yith_woocompare_list' . $suffix;
    }
    
    $products_list = isset( $_COOKIE[ get_cookie_name() ] ) ? json_decode( wp_unslash( $_COOKIE[ get_cookie_name() ] ) ) : array();
    if ( isset( $_REQUEST['yith_compare_cat'] ) ) {
        $current_cat = array( absint( $_REQUEST['yith_compare_cat'] ) );
    } elseif ( ! empty( $categories ) ) {
        $current_cat = $categories;
    } elseif ( ! empty( $products_list ) ) {
        $products    = $products_list;
        $product     = array_pop( $products );
        $categories  = array_keys( get_product_categories( $product ) );
        $current_cat = $categories;
    } else {
        $current_cat = $categories;
    }
    

  2. Your current_cat is a public property of a class defined by your plugin.

    Within functions that are part of that same class, code can access that property with $this->current_cat. Why? $this is, inside that class’s code, a reference to the current instance of the class. The -> object operator tells php to look inside $this.

    Outside that class’s code you can access public properties like so.

    $my_object = new PluginClass();
    $cat = $my_object->current_cat;
    

    You are trying to do the thing shown in the second line of that example. To do it you need access to the variable returned by new PluginClass() in the first line. Different plugins have different ways of making that kind of information available to theme code (functions.php). It’s necessary to look at the plugin’s code to figure out how to do that, or look at the documentation, or consult the plugin’s support forum.

    With respect, explaining how php classes work is too complex for a Stack Overflow answer. So is explaining how WordPress, plugins, and theme code interact. I hope this answer points you in a useful direction.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search