woocommerce 将自定义帖子类型添加到购物车 [英] woocommerce add custom post type to cart

查看:32
本文介绍了woocommerce 将自定义帖子类型添加到购物车的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 woocommerce,我创建了一个自定义帖子类型,必须将其视为产品,用户可以添加到购物车.我跟着这个教程http://reigelgallarde.me/programming/how-to-add-custom-post-type-to-woocommerce/并确保我的价格字段元键是_price"但它没有用.

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/programming/how-to-add-custom-post-type-to-woocommerce/ and made sure my price field meta key is "_price" but it didn't work.

当我尝试将此代码添加到functions.php时

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' );

也没有用

推荐答案

一直在解决这个问题,由于上面的代码不起作用,我继续制作了一个工作版本.请注意,此代码适用于 Woocommerce 3.6.2(至少),旨在使其适用于自定义帖子类型 industry-ad ,但您可以更改为欲望

been tackling with this and since the above code didn't work, I went ahead and made a working version. Note that this code works with Woocommerce 3.6.2 (at least) and is intended to make it work with custom post type industry-ad , but you can change to what you desire

class IA_Woo_Product extends WC_Product  {

    protected $post_type = 'industry-ad';

    public function get_type() {
        return 'industry-ad';
    }

    public function __construct( $product = 0 ) {
        $this->supports[]   = 'ajax_add_to_cart';

        parent::__construct( $product );


    }
    // maybe overwrite other functions from WC_Product

}

class IA_Data_Store_CPT extends WC_Product_Data_Store_CPT {

    public function read( &$product ) { // this is required
        $product->set_defaults();
        $post_object = get_post( $product->get_id() );

        if ( ! $product->get_id() || ! $post_object || 'industry-ad' !== $post_object->post_type ) {

            throw new Exception( __( 'Invalid product.', 'woocommerce' ) );
        }

        $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 );
    }

    // maybe overwrite other functions from WC_Product_Data_Store_CPT

}


class IA_WC_Order_Item_Product extends WC_Order_Item_Product {
    public function set_product_id( $value ) {
        if ( $value > 0 && 'industry-ad' !== get_post_type( absint( $value ) ) ) {
            $this->error( 'order_item_product_invalid_product_id', __( 'Invalid product ID', 'woocommerce' ) );
        }
        $this->set_prop( 'product_id', absint( $value ) );
    }

}




function IA_woocommerce_data_stores( $stores ) {
    // the search is made for product-$post_type so note the required 'product-' in key name
    $stores['product-industry-ad'] = 'IA_Data_Store_CPT';
    return $stores;
}
add_filter( 'woocommerce_data_stores', 'IA_woocommerce_data_stores' , 11, 1 );


function IA_woo_product_class( $class_name ,  $product_type ,  $product_id ) {
    if ($product_type == 'industry-ad')
        $class_name = 'IA_Woo_Product';
    return $class_name; 
}
add_filter('woocommerce_product_class','IA_woo_product_class',25,3 );



function my_woocommerce_product_get_price( $price, $product ) {

    if ($product->get_type() == 'industry-ad' ) {
        $price = 10;  // or get price how ever you see fit     
    }
    return $price;
}
add_filter('woocommerce_get_price','my_woocommerce_product_get_price',20,2);
add_filter('woocommerce_product_get_price', 'my_woocommerce_product_get_price', 10, 2 );



// required function for allowing posty_type to be added; maybe not the best but it works
function IA_woo_product_type($false,$product_id) { 
    if ($false === false) { // don't know why, but this is how woo does it
        global $post;
        // maybe redo it someday?!
        if (is_object($post) && !empty($post)) { // post is set
            if ($post->post_type == 'industry-ad' && $post->ID == $product_id) 
                return 'industry-ad';
            else {
                $product = get_post( $product_id );
                if (is_object($product) && !is_wp_error($product)) { // post not set but it's a industry-ad
                    if ($product->post_type == 'industry-ad') 
                        return 'industry-ad';
                } // end if 
            }    

        } else if(wp_doing_ajax()) { // has post set (usefull when adding using ajax)
            $product_post = get_post( $product_id );
            if ($product_post->post_type == 'industry-ad') 
                return 'industry-ad';
        } else { 
            $product = get_post( $product_id );
            if (is_object($product) && !is_wp_error($product)) { // post not set but it's a industry-ad
                if ($product->post_type == 'industry-ad') 
                    return 'industry-ad';
            } // end if 

        } // end if  // end if 



    } // end if 
    return false;
}
add_filter('woocommerce_product_type_query','IA_woo_product_type',12,2 );

function IA_woocommerce_checkout_create_order_line_item_object($item, $cart_item_key, $values, $order) {

    $product                    = $values['data'];
    if ($product->get_type() == 'industry-ad') {
        return new IA_WC_Order_Item_Product();
    } // end if 
    return $item ;
}   
add_filter( 'woocommerce_checkout_create_order_line_item_object', 'IA_woocommerce_checkout_create_order_line_item_object', 20, 4 );

function cod_woocommerce_checkout_create_order_line_item($item,$cart_item_key,$values,$order) {
    if ($values['data']->get_type() == 'industry-ad') {
        $item->update_meta_data( '_industry-ad', 'yes' ); // add a way to recognize custom post type in ordered items
        return;
    } // end if 

}
add_action( 'woocommerce_checkout_create_order_line_item', 'cod_woocommerce_checkout_create_order_line_item', 20, 4 );

function IA_woocommerce_get_order_item_classname($classname, $item_type, $id) {
    global $wpdb;
    $is_IA = $wpdb->get_var("SELECT meta_value FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE order_item_id = {$id} AND meta_key = '_industry-ad'");


    if ('yes' === $is_IA) { // load the new class if the item is our custom post
        $classname = 'IA_WC_Order_Item_Product';
    } // end if 
    return $classname;
}
add_filter( 'woocommerce_get_order_item_classname', 'IA_woocommerce_get_order_item_classname', 20, 3 );

这篇关于woocommerce 将自定义帖子类型添加到购物车的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