在产品中添加自定义字段以显示在购物车,结帐和订单中

时间:2019-03-25 08:56:43

标签: php wordpress woocommerce product custom-post-type

我正在使用自定义帖子类型并在其上集成了woocommerce,我可以在页面上添加购物车按钮并将其添加到购物车,因此效果很好

这是我的代码

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( 'products', '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( 'products', 'product' ) ) ) { // change birds with your post type
            $terms = get_the_terms( $product_id, 'product_type' );
            return ! empty( $terms ) ? sanitize_title( current( $terms )->name ) : 'simple';
        } 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( $price, $product ) {

    $reflector = new \ReflectionClass($product);
    $classProperty = $reflector->getProperty('id');
    $classProperty->setAccessible(true);
    $vid_id = $classProperty->getValue($product);
    //$cpt_product_price = get_field('product_specifications', $vid_id);    
    $cpt_product_price = get_field('e-commerce', $vid_id);

    if ($product->get_id() == $vid_id) {
        $price = $cpt_product_price['global']['price'];
    }

    return $price;
}

//add_filter('the_content','rei_add_to_cart_button', 20,1);
/*
function rei_add_to_cart_button($content){
    global $post;
    if ($post->post_type !== 'products') {return $content; }
    var_dump($content);


    $cpt_product_price = get_field('product_specifications', get_the_ID());
    //var_dump($cpt_product_price);


    ob_start();
    if ( !empty($cpt_product_price['_price']) ) {
    ?>
    <form action="" method="post">
        <input name="add-to-cart" type="hidden" value="<?php echo $post->ID ?>" />
        <input name="quantity" type="number" value="1" min="1"  />
        <input name="submit" type="submit" value="Add to cart" />
    </form>
    <?php
    }
    return $content . ob_get_clean();
}

*/




function rei_add_to_cart_button(){
    global $post;
    if ($post->post_type !== 'products') {return; }
    //var_dump($content);



    $get_ecommerce = get_field('e-commerce', get_the_ID());
    //var_dump($get_ecommerce);

    $get_ecommerce_price = $get_ecommerce['global']['price'];

    //global

    //price

    //$cpt_product_price = get_field('product_specifications', get_the_ID());


    //var_dump($cpt_product_price);
    //var_dump($cpt_product_price);

    ob_start();
    if ( !empty($get_ecommerce_price) ) {
    ?>
    <form action="" method="post">
        <input name="_gram" type="hidden" value="10">
        <input name="add-to-cart" type="hidden" value="<?php echo $post->ID ?>" />
        <input name="quantity" type="number" value="1" min="1"  />
        <input name="submit" type="submit" value="Add to cart" />
    </form>
    <?php
    }
    return ob_get_clean();
}

但是现在我需要在产品页面上添加一些自定义字段,一个包含颜色“黑色”和“白色”的下拉列表,这只是一个普通的下拉列表。选择一个值后如何在商品名称下将其显示在购物车上

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:1)

更新2

尝试以下(通常会在单个产品页面中添加一个下拉菜单,并在购物车商品中保存/显示选定的值)

// Frontend: custom select field (dropdown) in product single pages
add_action( 'woocommerce_before_add_to_cart_button', 'fabric_length_product_field' );
function fabric_length_product_field() {
    global $product;

    // Select field
    woocommerce_form_field('fitting_color', array(
        'type'     => 'select',
        'class'    => array('my-field-class form-row-wide'),
        'label'    => __('_fitting_color', 'woocommerce'),
        'required' => true, // or false
        'options'  => array(
            ''    => __('Select a color', 'woocommerce'),
            'black'    => __('Black', 'woocommerce'),
            'white'    => __('White', 'woocommerce'),
        ),
    ),'');
}

// Add "fitting_color" selected value as custom cart item data
add_filter( 'woocommerce_add_cart_item_data', 'add_custom_cart_item_data', 20, 2 );
function add_custom_cart_item_data( $cart_item_data, $product_id ){
    if( isset($_POST['fitting_color']) && ! empty($_POST['fitting_color'])) {
        $cart_item_data['fcolor']= array(
            'value' => esc_attr($_POST['fitting_color']),
            'unique_key' => md5( microtime() . rand() ), // <= Make each cart item unique
        );
    }
    return $cart_item_data;
}


// Display custom cart item data in cart and checkout pages
add_filter( 'woocommerce_get_item_data', 'display_custom_cart_item_data', 10, 2 );
function display_custom_cart_item_data( $cart_item_data, $cart_item ) {
    if ( isset( $cart_item['fcolor']['value'] ) ){
        $cart_item_data[] = array(
            'name' => __( 'Fitting color', 'woocommerce' ),
            'value' => $cart_item['fcolor']['value'],
        );
    }
    return $cart_item_data;
}

代码在您的活动子主题(或活动主题)的function.php文件上。应该可以。


要保存/显示订单(并显示在电子邮件通知上),您将使用:

// Save chosen slelect field value to each order item as custom meta data and display it everywhere
add_action('woocommerce_checkout_create_order_line_item', 'save_order_item_product_fitting_color', 10, 4 );
function save_order_item_product_fitting_color( $item, $cart_item_key, $values, $order ) {
    if( isset($values['fcolor']['value']) ) {
        $key = __('Fitting color', 'woocommerce');
        $value = $values['fcolor']['value'];
        $item->update_meta_data( $key, $value );
    }
}

以及缺少的字段验证:

// Field validation
add_filter( 'woocommerce_add_to_cart_validation',  'dropdown_fitting_color_validation', 10, 3 );
function dropdown_fitting_color_validation( $passed, $product_id, $quantity ) {
    if( isset($_POST['fitting_color']) && empty($_POST['fitting_color']) ) {
        wc_add_notice( __( "Please select a Fitting color", "woocommerce" ), 'error' );
        return false;
    }

    return $passed;
}