10

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

我已经在 StackOverflow 上看到了这个答案,将自定义帖子类型添加到 Woocommerce 最终解决方案提供了一个免费的(不再是)插件来解决。

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

4

4 回答 4

14

创建了一个简单的教程,在这里添加它会太长。

为了实现您的目标,您的帖子类型必须有一个价格。也就是说,价格的自定义字段应该有一个 meta key _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;
}

有了这个,您现在可以将帖子类型中的任何帖子添加到购物车。这样做就像http://localhost/wordpress/cart/?add-to-cart=11 是您帖子类型中帖子的 ID。

访问该链接后的示例结果图像:

reigelgallarde.me

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

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

你需要有这个才能获得“添加到购物车”按钮。输入的名称必须保持原样。

于 2016-02-10T06:36:27.673 回答
3

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

  1. 检查是否未安装 woocommerce。

  2. 如果没有,则创建一个自定义帖子类型作为“产品”(类似于 woocommerce)。

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

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

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

在此处输入图像描述

于 2016-02-15T12:24:39.400 回答
3

我正在销售一个插件,可以将自定义帖子类型用作 WooCommerce 的“产品”。我做了很多工作以使其与 WooCommerce 3.0 兼容

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

首先创建一个扩展至WC_Product_Data_Store_CPT. 这个想法是覆盖这个检查帖子类型的现有函数。我发现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并使用你的类。

add_filter( 'woocommerce_data_stores', 'woocommerce_data_stores' );

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

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

WooCommerce 自定义帖子类型

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

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

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

WooCommerce 自定义帖子类型

于 2017-04-22T12:07:32.517 回答
0

我面临着同样的问题。花了几天后,我弄清楚了它应该如何工作及其解决方案。

这是我所做的

  1. 将价格元数据保留_price为我的自定义帖子
  2. 改变了ifclass woocommerce/includes/class-wc-product-factory.php函数中的条件,get_product_class如下所示:

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

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

它工作得很好。

现在我想使用一些过滤方法来改变这个产品类型。任何人都可以让我知道它:

if ( 'product' === $post_type || 'packages' === $post_type ){   
于 2017-04-21T18:05:51.633 回答