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
1 Answer
Reset to default 0If 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
版权声明:本文标题:woocommerce offtopic - Use PHP Class in Wordpress functions 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736286359a1927653.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论