WP自定义帖子类型无法在woocommerce

WP自定义帖子类型无法在woocommerce

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

问题描述

我仍然可以通过在"woocommerce_product_class"中添加过滤器,将自定义帖子类型添加到WooCommerce 2.6中的购物车中.

I can still add custom post type to cart in WooCommerce 2.6 just by 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());

不幸的是,这不适用于最新版本的WooCommerce.有人可以帮我解决这个问题的方法是什么?任何建议,评论和解决方案都非常感谢.

Unfortunately this doesn't already work on the latest version of WooCommerce. Would somebody please help me what is the solution for this issue? Any suggestions, comments, solutions are much appreciated.

推荐答案

我正在销售一个插件,该插件可以将自定义帖子类型用作WooCommerce的产品" .我做了很多工作.

但这是最重要的部分.
您必须创建自己的数据存储,如下所示:

But this is the most important part.
You have to create your own data store, like this:

首先创建一个扩展到WC_Product_Data_Store_CPT的类.这个想法是要覆盖此类检查帖子类型的现有功能.我发现进行检查的readget_product_type.

first create a class that extends to WC_Product_Data_Store_CPT. The idea is to overwrite the existing function of this class that check the post type. I found read and get_product_type that does the checking.

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

然后,将过滤器添加到woocommerce_data_stores并使用您的课程.

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

这样,您就可以在购物车中添加birds的帖子类型.但是实际上并不会成功地添加到购物车中.因为没有价格,购物车会拒绝它.

with that, you'll be able to add a post type of birds to cart. But will not actually be a success add to cart. Because there's no price, cart will reject it.

要解决此问题,您需要另一个过滤器.以下是增加价格的简单方法.

To solve that, you need another filter. Below is a simple way of adding the 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 to cart.

这篇关于WP自定义帖子类型无法在woocommerce 3.0以上版本中添加到购物车的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 14:05