简体   繁体   English

在 Woocommerce 的购物车中显示多个产品自定义字段值

[英]Show multiple product custom fields values in cart on Woocommerce

I have a problem with custom fields & values from my product page appearing on the cart page.我的产品页面中的自定义字段和值出现在购物车页面上时出现问题。 As you can see from the screenshots, I have three fields: "Color," "Texto1" and "Texto2" and only the first seems to appear on my cart page.正如您从屏幕截图中看到的,我有三个字段:“颜色”、“Texto1”和“Texto2”,并且似乎只有第一个出现在我的购物车页面上。

This is the code that print the fields on the product page:这是在产品页面上打印字段的代码:

// Field 1
if( ! empty( $FieldType1 ) ){
    if( $FieldType1 == "TEXT AREA"){
    echo '<div>
        <label>'.$FieldName1.':<br></label> <textarea name="FieldTypeValue1" maxlength="'.$FieldLenght1.'" rows="2" cols="80" placeholder="" required></textarea>
    </div><br>';
    }

    if( $FieldType1 == "TEXT BOX"){
    echo '<div>
        <label>'.$FieldName1.':<br></label> <input type="text"  maxlength="'.$FieldLenght1.'" name="FieldTypeValue1" value="" required>
    </div><br>';
    }

    if( $FieldType1 == "DROP DOWN"){
        echo '<div>
        <label>'.$FieldName1.':<br></label>
        <select name="FieldTypeValue1">';
        foreach ($Dropdown1Content as $Dropdown1IndividualContent) {
        echo '<option>';
        echo $Dropdown1IndividualContent;
        echo '</option>';
        }

        echo '</select></div><br>';
    }
}

// Field 2  
if( ! empty( $FieldType2 ) ){
    if( $FieldType2 == "TEXT AREA"){
    echo '<div>
        <label>'.$FieldName2.':<br></label> <textarea name="FieldTypeValue2" maxlength="'.$FieldLenght2.'" rows="2" cols="80" placeholder="" required></textarea>
    </div><br>';
    }

    if( $FieldType2 == "TEXT BOX"){
    echo '<div>
        <label>'.$FieldName2.':<br></label> <input type="text"  maxlength="'.$FieldLenght2.'" name="FieldTypeValue2" value="" required>
    </div><br>';
    }

    if( $FieldType2 == "DROP DOWN"){
        echo '<div>
        <label>'.$FieldName2.':<br></label>
        <select name="FieldTypeValue2">';
        foreach ($Dropdown2Content as $Dropdown2IndividualContent) {
        echo '<option>';
        echo $Dropdown2IndividualContent;
        echo '</option>';
        }

        echo '</select></div><br>';
    }
}



// Field 3
if( ! empty( $FieldType3 ) ){
    if( $FieldType3 == "TEXT AREA"){
    echo '<div>
        <label>'.$FieldName3.':<br></label> <textarea name="FieldTypeValue3" maxlength="'.$FieldLenght3.'" rows="2" cols="80" placeholder="" required></textarea>
    </div><br>';
    }

    if( $FieldType3 == "TEXT BOX"){
    echo '<div>
        <label>'.$FieldName3.':<br></label> <input type="text"  maxlength="'.$FieldLenght3.'" name="FieldTypeValue3" value="" required>
    </div><br>';
    }

    if( $FieldType3 == "DROP DOWN"){
        echo '<div>
        <label>'.$FieldName3.':<br></label>
        <select name="FieldTypeValue3">';
        foreach ($Dropdown3Content as $Dropdown3IndividualContent) {
        echo '<option>';
        echo $Dropdown3IndividualContent;
        echo '</option>';
        }

        echo '</select></div><br>';
    }
}

This is the code that saves the value of the fields on the product page这是保存产品页面字段值的代码

