添加自定义 post 类型 / post 到 Woocommerce

Adding custom post type / post to Woocommerce

我有一个 个人主题 "A" 我希望它也可以在没有 Woocommerce 的情况下工作。 添加 Woocommerce "WC" 插件后,我会将 A 产品与 WC 集成。我有一个名为 "objects" 的自定义 post 类型,我怎样才能让 "object" 通过 WC 购买?

我已经在 Whosebug 上看到这个答案 Adding Custom Post Types to Woocommerce 解决方案最后给出了一个免费的(不再是)插件来解决。

我更愿意自己做这件事,没有插件的帮助。我很好奇,预打包解决方案不是我要找的。

我有 created a simple tutorial 在这里添加它会太长。

要实现您的目标,您的 post 类型必须有价格。也就是说,价格的自定义字段应该有一个元键 _price.

如果您已经有一个不是 _price 的元键,您可以向 woocommerce_get_price 添加过滤器,如下所示。

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); // assuming your price meta key is price
    return $price;
}

现在,您可以将 post 类型中的任何 post 添加到购物车。像 http://localhost/wordpress/cart/?add-to-cart=1 那样做,其中 1 是 post 在你的 post 类型中的 ID。

访问 link:

后的示例结果图像

现在,您必须设置如下所示的表格..

<form action="" method="post">
    <input name="add-to-cart" type="hidden" value="<?php echo $post->ID ?>" />
    <input name="quantity" type="number" value="1" min="1"  />
    <input name="submit" type="submit" value="Add to cart" />
</form>

你需要这个才能获得 "Add to Cart" 按钮。输入的名称必须保持原样。

我们可以尝试一件简单的事情..您想要一个自定义 post 类型来存储您的产品,当集成 WooCommerce 时,您的自定义 post 类型中添加的产品可以用作 woocommerce 产品。 .

  1. 检查是否安装了 woocommerce。

  2. 如果没有,则创建自定义 post 类型为 'product'(类似于 woocommerce)。

  3. 添加类似于 Woocommerce 的自定义字段,并在其下添加产品。 (为 Woocommerce 的自定义字段添加的图片)
  4. 现在,在自定义 post 类型下添加产品,添加 woocommerce 时将面临自定义 post 类型已存在的问题。
  5. 因此,如果将来您想将 woocommerce 添加到您的站点,请禁用 register_post_Type 功能并安装 WooCommerce。
  6. 在自定义 post 类型中创建的所有产品也将在 Woocommerce 产品选项中可见

要检查 woocommerce 是否处于活动状态,请使用此代码

<?php
    if ( class_exists( 'WooCommerce' ) ) {
      // code that requires WooCommerce
    } else {
      // you don't appear to have WooCommerce activated
    }
    ?>

我遇到了同样的问题。花了几天时间,我弄清楚了它应该如何工作以及它的解决方案。

这是我所做的

  1. 为我的自定义 post
  2. 保留价格元 _price
  3. 在 class woocommerce/includes/class-wc-product-factory.php 的函数 get_product_class 中更改了 if 条件,如下所示:

    之前

    if ( 'product' === $post_type){
    

    之后
    if ( 'product' === $post_type || 'packages' === $post_type ){   
    

效果很好。

现在我想使用某种过滤方法更改此产品类型。谁能告诉我一下:

if ( 'product' === $post_type || 'packages' === $post_type ){   

I'm selling a plugin that would enable to use Custom Post Type as "product" of WooCommerce。我为此做了很多工作以使其与 WooCommerce 3.0

兼容

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

首先创建一个扩展到 WC_Product_Data_Store_CPT 的 class。这个想法是覆盖这个class检查post类型的现有功能。我发现 readget_product_type 进行检查。

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 并使用您的 class。

add_filter( 'woocommerce_data_stores', 'woocommerce_data_stores' );

function woocommerce_data_stores ( $stores ) {      
    $stores['product'] = 'WCCPT_Product_Data_Store_CPT';
    return $stores;
}

这样,您就可以将 post 类型的 birds 添加到购物车。但实际上不会成功加入购物车。因为没有价格,购物车会拒绝它。

要解决这个问题,您需要另一个过滤器。下面是添加价格的简单方法。

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

完成后,您将成功添加到购物车。