phpwordpresswoocommercewordpress-themingwordpress-plugin-creation

Calling the variable outside the plugin file and inside the function file


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.


Solution

  • 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;
    }