Call a function of Woocommerce plugin from my plug

2019-07-10 19:42发布

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条回答
爷、活的狠高调
2楼-- · 2019-07-10 20:17

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.

查看更多
登录 后发表回答