2

I'm writing a plugin for WordPress which has an e-commerce element to it, and would like to leverage WooCommerce's ordering and online payment features for it.

However, from what I can see, if I want to add anything from my plugin into a WooCommerce cart (and then have an order generated from it) then there needs to be a product ID created in WooCommerce for me to pass to the WC()->add_to_cart() function.

If this is the case, it's not the end of the world - I'd just create a product in WooCommerce that acts as a "catch all" for everything my plugin adds to the cart, and use custom meta data on the cart items and order items to deal with the finer points of it.

But, it would be great if I could avoid this.

Is it possible?

gazchap
  • 23
  • 6
  • No… WC_Cart add_to_cart() method only works with existing products… – LoicTheAztec Aug 06 '18 at 10:48
  • I did this a few years back with a product that was disabled. I was able to add it to the cart. Before the cart loaded I added the required data to the product. – Gertjan Brouwer Aug 06 '18 at 12:12
  • Obviously you can't add something to the cart that doesn't exist, but you can completely hide products from the catalogue and search functions, so you can make products that can't be viewed by the end user unless they know the URL. – Peter HvD Aug 06 '18 at 15:08
  • You could create your own product type. WooCommerce already has several Simple, Variable, ... . Of course instances of this product type must support the product API. But you would need to provide that info (e.g. price, tax class, ...) anyway to create an order. Using the product API means that WooCommerce will do much of the work for you. –  Aug 07 '18 at 01:56
  • Conceptually, a product is just a progenitor for creating a line item in a cart or order. WooCommerce has two other progenitor for creating line items - shipping and tax. I suppose you could create your own line item progenitor but I am not sure if the WooCommerce core can be made to understand a new line item progenitor. That could be a useful capability. When I have some time I will investigate this further. –  Aug 07 '18 at 02:11
  • @user8262086 - I'll have a look into it myself too, thanks for pointing me in that direction. – gazchap Aug 07 '18 at 13:37

0 Answers0