Php 获取Woocommerce中自定义帖子类型的自定义字段价格值

Php 获取Woocommerce中自定义帖子类型的自定义字段价格值,php,wordpress,woocommerce,custom-post-type,price,Php,Wordpress,Woocommerce,Custom Post Type,Price,我使用下面的代码使用custom post类型的自定义字段作为价格字段。这样我就可以在购物车中添加此字段值并付款 好消息是,此自定义帖子已成功进入购物车,但问题是价格值错误 价格应该是400美元,但显示的是51376美元 代码如下: if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) { // Put your plu

我使用下面的代码使用custom post类型的自定义字段作为价格字段。这样我就可以在购物车中添加此字段值并付款

好消息是,此自定义帖子已成功进入购物车,但问题是价格值错误

价格应该是400美元,但显示的是51376美元

代码如下:

if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
// Put your plugin code here

add_action('woocommerce_loaded' , function (){
    //Put your code here that needs any woocommerce class
    //You can also Instantiate your main plugin file here
    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('refered_customer', '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('refered_customer', 'product'))) { // change birds with your post type
                return false;
            } else {
                return false;
            }
        }
    }
});

}

add_filter( 'woocommerce_data_stores', 'woocommerce_data_stores' );

function woocommerce_data_stores ( $stores ) {

$stores['product'] = 'WCCPT_Product_Data_Store_CPT';

return $stores;

}

add_filter('woocommerce_product_get_price', 'woocommerce_product_get_price', 10, 2 );

function woocommerce_product_get_price( $invoice_price, $product ) {

if ($post->post->post_type === 'refered_customer') // change birds with your post type

    $invoice_price = get_post_meta($post->id, "invoice_price", true);

return $invoice_price;

}
if(在_数组('woocommerce/woocommerce.php',应用_过滤器('active_plugins',获取_选项('active_plugins'))){
//把你的插件代码放在这里
添加操作('woocommerce'u loaded',函数(){
//把你的代码放在这里,需要任何商业类
//您还可以在此处实例化主插件文件
类WCCPT_产品_数据_存储_CPT扩展了WC_产品_数据_存储_CPT
{
/**
*方法从数据库中读取产品。
*@param WC_产品
*/
公共函数读取(&$product)
{
$product->set_defaults();
如果(!$product->get_id()| |!($post_对象=get_post($product->get_id())| |!在_数组中($post_对象->post_类型,数组('refered_customer',product')){//使用您的帖子类型更改小鸟
抛出新异常(uuuuu('Invalid product.','woocommerce');
}
$id=$product->get_id();
$product->set_道具(阵列)(
“名称”=>$post\u对象->post\u标题,
'slug'=>$post\u对象->post\u名称,
“创建日期”=>0<$post\u object->post\u date\u gmt?wc\u string\u to\u timestamp($post\u object->post\u date\u gmt):空,
'date\u modified'=>0<$post\u object->post\u modified\u gmt?wc\u string\u to\u timestamp($post\u object->post\u modified\u gmt):空,
“状态”=>$post\U对象->post\U状态,
'description'=>$post\u对象->post\u内容,
“简短描述”=>$post\U对象->post\U摘录,
'父对象id'=>$post\U对象->post\U父对象,
“菜单顺序”=>$post\U对象->菜单顺序,
'允许评论'=>'打开'==$post\u对象->评论\u状态,
));
$this->read_属性($product);
$this->read_下载($product);
$this->read_可见性($product);
$this->read_product_数据($product);
$this->read_extra_data($product);
$product->set\u object\u read(真);
}
/**
*根据产品ID获取产品类型。
*
*@自3.0.0以来
*@param int$product\u id
*@return bool | string
*/
公共函数获取产品类型($product\U id)
{
$post\u type=get\u post\u type($product\u id);
if('product\U VARIANCE'==$post\U type){
返回“变化”;
}elseif(在数组中($post\u type,数组('refered\u customer','product')){//使用您的post类型更改birds
返回false;
}否则{
返回false;
}
}
}
});
}
添加_过滤器(“woocommerce_数据_存储”、“woocommerce_数据_存储”);
功能商业数据存储($stores){
$stores['product']='WCCPT_产品_数据_商店_CPT';
退回$10的店铺;
}
添加过滤器('woocommerce\u product\u get\u price','woocommerce\u product\u get\u price',10,2);
功能:商业商品获取价格($invoice\u price,$product){
如果($post->post->post_type==='refered_customer')//用您的帖子类型更改bird
$invoice\u price=get\u post\u meta($post->id,“invoice\u price”,true);
返回$invoice\u价格;
}

已更新-在上一次获得正确价格的钩住函数中,出现了一些错误:

  • $post->post->post\u type
    将不起作用,因为未定义
    $post
    对象
  • $post->id
    应该是
    $post->id
    但它将不起作用,因为没有定义
    $post
    对象
相反,您将使用专用函数和方法:和

所以试试这个:

add_filter('woocommerce_product_get_price', 'woocommerce_product_get_price', 10, 2 );
function woocommerce_product_get_price( $price, $product ) {
    // Change birds with your post type
    if ( get_post_type( $product->get_id() ) === 'refered_customer' )
        $price = get_post_meta( $product->get_id(), "invoice_price", true );

    return $price;
}

代码进入活动子主题(或活动主题)的function.php文件。现在应该可以用了。

LoicTheAztec非常感谢您为我们指明了正确的方向。但是获取函数的作用是:返回购物车页面ID而不是自定义帖子类型。你知道为什么会发生这种情况,以及如何在购物车页面上获取自定义帖子类型id…是的,它现在正在工作。我当然会接受这个答案。当之无愧非常感谢您提供的有用提示…LoicTheAztec!还有一件事。。。函数get_post_type()返回“page”。你能帮助我如何在购物车页面上获取自定义帖子类型“refered\u customer”…@Gurjit再次更新为
get\u post\u type($product->get\u id())
…现在应该可以正常工作了。