Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use wc() in woocommerce?

I'm working on using the wc() function in woocommerce. The documentation says it Returns the main instance of WC to prevent the need to use globals.

I can't find any examples of the use of this and I would like to know how to use wc() to do some basic things. I understand that it returns the main woocommerce instance; and from that I can extract all the data I would need; but I don't know the syntax for correct use ... might it be something like?

$foo = WC();
$bar = $foo->cart;
echo $bar;

could someone please correct this.

Also I am trying to understand what the advantages of doing it this way instead of globalizing variables.

like image 924
byronyasgur Avatar asked Mar 16 '15 14:03

byronyasgur


1 Answers

as what the doc in your link says. 'prevent the need to use globals'. An example would be like this...

code using global.

global $woocommerce;
$customer_country = $woocommerce->customer->get_country();

code not using global

$customer_country = WC()->customer->get_country(); 
// some servers may not like like this... best is to use variables like $foo = WC(); then use $foo->customer->get_country()...

How to use WC() ? start here...

why must I avoid global?

like image 146
Reigel Avatar answered Oct 04 '22 05:10

Reigel