admin管理员组

文章数量:1122846

I have a class that I want to use in functions.php but I'm not sure the best way to set it up

I also want to get WooCommerce's cart array. I currently do not have access to WC() in my class yet.

What is the best route to achieve OOP in functions.php and whats the best way to get WooCommerce's class WC() to my class so I can use for other functions.

my code in functions.php

class MyCart {
    public $cart_contents;

    public function __construct(){
        add_action( 'get_header', [ $this, 'set_users_cart_contents' ] );
        //$this->set_users_cart_contents();
    }

    public function set_users_cart_contents() {
        $this->cart_contents = WC()->cart->get_cart_contents(); // what i want to use
        //$this->cart_contents = ['test' => '123']; // dummy array for testing
    }

    public function get_users_cart_contents() {
        return $this->cart_contents;
    }
} // class

$cartClass = new MyCart();

// add action
function emdr_add_to_cart(){
    // I want to use my class here
    // $items = $cartClass->get_users_cart_contents();
}
add_action( 'woocommerce_add_to_cart', 'emdr_add_to_cart');

// add action
function cart_page_logic(){
    if(is_cart()){
        // I want to use my class here
        // $items = $cartClass->get_users_cart_contents();
    }
}
add_action( 'get_header', 'cart_page_logic' );

I have a class that I want to use in functions.php but I'm not sure the best way to set it up

I also want to get WooCommerce's cart array. I currently do not have access to WC() in my class yet.

What is the best route to achieve OOP in functions.php and whats the best way to get WooCommerce's class WC() to my class so I can use for other functions.

my code in functions.php

class MyCart {
    public $cart_contents;

    public function __construct(){
        add_action( 'get_header', [ $this, 'set_users_cart_contents' ] );
        //$this->set_users_cart_contents();
    }

    public function set_users_cart_contents() {
        $this->cart_contents = WC()->cart->get_cart_contents(); // what i want to use
        //$this->cart_contents = ['test' => '123']; // dummy array for testing
    }

    public function get_users_cart_contents() {
        return $this->cart_contents;
    }
} // class

$cartClass = new MyCart();

// add action
function emdr_add_to_cart(){
    // I want to use my class here
    // $items = $cartClass->get_users_cart_contents();
}
add_action( 'woocommerce_add_to_cart', 'emdr_add_to_cart');

// add action
function cart_page_logic(){
    if(is_cart()){
        // I want to use my class here
        // $items = $cartClass->get_users_cart_contents();
    }
}
add_action( 'get_header', 'cart_page_logic' );
Share Improve this question asked Nov 19, 2020 at 16:40 bghousebghouse 1014 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

If you have defined the class MyCart directly in functions.php with your hooks. I'd expect for the following to work:

// add action
$cart = new MyCart();
function emdr_add_to_cart(){
    $items = $cartClass->get_users_cart_contents();
    // process items
}
add_action( 'woocommerce_add_to_cart', 'emdr_add_to_cart');

I might also add that duplicating the "state" of the users cart in 2 places is probably a code smell. I would suggest using the values directly from the WooCommerce cart.

本文标签: woocommerce offtopicUse PHP Class in Wordpress functions