WP 自定义帖子类型无法在上述 woocommerce 3.0 中添加到购物车

2023-12-04

我仍然可以通过向“woocommerce_product_class”添加过滤器来将自定义帖子类型添加到 WooCommerce 2.6 中的购物车

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。有人可以帮我解决这个问题吗?非常感谢任何建议、意见、解决方案。


我正在销售一个插件,可以使用自定义帖子类型作为 WooCommerce 的“产品”。我在这方面做了很多工作。

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

首先创建一个扩展至的类WC_Product_Data_Store_CPT。这个想法是覆盖此类检查帖子类型的现有函数。我发现read and get_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 Custom Post Type

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

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 Custom Post Type

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

WP 自定义帖子类型无法在上述 woocommerce 3.0 中添加到购物车 的相关文章

  • 使用 PHP 删除文件 onclick [关闭]

    很难说出这里问的是什么 这个问题是含糊的 模糊的 不完整的 过于宽泛的或修辞性的 无法以目前的形式得到合理的回答 如需帮助澄清此问题以便重新打开 访问帮助中心 help reopen questions 我想在用户单击删除链接时删除文件 但
  • 如何将 php curl 中的 cookie 获取到变量中

    因此 其他公司的一些人认为 如果不使用soap xml rpc rest 或任何其他合理的通信协议 而是将所有响应作为cookie 嵌入标头中 那就太棒了 我需要从这个卷曲响应中将这些 cookie 作为数组取出 如果我不得不为此浪费大量的
  • 如何使用 Laravel Eloquent 创建多个Where子句查询?

    我正在使用 Laravel Eloquent 查询构建器 并且我有一个查询 我想要一个WHERE多个条件的子句 它可以工作 但并不优雅 Example results User where this 1 gt where that 1 gt
  • laravel 中的 jwt 中的“无法从请求中解析令牌”

    我面临着 无法从请求中解析令牌 Laravel 中的 JWT 错误 我在 localhost Windows 7 中的 Xampp 中尝试了相同的代码 它正在工作 但在服务器上它不起作用 我已经通过了 授权 标头中的令牌也发生了变化 hta
  • 从前端更改记录顺序

    我在编写下一个功能时遇到问题 我希望用户能够重新排列记录并更改 display order 值 我使用 Jquery UI 的可拖放功能来促进这一点 我可以看到如何简单地交换 display order 值 但我想为一条记录设置一个显示顺序
  • PHP CSV VLookup

    我正在寻找一个 PHP 函数 它可以读取 CSV 文件并在第 1 列上执行 vlookup 以回显第 2 列中同一行的相关值 例如 如果 CSV 包含 Name Email John j email protected cdn cgi l
  • 使用哪个正则表达式将此字符串转换为数组?

    从 mysql 中的地理空间列我得到以下字符串值 我想将其转换为数组 最终目标是将其转换为 geoJSON POLYGON 4 885838 52 388063 4 891061 52 388381 4 890973 52 382909 该
  • 如何在wordpress中提交表单

    我正在使用 WordPress 3 3 1twentyten theme 我创建了一个插件来创建自定义表单 我已经在 WordPress 中成功安装了这个 我的插件文件代码如下
  • 检查用户是否连接到 Facebook,然后检查他是否喜欢某个页面

    有没有什么方法可以检查用户是否在我的外部页面上连接到 Facebook 而不让他们允许我的应用程序之一 同样的问题也适用于 检查用户是否喜欢某个页面 我检查了大约 20 个问题和 3 4 个教程 似乎所有问题都在讨论内部脚本 粉丝页面 应用
  • 类别树的路由

    我正在使用Tree http www gediminasm org article tree nestedset behavior extension for doctrine 2类别树的学说扩展并希望有如下路线 cat subcat1 s
  • 在 TCPDF 中设置背景颜色

    我已经手动设置了第一页的背景颜色 如下所示 pdf gt AddPage pdf gt SetFillColor 52 21 0 76 pdf gt Rect 0 0 pdf gt getPageWidth pdf gt getPageHe
  • 带有列标题的php数组到csv的转换

    我想将数组转换为 csv 我能够将关联数组转换为 csv 但无法获取标题 我想要动态地将数字类型日期作为标题 下面是我转换的数组 Array 0 gt Array NUMBER gt 67 TYPE gt Other DATE gt 3 3
  • PHP 继承以及静态方法和属性

    PHP 中的静态属性和方法不能被继承吗 一些例子会有所帮助 不 那不是真的 静态方法和属性 http www php net manual en language oop5 static php将会得到遗传 http www php net
  • PHP PCRE 函数中的 $0 是什么

    我读过一个文档preg filter功能如下 这是来自 php net http php net manual en function preg filter php site subject array 1 a 2 b 3 A B 4 p
  • Yii2 Rest - 自定义操作和 OPTIONS 方法

    我在 UsersController 类中执行以下操作 login 路由操作时遇到问题 public function actionLogin data Yii app gt getRequest gt getBodyParams mode
  • 显示产品中的类别名称和类别 ID - Laravel

    我已经找到了这个答案 但它对我不起作用 Laravel 按 id 显示类别 https stackoverflow com questions 39222584 laravel displaying categories by id 我无法
  • 是否可以在 php.ini 中指示 PHP 使用 postfix 配置?

    是否可以配置 PHP 使用 postfix 的配置集发送电子邮件 WordPress 通过 PHPMailer 依赖于 php ini 中的这些设置是否正确 SMTP localhost http php net smtp port smt
  • 依赖注入容器什么时候会变得太大,我该怎么办?

    我们都知道为什么依赖注入很棒因为它使代码耦合更少 更容易测试 并且更容易阅读 然后有些人决定使用依赖注入容器 like pimple http pimple sensiolabs org PHP 可以协助依赖倒置 http en wikip
  • 带结束标记和不带结束标记的 XML(自结束标记)

    如何区别
  • 在网络托管上发布后,php 会话无法正常工作

    我的网站在本地主机上运行良好 但是一旦我将其部署到我的托管服务 会话就会停止工作

随机推荐