【问题标题】:Using ACF number field as price for WooCommerce custom product type使用 ACF 编号字段作为 WooCommerce 自定义产品类型的价格
【发布时间】:2020-12-15 22:35:23
【问题描述】:

我在网上看到,只要 CPT 有价格字段,您就可以将所有自定义帖子类型添加到 WooCommerce 购物车。唯一的问题是您必须告诉 WooCommerce 哪个 CPT 字段包含价格。

您可以轻松地创建一个添加到购物车的网址,如下所示: https://yourdomain.com/?add-to-cart=XXX(XXX 是自定义帖子类型帖子 ID)

为什么这么方便?
我有一个在线菜单(只是为了告诉我的客人我们提供什么服务),但由于 Corona 我们不得不关门,所以我希望这些菜肴可以在线订购。

代码应该是这样的,但 CPT 没有添加到购物车:

add_filter('woocommerce_get_price', 'yl_get_dish_price', 20,2);
function yl_get_dish_price($price,$post) {
    if ($post->post->post_type === 'dish') {
        $price = get_field('price', $post->ID);
    }
    return $price;
}

更新

我从这里得到了这个答案https://stackoverflow.com/a/60320662/10291365

class YL_Dish_Product extends WC_Product  {

    protected $post_type = 'dish';

    public function get_type() {
        return 'dish';
    }

    public function __construct( $product = 0 ) {
        $this->supports[]   = 'ajax_add_to_cart';

        parent::__construct( $product );


    }
    // maybe overwrite other functions from WC_Product

}

class YL_Data_Store_CPT extends WC_Product_Data_Store_CPT {

    public function read( &$product ) { // this is required
        $product->set_defaults();
        $post_object = get_post( $product->get_id() );

        if ( ! $product->get_id() || ! $post_object || 'dish' !== $post_object->post_type ) {

            throw new Exception( __( 'Invalid product.', 'woocommerce' ) );
        }

        $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 );
    }

    // maybe overwrite other functions from WC_Product_Data_Store_CPT

}


class YL_WC_Order_Item_Product extends WC_Order_Item_Product {
    public function set_product_id( $value ) {
        if ( $value > 0 && 'dish' !== get_post_type( absint( $value ) ) ) {
            $this->error( 'order_item_product_invalid_product_id', __( 'Invalid product ID', 'woocommerce' ) );
        }
        $this->set_prop( 'product_id', absint( $value ) );
    }

}




function YL_woocommerce_data_stores( $stores ) {
    // the search is made for product-$post_type so note the required 'product-' in key name
    $stores['product-dish'] = 'YL_Data_Store_CPT';
    return $stores;
}
add_filter( 'woocommerce_data_stores', 'YL_woocommerce_data_stores' , 11, 1 );


function YL_woo_product_class( $class_name ,  $product_type ,  $product_id ) {
    if ($product_type == 'dish')
        $class_name = 'YL_Dish_Product';
    return $class_name; 
}
add_filter('woocommerce_product_class','YL_woo_product_class',25,3 );



function my_woocommerce_product_get_price( $price, $product ) {

    if ($product->get_type() == 'dish' ) {
        $price = 10;  // or get price how ever you see fit     
    }
    return $price;
}
add_filter('woocommerce_get_price','my_woocommerce_product_get_price',20,2);
add_filter('woocommerce_product_get_price', 'my_woocommerce_product_get_price', 10, 2 );



// required function for allowing posty_type to be added; maybe not the best but it works
function YL_woo_product_type($false,$product_id) { 
    if ($false === false) { // don't know why, but this is how woo does it
        global $post;
        // maybe redo it someday?!
        if (is_object($post) && !empty($post)) { // post is set
            if ($post->post_type == 'dish' && $post->ID == $product_id) 
                return 'dish';
            else {
                $product = get_post( $product_id );
                if (is_object($product) && !is_wp_error($product)) { // post not set but it's a dish
                    if ($product->post_type == 'dish') 
                        return 'dish';
                } // end if 
            }    

        } else if(wp_doing_ajax()) { // has post set (usefull when adding using ajax)
            $product_post = get_post( $product_id );
            if ($product_post->post_type == 'dish') 
                return 'dish';
        } else { 
            $product = get_post( $product_id );
            if (is_object($product) && !is_wp_error($product)) { // post not set but it's a dish
                if ($product->post_type == 'dish') 
                    return 'dish';
            } // end if 

        } // end if  // end if 



    } // end if 
    return false;
}
add_filter('woocommerce_product_type_query','YL_woo_product_type',12,2 );

function YL_woocommerce_checkout_create_order_line_item_object($item, $cart_item_key, $values, $order) {

    $product                    = $values['data'];
    if ($product->get_type() == 'dish') {
        return new YL_WC_Order_Item_Product();
    } // end if 
    return $item ;
}   
add_filter( 'woocommerce_checkout_create_order_line_item_object', 'YL_woocommerce_checkout_create_order_line_item_object', 20, 4 );

function cod_woocommerce_checkout_create_order_line_item($item,$cart_item_key,$values,$order) {
    if ($values['data']->get_type() == 'dish') {
        $item->update_meta_data( '_dish', 'yes' ); // add a way to recognize custom post type in ordered items
        return;
    } // end if 

}
add_action( 'woocommerce_checkout_create_order_line_item', 'cod_woocommerce_checkout_create_order_line_item', 20, 4 );

function YL_woocommerce_get_order_item_classname($classname, $item_type, $id) {
    global $wpdb;
    $is_IA = $wpdb->get_var("SELECT meta_value FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE order_item_id = {$id} AND meta_key = '_dish'");


    if ('yes' === $is_IA) { // load the new class if the item is our custom post
        $classname = 'YL_WC_Order_Item_Product';
    } // end if 
    return $classname;
}
add_filter( 'woocommerce_get_order_item_classname', 'YL_woocommerce_get_order_item_classname', 20, 3 );

以上代码确实将 CPT 添加到您的购物车(太棒了!!),但价格始终设置为 10,00

所以下面的代码没有给出正确的价格:(

add_filter('woocommerce_get_price', 'yl_get_dish_price', 20,2);
function yl_get_dish_price($price,$post) {
    if ($post->post->post_type === 'dish') {
        $price = get_field('price', $post->ID);
    }
    return $price;
}

有什么想法吗?

【问题讨论】:

  • 这个stackoverflow.com/questions/40732683/… 可能会回答你的问题。
  • 确定$post-&gt;post-&gt;post_type 是正确的?那里看起来像一个额外的-&gt;post
  • 我猜你是对的。我删除了 1 -> post 部分没有任何运气:(

标签: php wordpress woocommerce advanced-custom-fields price


【解决方案1】:

自 WooCommerce 3 以来,钩子 woocommerce_get_price 已过时且不推荐使用......它被以下复合钩子取代:

add_filter( 'woocommerce_product_get_price', 'yl_get_dish_price', 20, 2 );
add_filter( 'woocommerce_product_get_regular_price', 'yl_get_dish_price', 20, 2 );
function yl_get_dish_price( $price, $product ) {
    if ( $product->is_type('dish') ) {
        $price = get_field( 'price', $product->get_id() );
    }
    return $price;
}

它可以而且应该更好地工作。

【讨论】:

  • 你震撼了我的世界!!将上面的部分与我更新的大代码一起使用,你就有了一个picknick :) Tnx 很多!
猜你喜欢
  • 2021-01-04
  • 2015-07-04
  • 1970-01-01
  • 2020-10-07
  • 1970-01-01
  • 1970-01-01
  • 2021-04-12
  • 2019-04-24
  • 2020-11-19
相关资源
最近更新 更多