woocommerce add custom post type to cart

前端 未结 3 1898
猫巷女王i
猫巷女王i 2021-01-03 17:44

I\'m using woocommerce and i created a custom post type that has to be treated as a product and user may add to cart. I followed this tutorial http://reigelgallarde.me/prog

3条回答
  •  没有蜡笔的小新
    2021-01-03 17:48

    In latest version 3.0.* Woocommcerce used to hard check the $post_type === 'product' You can override it

    class My_Product_Data_Store_CPT extends WC_Product_Data_Store_CPT implements WC_Object_Data_Store_Interface, WC_Product_Data_Store_Interface {
    
    
        /**
         * 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() ) ) || 'product' !== $post_object->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 );
        }
    
    
    }
    

    Then include this file via hook;

    add_filter( 'woocommerce_data_stores', 'my_woocommerce_data_stores' );

    function my_woocommerce_data_stores( $stores ) {
    
        require_once PLUGIN_PATH . '/includes/classes/class-data-store-cpt.php';
        $stores['product'] = 'MY_Product_Data_Store_CPT';
    
        return $stores;
    }
    

    Use this filter to provide the price from custom meta;

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

    Now if you try adding to cart using url param add-to-cart=[POST_ID] like http://localhost/wordpress/cart/?add-to-cart=244 will add the item to cart.

    You can also use button to add to cart.

            

提交回复
热议问题