我有一个个人主题“A”,我希望它也可以在没有 Woocommerce 的情况下工作。添加Woocommerce“WC”插件后,我会将 A 产品与 WC 集成。我有一个名为“对象”的自定义帖子类型,我怎样才能使“对象”可以通过 WC 购买?
我已经在 StackOverflow 上看到了这个答案,将自定义帖子类型添加到 Woocommerce 最终解决方案提供了一个免费的(不再是)插件来解决。
我宁愿自己做这件事,没有插件的帮助。我很好奇,预包装解决方案不是我想要的。
我有一个个人主题“A”,我希望它也可以在没有 Woocommerce 的情况下工作。添加Woocommerce“WC”插件后,我会将 A 产品与 WC 集成。我有一个名为“对象”的自定义帖子类型,我怎样才能使“对象”可以通过 WC 购买?
我已经在 StackOverflow 上看到了这个答案,将自定义帖子类型添加到 Woocommerce 最终解决方案提供了一个免费的(不再是)插件来解决。
我宁愿自己做这件事,没有插件的帮助。我很好奇,预包装解决方案不是我想要的。
我创建了一个简单的教程,在这里添加它会太长。
为了实现您的目标,您的帖子类型必须有一个价格。也就是说,价格的自定义字段应该有一个 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=1
1 是您帖子类型中帖子的 ID。
访问该链接后的示例结果图像:
现在,您必须设置如下表格..
<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>
你需要有这个才能获得“添加到购物车”按钮。输入的名称必须保持原样。
我们可以尝试一个简单的事情..您想要一个自定义帖子类型来存储您的产品,并且当 WooCommerce 集成时,您的自定义帖子类型中添加的产品可以用作 woocommerce 产品..
检查是否未安装 woocommerce。
如果没有,则创建一个自定义帖子类型作为“产品”(类似于 woocommerce)。
要检查 woocommerce 是否处于活动状态,请使用此代码
<?php
if ( class_exists( 'WooCommerce' ) ) {
// code that requires WooCommerce
} else {
// you don't appear to have WooCommerce activated
}
?>
我正在销售一个插件,可以将自定义帖子类型用作 WooCommerce 的“产品”。我做了很多工作以使其与 WooCommerce 3.0 兼容
但这是最重要的部分。
您必须创建自己的数据存储,如下所示:
首先创建一个扩展至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;
}
完成后,您将成功添加到购物车。
我面临着同样的问题。花了几天后,我弄清楚了它应该如何工作及其解决方案。
这是我所做的
_price
为我的自定义帖子改变了if
class 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 ){