frame

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In Register

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

WooCommerce - How to setup custom post type instead of product

edited November 21 in WordPress

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 and made sure my price field meta key is “_price” but it didn't work.

when I tried to add this code to functions.php

function reigel_woocommerce_get_price($price = null,$post = null){
if ($post->post->post_type === 'aytam'){
    $price = get_post_meta($post->id, "_price", true);
    }

    return $price;
}
add_filter('woocommerce_get_price','reigel_woocommerce_get_price',20,1);
add_action( 'init', 'reigel_woocommerce_get_price' );

didn't work either

Comments

  • 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.

            <form action="" method="post">
                <input name="add-to-cart" type="hidden" value="<?php the_ID(); ?>" />
                <input name="quantity" type="number" value="1" min="1"  />
                <input name="submit" type="submit" value="Add to cart" />
            </form>
    
  • If you already have a meta key which is not _price, you can add a filter to woocommerce_get_price shown below.

      add_filter('woocommerce_get_price','reigel_woocommerce_get_price',20,2);
        function reigel_woocommerce_get_price($price,$post){
            if ($post->post->post_type === 'post') // change this to your post type
                $price = get_post_meta($post->id, "price", true); // your price meta key is price
            return $price;
        }
    
Sign In or Register to comment.

The Forum Hub

| make vanilla clean and light
@ 2018 The Forum Hub, All rights reserved.
Powered by VanillaForums, Designed by ThemeSteam

Contact us

admin@theforumhub.com
support@theforumhub.com
(800) 3032120

Get In Touch