The idea of object-oriented programming is widely used in the WooCommerce system. When it comes to object-oriented programming, classes and objects are indispensable. All classes in WooCommerce can beAPI DOCSfound in. The following are some of the core classes in WooCommerce.
Woocommerce
The Woocommerce class is the most important class in WooCommerce. We can use global variables$woocommerceAccess this class. This class contains the main functions of WooCommerce, initializes other classes, stores global variables, and handles error/success messages. When Woocommerce is initialized, it contains the following instances.
- WC_Query – stored in $woocommerce->query
- WC_Customer – stored in $woocommerce->customer
- WC_Shipping – stored in $woocommerce->shipping
- WC_Payment_Gateways – stored in $woocommerce->payment_gateways
- WC_Countries – stored in $woocommerce->countries
Other classes are automatically loaded as needed.
WC_Product product category
WooCommerce has several product classes that are responsible for loading and outputting product data. This class can be usedwc_get_productFunction loading:
$product = wc_get_product( $post->ID );
In a loop, this method is not always needed, when we callthe_post()method, if the article is a product, the global variable$productwill be loaded automatically.
WC_Customer customer class
The Customer class allows us to get data about the current customer. For example, if we need to get the country of a customer:
global $woocommerce;
$customer_country = $woocommerce->customer->get_country();
More methods can be viewed in the API documentation.
WC_Cart shopping cart class
The shopping cart class loads and stores the user’s shopping cart data in a session. For example, to get the user’s shopping cart subtotal, we can do this:
global $woocommerce;
$cart_subtotal = $woocommerce->cart->get_cart_subtotal();
More methods can be viewed in the API documentation.
The above is justWooCommerce secondary developmentClasses that are frequently used in the API. More classes can be viewed in the official API documentation. If you encounter problems while using the above classes, please feel free to communicate in the documentation.
