Call a function of Woocommerce plugin from my plugin

拟墨画扇 提交于 2019-12-11 04:00:05

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!