使用ACF编号字段作为WooCommerce自定义产品类型的价格

YorlinqNL

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

后记,您可以像这样轻松地创建购物车网址:https://yourdomain.com/?add-to-cart=XXX(XXX是“自定义帖子类型”帖子ID)

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

该代码应类似于以下内容,但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添加到您的购物车中(GREAT !!),但是价格始终设置为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;
}

任何想法?

LoicTheAztec

从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;
}

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在WooCommerce中为自定义产品类型启用价格和库存

产品类别的WooCommerce产品价格中的自定义小数

根据产品类型向产品价格添加自定义文本标签

将自定义字段价格作为产品价格分配给WooCommerce中的特定用户角色

在WooCommerce 4中以编程方式从自定义产品类型添加新产品

如何仅为自定义产品类型定义字段-Woo Commerce Hook

从自定义产品类型创建产品

基于Woocommerce中产品类别的条件自定义结帐字段

产品类别内的“高级自定义字段中继器”(woocommerce)

在Woocommerce商店和产品类别页面上显示高级自定义字段

根据Woocommerce中的产品类别添加自定义结帐字段

在 Wordpress 自定义帖子类型循环中使用 ACF 分类字段作为变量

在Woocommerce单个产品页面中显示高于产品价格的自定义字段

WooCommerce自定义产品类型-多个添加到购物车部分的问题

在WooCommerce自定义产品类型上显示库存状态

如何检查WooCommerce中的自定义产品类型选项?

如何在 Woocommerce 的自定义产品类型中添加变体选项卡?

在WooCommerce中更改自定义产品类型的订单运送地址

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

也可以使用ACF在WooCommerce购物车中为可变产品获取自定义字段

在 WooCommerce 循环产品标题后显示来自产品类别的自定义字段

在WooCommerce中将自定义标签添加到特定产品类型的单个产品页面

自定义价格字段未保存在WooCommerce中的管理产品上

从WooCommerce中的变化产品获取自定义字段值和最高价格

同时根据产品自定义字段更改WooCommerce Minicart项目价格

通过自定义字段在Woocommerce中更改购物车中的产品价格

用产品自定义字段值替代woocommerce购物车项目价格

从自定义字段值替换 Woocommerce 产品可变价格范围

在选择WooCommerce产品变动价格后添加自定义字段值