// Store custom field label and value in cart item data
add_action( 'woocommerce_add_cart_item_data','save_my_custom_checkout_field', 10, 2 );
function save_my_custom_checkout_field( $cart_item_data, $product_id ) {
if( isset( $_REQUEST['FieldTypeValue1'] ) ) {
    $cart_item_data['custom_data']['label'] = get_post_meta($product_id, 'FieldName1', true);
    $cart_item_data['custom_data']['value'] = sanitize_text_field( $_REQUEST['FieldTypeValue1'] );
    $cart_item_data['custom_data']['ukey'] = md5( microtime().rand() );
}
return $cart_item_data;

if( isset( $_REQUEST['FieldTypeValue2'] ) ) {
    $cart_item_data['custom_data']['label'] = get_post_meta($product_id, 'FieldName2', true);
    $cart_item_data['custom_data']['value'] = sanitize_text_field( $_REQUEST['FieldTypeValue2'] );
    $cart_item_data['custom_data']['ukey'] = md5( microtime().rand() );
}
return $cart_item_data;

if( isset( $_REQUEST['FieldTypeValue3'] ) ) {
    $cart_item_data['custom_data']['label'] = get_post_meta($product_id,'FieldName3', true);
    $cart_item_data['custom_data']['value'] = sanitize_text_field( $_REQUEST['FieldTypeValue3'] );
    $cart_item_data['custom_data']['ukey'] = md5( microtime().rand() );
}
return $cart_item_data;

And this is the one that print the values on the cart:这是在购物车上打印值的那个:

// Display items custom fields label and value in cart and checkout pages
add_filter( 'woocommerce_get_item_data', 'render_meta_on_cart_and_checkout', 10, 2 );
function render_meta_on_cart_and_checkout( $cart_data, $cart_item ){

$custom_items = array();

/* Woo 2.4.2 updates */
if( !empty( $cart_data ) ) {
    $custom_items = $cart_data;
}
if( isset( $cart_item['custom_data'] ) ) {
    $custom_items[] = array(
        'name' => $cart_item['custom_data']['label'],
        'value' => $cart_item['custom_data']['value'],
    );
}
return $custom_items;

}

The Custom fields on product page:产品页面上的自定义字段:

产品页面

The product in cart (with the missing data):购物车中的产品(缺少数据):

购物车中的产品

I'm not sure if the problem is at the point where values are saved or at the point where they are printed.我不确定问题是在保存值的地方还是在打印它们的地方。

Any help would be much appreciated.任何帮助将非常感激。

Try this hooked functions instead, where I have revisited your code to make it work:试试这个挂钩函数,我已经重新访问了你的代码以使其工作:

// Store custom field label and value in cart item data
add_filter( 'woocommerce_add_cart_item_data','save_my_custom_checkout_field', 20, 2 );
function save_my_custom_checkout_field( $cart_item_data, $product_id ) {
    $label1 = get_post_meta( $product_id, 'FieldName1', true );
    $label2 = get_post_meta( $product_id, 'FieldName2', true );
    $label3 = get_post_meta( $product_id, 'FieldName3', true );

    if( isset( $_REQUEST['FieldTypeValue1'] ) && ! empty( $label1 ) )
        $cart_item_data['custom_data']['1'] = array(
            'label' => $label1,
            'value' => sanitize_text_field( $_REQUEST['FieldTypeValue1'] ),
        );

    if( isset( $_REQUEST['FieldTypeValue2'] ) && ! empty( $label2 ) )
        $cart_item_data['custom_data']['2'] = array(
            'label' => $label2,
            'value' => sanitize_text_field( $_REQUEST['FieldTypeValue2'] ),
        );

    if( isset( $_REQUEST['FieldTypeValue3'] ) && ! empty( $label3 ) )
        $cart_item_data['custom_data']['3'] = array(
            'label' => $label3,
            'value' => sanitize_text_field( $_REQUEST['FieldTypeValue3'] ),
        );

    if( count($cart_item_data['custom_data']) > 0 )
        $cart_item_data['custom_data']['key'] = md5( microtime().rand() );

    return $cart_item_data;
}

// Display items custom fields label and value in cart and checkout pages
add_filter( 'woocommerce_get_item_data', 'render_meta_on_cart_and_checkout', 20, 2 );
function render_meta_on_cart_and_checkout( $cart_data, $cart_item ){
    $custom_items = array();

    if( !empty( $cart_data ) )
        $custom_items = $cart_data;

    if( isset( $cart_item['custom_data'] ) ) {
        foreach( $cart_item['custom_data'] as $key => $custom_data ){
            if( $key != 'key' ){
                $custom_items[] = array(
                    'name' => $custom_data['label'],
                    'value' => $custom_data['value'],
                );
            }
        }
    }
    return $custom_items;
}

Code goes in function.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 function.php 文件中。

Tested and works… You will get something like that:测试和工作......你会得到这样的东西:

在此处输入图片说明


For testing I have used the following to display the 3 custom fields on single product page:为了测试,我使用以下内容在单个产品页面上显示 3 个自定义字段:

// Displaying 3 custom fields on single product pages
add_action( 'woocommerce_before_add_to_cart_button', 'add_custom_fields_single_product', 20 );
function add_custom_fields_single_product(){
    global $product;

    echo '<div>
    <label>'.__('Color').': </label><br>
    <input type="text" name="FieldTypeValue1" value="" required>
</div><br>';
    echo '<div>
    <label>'.__('Texto 1').': </label><br>
    <input type="text" name="FieldTypeValue2" value="" required>
</div><br>';
    echo '<div>
    <label>'.__('Texto 2').': </label><br>
    <input type="text" name="FieldTypeValue3" value="" required>
</div><br>';
}

Changing the following for testing purpose only (in the functions code):仅出于测试目的更改以下内容(在函数代码中):

    $label1 = get_post_meta( $product_id, 'FieldName1', true );
    $label2 = get_post_meta( $product_id, 'FieldName2', true );
    $label3 = get_post_meta( $product_id, 'FieldName3', true );

by:经过:

    $label1 = __('Color'); 
    $label2 = __('Texto 1'); 
    $label3 = __('Texto 2'); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果Woocommerce上的购物车中有特定产品,则显示自定义结帐字段 - Show custom checkout fields if a specific product is in cart on Woocommerce 在购物车和订单项目名称下显示 woocommerce 产品自定义字段 - Show woocommerce product custom fields under the cart and order item name WooCommerce条件自定义字段,如果购物车中的产品类别 - WooCommerce Conditional Custom Fields if Product Category in Cart 自定义添加到购物车按钮,将多个产品添加到购物车中,数量:woocommerce - Custom add to cart button to add multiple product into cart with quantity :woocommerce 将产品自定义字段添加到购物车并在 WooCommerce 中随处显示 - Add product custom fields to cart and display them everywhere in WooCommerce 在Woocommerce中每个商品名称旁边的购物车中显示产品自定义字段 - Display product custom fields in cart next to each item name in Woocommerce Woocommerce AJAX API未将自定义产品字段发布到购物车 - Woocommerce AJAX API not Posting custom Product fields to Cart 在WooCommerce中将自定义产品添加到购物车 - Add custom product to cart in WooCommerce woocommerce产品自定义字段 - woocommerce product custom fields WooCommerce自定义产品类型 - 多个添加到购物车部分的问题 - WooCommerce Custom product type - Multiple add to cart sections issue
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM