我仍然可以通过在' woocommerce_product_class'
中添加过滤器,在WooCommerce 2.6中将自定义帖子类型添加到购物车from bs4 import BeautifulSoup
file_names = ['bbb.html', 'ccc.html', ... , 'yyy.html']
# we exclude first and last files (not sure what to do with them ?)
for ind, file_name in enumerate(file_names):
with open(file_name, 'r+') as f:
soup = BeautifulSoup(f.read(), 'html.parser')
# we suppose that there is only one link for previous and next
soup.find_all(class_='previous')[0]['href'] = file_names[ind - 1]
soup.find_all(class_='next')[0]['href'] = file_names[ind + 1]
# erase contents and replace with new html
f.seek(0)
f.truncate()
f.write(soup.prettify("utf-8")) # to get readable HTML
不幸的是,这还没有在最新版本的WooCommerce上运行。有人请帮我解决这个问题的解决方案吗?任何建议,评论和解决方案都非常感谢。
答案 0 :(得分:6)
I'm selling a plugin that would enable to use Custom Post Type as "product" of WooCommerce。我做了很多工作。
但这是最重要的部分 您必须创建自己的数据存储,如下所示:
首先创建一个扩展到WC_Product_Data_Store_CPT
的类。这个想法是覆盖检查帖子类型的这个类的现有函数。我找到了read
和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
的帖子类型添加到购物车。但实际上并不会成功添加到购物车。因为没有价格,购物车会拒绝它。
要解决此问题,您需要另一个过滤器。以下是添加价格的简单方法。
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;
}
完成后,您就可以成功添加到购物车中。