2

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 ?

  • require_once doesn't work? Inlucde the php file and use the class you need. Only an idea, I am not a woocommerce developer :) Nice weekend. – cgee Aug 14 '15 at 14:17
  • It worked for me. But had to include require many files. Just want to follow the best practices. Anyways, thankyou :). – Gaurang Deshpande Aug 15 '15 at 07:59

1 Answers1

3

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.

Anand Shah
  • 14,575
  • 16
  • 72
  • 110