WP custom post type can not already added to cart in woocommerce 3.0 above

I can add a custom post type to the cart in WooCommerce 2.6 by simply adding a filter to 'woocommerce_product_class'

function wc_product_class( $class, $product_type, $post_type ) { if( 'my_custom_post_type_slug' == $post_type ) $class = 'WC_Product_Simple'; return $class; } add_filter( 'woocommerce_product_class', 'wc_product_class', 10, 3); //This will echo the carti item id echo WC()->cart->add_to_cart($custom_post_type_id, $quantity, null, null, array()); 

Unfortunately, this does not work in the latest version of WooCommerce. Can someone please help me in solving this problem? Any suggestions, comments, decisions are greatly appreciated.

+3
php wordpress woocommerce
source share
1 answer

I am selling a plugin that will allow you to use Custom Post Type as a WooCommerce β€œproduct” . I worked a lot on this.

But this is the most important part.
You must create your own data warehouse, for example:

first create a class that extends to WC_Product_Data_Store_CPT . The idea is to overwrite an existing function of this class that checks the type of message. I found read and get_product_type , which does the check.

 class WCCPT_Product_Data_Store_CPT extends WC_Product_Data_Store_CPT { /** * Method to read a product from the database. * @param WC_Product */ public function read( &$product ) { $product->set_defaults(); if ( ! $product->get_id() || ! ( $post_object = get_post( $product->get_id() ) ) || ! in_array( $post_object->post_type, array( 'birds', 'product' ) ) ) { // change birds with your post type throw new Exception( __( 'Invalid product.', 'woocommerce' ) ); } $id = $product->get_id(); $product->set_props( array( 'name' => $post_object->post_title, 'slug' => $post_object->post_name, 'date_created' => 0 < $post_object->post_date_gmt ? wc_string_to_timestamp( $post_object->post_date_gmt ) : null, 'date_modified' => 0 < $post_object->post_modified_gmt ? wc_string_to_timestamp( $post_object->post_modified_gmt ) : null, 'status' => $post_object->post_status, 'description' => $post_object->post_content, 'short_description' => $post_object->post_excerpt, 'parent_id' => $post_object->post_parent, 'menu_order' => $post_object->menu_order, 'reviews_allowed' => 'open' === $post_object->comment_status, ) ); $this->read_attributes( $product ); $this->read_downloads( $product ); $this->read_visibility( $product ); $this->read_product_data( $product ); $this->read_extra_data( $product ); $product->set_object_read( true ); } /** * Get the product type based on product ID. * * @since 3.0.0 * @param int $product_id * @return bool|string */ public function get_product_type( $product_id ) { $post_type = get_post_type( $product_id ); if ( 'product_variation' === $post_type ) { return 'variation'; } elseif ( in_array( $post_type, array( 'birds', 'product' ) ) ) { // change birds with your post type $terms = get_the_terms( $product_id, 'product_type' ); return ! empty( $terms ) ? sanitize_title( current( $terms )->name ) : 'simple'; } else { return false; } } } 

after that add a filter to woocommerce_data_stores and use your class.

 add_filter( 'woocommerce_data_stores', 'woocommerce_data_stores' ); function woocommerce_data_stores ( $stores ) { $stores['product'] = 'WCCPT_Product_Data_Store_CPT'; return $stores; } 

with this you can add the message type of birds to the cart. But in reality it will not be successful to add to the basket. Because there is no price, the basket will reject it.

Custom Publishing Type WooCommerce

To solve this problem, you need one more filter. Below is an easy way to add a price.

 add_filter('woocommerce_product_get_price', 'woocommerce_product_get_price', 10, 2 ); function woocommerce_product_get_price( $price, $product ) { if ($product->get_id() == 815 ) { $price = 10; } return $price; } 

Once this is done, you will have a successful addition to the cart.

Custom Publishing Type WooCommerce

+6
source share

All Articles