Php WP自定义帖子类型无法添加到woocommerce 3.0以上版本中的购物车中
在WooCommerce 2.6中,我仍然可以通过在“WooCommerce\u product\u class”中添加一个过滤器将自定义帖子类型添加到购物车中Php WP自定义帖子类型无法添加到woocommerce 3.0以上版本中的购物车中,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,在WooCommerce 2.6中,我仍然可以通过在“WooCommerce\u product\u class”中添加一个过滤器将自定义帖子类型添加到购物车中 function wc_product_class( $class, $product_type, $post_type ) { if( 'my_custom_post_type_slug' == $post_type ) $class = 'WC_Product_Simple'; return $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上还不起作用。有人能帮我解决这个问题吗?非常感谢您的任何建议、意见和解决方案。。我在这方面做了很多工作
但这是最重要的部分。您必须创建自己的数据存储,如下所示: 首先创建一个扩展到
WC\u Product\u Data\u Store\u CPT
的类。这样做的目的是覆盖该类检查post类型的现有函数。我找到了执行检查的read
和get\u product\u 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;
}
}
}
这样,你就可以在购物车中添加一个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;
}
完成后,您将成功添加到购物车
非常感谢您!我真的很感谢你的帮助。而且我也看过你的插件,我印象深刻。干得好。@solomonculaste不客气。如果你觉得这是正确的和有帮助的,请将此标记为答案。嘿,reigel,我做了所有这些,但是帖子没有添加,我有woo coomerce 3.04installed@NirmalGoswami,那应该有用。。。但是您也可以将该类添加到这一行的上方
$stores['product']='WCCPT\u product\u Data\u Store\u CPT'代码>轻松。。。但是,更好的方法是创建一个类文件并要求它。。
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;
}