woocommerce将自定义帖子类型添加到购物车

问题描述:

我正在使用woocommerce,并且我创建了必须视为产品并且用户可以添加到购物车的自定义帖子类型。我按照这个教程 http://reigelgallarde.me/programming/how-to-add-custom-post-type-to-woocommerce/ 并确保我的价格字段元键是“_price” ,但它没有奏效。woocommerce将自定义帖子类型添加到购物车

,当我尝试添加该代码添加到functions.php

function reigel_woocommerce_get_price($price = null,$post = null){ 
if ($post->post->post_type === 'aytam'){ 
    $price = get_post_meta($post->id, "_price", true); 
    } 

return $price; 
} 
add_filter('woocommerce_get_price','reigel_woocommerce_get_price',20,1); 
add_action('init', 'reigel_woocommerce_get_price'); 

没有工作,要么

如果你已经有了一个Meta键是not _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); // your price meta key is price 
     return $price; 
    } 
+0

对我不起作用,我改用了自定义产品类型。但感谢您的帮助:) –

+0

没有为我工作,因为在最新的版本3.0。*有很多东西改变。仍在寻找解决方案。 –

在最新版本3.0 * Woocommcerce用于硬检查$post_type === 'product'你可以重写它

class My_Product_Data_Store_CPT extends WC_Product_Data_Store_CPT implements WC_Object_Data_Store_Interface, WC_Product_Data_Store_Interface { 


    /** 
    * 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())) || 'product' !== $post_object->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); 
    } 


} 

然后包括通过挂钩此文件;

add_filter('woocommerce_data_stores', 'my_woocommerce_data_stores');

function my_woocommerce_data_stores($stores) { 

    require_once PLUGIN_PATH . '/includes/classes/class-data-store-cpt.php'; 
    $stores['product'] = 'MY_Product_Data_Store_CPT'; 

    return $stores; 
} 

使用此过滤器,以提供从自定义元的价格;

add_filter('woocommerce_product_get_price', 'my_woocommerce_product_get_price', 10, 2); 
function my_woocommerce_product_get_price($price, $product) { 

    if ($product->get_id() == 815) { 
     $price = 10;   
    } 
    return $price; 
} 

现在,如果你尝试使用网址PARAM add-to-cart=[POST_ID]http://localhost/wordpress/cart/?add-to-cart=244将项目添加到购物车添加到购物车。

您也可以使用按钮添加到购物车。

 <form action="" method="post"> 
      <input name="add-to-cart" type="hidden" value="<?php the_ID(); ?>" /> 
      <input name="quantity" type="number" value="1" min="1" /> 
      <input name="submit" type="submit" value="Add to cart" /> 
     </form>