I have a personal theme "A" and I want that it can also work without Woocommerce. When Woocommerce "WC" plugin is added I would integrate A products with WC. I have a custom post type called "objects", how can I make "object" buyable throught WC?
I've alredy seen this answer on StackOverflow Adding Custom Post Types to Woocommerce Where the solution in the end gives a free (not anymore) plugin to resolve.
I'd prefer to do this thing on my own, without helps of plugin. I'm curious and a pre-package solution isn't what i'm looking for.
I have created a simple tutorial as to adding it here would be too long.
To achieved your goal, your post type must have a price. That said, the custom field for price should have a meta key
_price
.if you already have a meta key which is not
_price
, you can add a filter towoocommerce_get_price
shown below.With this, you can now add any post in your post type to the cart. Do so like
http://localhost/wordpress/cart/?add-to-cart=1
where 1 is the ID of your post in your post type.sample result image after visiting that link:
Now, you have to setup a form like below..
you need to have this to get "Add to Cart" button. The name of the inputs are required to be as is.
I was facing the same issue. After spending days I figured out how it should work and its solution.
Here what I did
_price
for my custom postChanged the
if
condition in classwoocommerce/includes/class-wc-product-factory.php
's functionget_product_class
like this:before
after
It worked perfectly.
Now I want to change this product type using some filter method. Can anybody let me know about it:
We can try a easy thing .. you want a custom post type to store your products and when WooCommerce is integrated the added products in your custom post type can work as woocommerce products ..
Check if woocommerce is installed ot not.
If not, then Create a custom post type as 'product' (similar to woocommerce).
To check whether woocommerce is active or not use this code
I'm selling a plugin that would enable to use Custom Post Type as "product" of WooCommerce. I did a lot of work on that to make it compatible with WooCommerce 3.0
But here is the most important part.
You have to create your own data store, like this:
first create a class that extends to
WC_Product_Data_Store_CPT
. The idea is to overwrite the existing function of this class that check the post type. I foundread
andget_product_type
that does the checking.after that, add a filter to
woocommerce_data_stores
and use your class.with that, you'll be able to add a post type of
birds
to cart. But will not actually be a success add to cart. Because there's no price, cart will reject it.To solve that, you need another filter. Below is a simple way of adding the price.
Once that's done, you'll have success adding to cart.