php - WP custom post type cant already add to cart in woocommerce 3.0 above -
i can still add custom post type cart in woocommerce 2.6 adding filter '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 echo carti item id echo wc()->cart->add_to_cart($custom_post_type_id, $quantity, null, null, array());
unfortunately doesn't work on latest version of woocommerce. please me solution issue? suggestions, comments, solutions appreciated.
i'm selling plugin enable use custom post type "product" of woocommerce. did lot of work on that.
but important part.
have create own data store, this:
first create class extends wc_product_data_store_cpt
. idea overwrite existing function of class check post type. found read
, get_product_type
checking.
class wccpt_product_data_store_cpt extends wc_product_data_store_cpt { /** * method read product 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 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 ); } /** * 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 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 filter woocommerce_data_stores
, use class.
add_filter( 'woocommerce_data_stores', 'woocommerce_data_stores' ); function woocommerce_data_stores ( $stores ) { $stores['product'] = 'wccpt_product_data_store_cpt'; return $stores; }
with that, you'll able add post type of birds
cart. not success add cart. because there's no price, cart reject it.
to solve that, need filter. below simple way of adding 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 that's done, you'll have success adding cart.
Comments
Post a Comment