问题
I am new to wordpress, trying to develop a wordpress plugin where I need to call a woocommerce method add_to_cart
from the class woocommerce/includes/class-wc-cart.php
. Is there any way to do that ?
回答1:
WooCommerce declares a handy globlal WC()
that you can use inside your plugin to call its functions.
Add the following code to your plugin
add_action('woocommerce_after_single_product', 'woo_foo');
function woo_foo() {
WC()->cart->add_to_cart( 254, 1 ); //ensure to change 254 with product ID on your system.
}
Above code will automatically add a product to the cart when you visit the single product page. Here's a list of hooks & filters offered by WooCommerce that you can hook into.
来源:https://stackoverflow.com/questions/32011391/call-a-function-of-woocommerce-plugin-from-my-plugin