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

问题描述 投票:0回答:1

我使用以下代码来使用自定义帖子类型的自定义字段作为价格字段。这样我就可以在购物车中添加该字段值并进行付款。

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

价格应为 400 美元,但显示为 51,376 美元。

这是代码:

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;

}
php wordpress woocommerce custom-post-type product-price
1个回答
1
投票

更新 - 在您最后一个挂钩函数中获得正确的价格时,您遇到了一些错误:

  • $post->post->post_type
    无法工作,因为
    $post
    对象未定义
  • $post->id
    应该是
    $post->ID
    但是它不会工作,因为
    $post
    对象未定义

相反,您将使用专用函数和方法:

get_post_type()
$product->get_id()

所以试试这个:

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 文件中。现在应该可以工作了。

© www.soinside.com 2019 - 2024. All rights reserved.