【问题标题】:Show multiple product custom fields values in cart on Woocommerce在 Woocommerce 的购物车中显示多个产品自定义字段值
【发布时间】:2018-07-16 13:15:26
【问题描述】:

我的产品页面出现在购物车页面上的自定义字段和值有问题。从屏幕截图中可以看出,我有三个字段:“颜色”、“Texto1”和“Texto2”,而我的购物车页面上似乎只有第一个字段。

这是在产品页面上打印字段的代码:

// 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>';
    }
}

这是保存产品页面字段值的代码

// 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;

这是在购物车上打印值的那个:

// 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;

}

产品页面的自定义字段:

购物车中的产品(缺少数据):

我不确定问题出在保存值的点还是打印点。

任何帮助将不胜感激。

【问题讨论】:

    标签: php wordpress woocommerce cart custom-fields


    【解决方案1】:

    试试这个钩子函数,我已经重新访问了你的代码以使其工作:

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

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。

    经过测试并且可以工作......你会得到类似的东西:


    为了测试,我使用以下内容在单个产品页面上显示 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>';
    }
    

    仅出于测试目的更改以下内容(在函数代码中):

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

    作者:

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

    【讨论】:

    • 工作!再次感谢您完成如此快速准确的工作!
    猜你喜欢
    • 1970-01-01
    • 2018-08-28
    • 1970-01-01
    • 2019-05-11
    • 2021-07-24
    • 1970-01-01
    • 2019-07-21
    • 2019-07-07
    • 2019-01-31
    相关资源
    最近更新 更多