当前位置: 首页 > 知识库问答 >
问题:

将自定义域数据保存到Woocommerce产品变体的购物车和订单中

胡昊
2023-03-14

我们在这里按照Remi Corson的指南为变体产品设置了一个自定义域

此时,当用户选择变体时,我们能够在单个产品页面中显示自定义文本字段,但这在购买过程中是不够的,因为我们需要:

A)在购物车和结帐中显示此文本
B)保存此信息,以便在“感谢”页面、“电子邮件”和“管理订单编辑”页面中显示此信息

类似于在WooCommerce订单和电子邮件上保存和显示产品自定义元,但使用的是产品变体而不是简单的产品。

这是我们添加到functions.php中的代码,用于将定制字段添加到产品变体中

// Add Variation Settings
add_action( 'woocommerce_product_after_variable_attributes', 'variation_settings_fields', 10, 3 );

// Save Variation Settings
add_action( 'woocommerce_save_product_variation', 'save_variation_settings_fields', 10, 2 );

/**
 * Create new fields for variations
 *
*/
function variation_settings_fields( $loop, $variation_data, $variation ) {

    // Text Field
    woocommerce_wp_text_input( 
        array( 
            'id'          => '_text_field[' . $variation->ID . ']', 
            'label'       => __( 'My Text Field', 'woocommerce' ), 
            'placeholder' => 'http://',
            'desc_tip'    => 'true',
            'description' => __( 'Enter the custom value here.', 'woocommerce' ),
            'value'       => get_post_meta( $variation->ID, '_text_field', true )
        )
    );

        // Hidden field
    woocommerce_wp_hidden_input(
    array( 
        'id'    => '_hidden_field[' . $variation->ID . ']', 
        'value' => 'hidden_value'
        )
    );

}

/**
 * Save new fields for variations
 *
*/
function save_variation_settings_fields( $post_id ) {

    // Text Field
    $text_field = $_POST['_text_field'][ $post_id ];
    if( ! empty( $text_field ) ) {
        update_post_meta( $post_id, '_text_field', esc_attr( $text_field ) );
    }

    // Hidden field
    $hidden = $_POST['_hidden_field'][ $post_id ];
    if( ! empty( $hidden ) ) {
        update_post_meta( $post_id, '_hidden_field', esc_attr( $hidden ) );
    }

}

// Add New Variation Settings
add_filter( 'woocommerce_available_variation', 'load_variation_settings_fields' );

/**
 * Add custom fields for variations
 *
*/
function load_variation_settings_fields( $variations ) {
    
    // duplicate the line for each field
    $variations['text_field'] = get_post_meta( $variations[ 'variation_id' ], '_text_field', true );
    
    return $variations;

}

并将每个变体所拥有的自定义字段信息保存到感谢页面、电子邮件和订单页面(我们使用此代码对简单的产品进行了此操作,但对可变的产品不起作用)

// Save and display "Custom Field for Simple Products" on order items everywhere
add_filter( 'woocommerce_checkout_create_order_line_item', 'action_wc_checkout_create_order_line_item', 10, 4 );
function action_wc_checkout_create_order_line_item( $item, $cart_item_key, $values, $order ) {

    // Get the Custom Field
    $value = $values['data']->get_meta( 'custom_field_for_simple_products' );

    if( ! empty( $value ) ) {
        // Save it and display it
        $item->update_meta_data( __( 'Custom Fields', 'woocommerce' ), $value );
    }
}

请救命!!

共有1个答案

商同化
2023-03-14

已更新

在您的代码中有一些错误…以下重新检查的代码将解决您的问题:

// Display Variation custom fields (admin)
add_action( 'woocommerce_product_after_variable_attributes', 'display_variation_setting_custom_fields', 10, 3 );
function display_variation_setting_custom_fields( $loop, $variation_data, $variation ) {
    echo '<div>';

    woocommerce_wp_text_input( array( // Text Field
        'id'            => "_text_field[$loop]",
        'label'         => __("My Text Field", "woocommerce"),
        'placeholder'   => "http://",
        'desc_tip'      => true,
        'description'   => __("Enter the custom value here.", "woocommerce"),
        'wrapper_class' => 'form-row form-row-full',
        'value'         => get_post_meta( $variation->ID, '_text_field', true ),
    ) );

    woocommerce_wp_hidden_input( array( // Hidden field
        'id'    => "_hidden_field[$loop]",
        'value' => 'hidden_value',
    ) );
    echo '</div>';
}


// Save Variation custom fields
add_action( 'woocommerce_save_product_variation', 'save_variation_custom_fields', 10, 2 );
function save_variation_custom_fields( $variation_id, $i ) {
    // Save Text Field
    if( isset( $_POST['_text_field'][$i] ) && ! empty( $_POST['_text_field'][$i] ) )
        update_post_meta( $variation_id, '_text_field', sanitize_text_field( $_POST['_text_field'][$i] ) );

    // Save Hidden Field
    if( isset( $_POST['_hidden_field'][$i] ) && ! empty( $_POST['_hidden_field'][$i] ) )
        update_post_meta( $variation_id, '_hidden_field', esc_attr( $_POST['_hidden_field'][$i] ) );
}

// Include our variation custom field
add_filter( 'woocommerce_available_variation', 'include_variation_custom_field', 10, 3) ;
function include_variation_custom_field( $data, $product, $variation ) {
    $data['text_field'] = $variation->get_meta( '_text_field' );

    return $data;
}

// Save and display "Custom Field for Simple Products" on order items everywhere
add_filter( 'woocommerce_checkout_create_order_line_item', 'action_wc_checkout_create_order_line_item_2', 10, 4 );
function action_wc_checkout_create_order_line_item_2( $item, $cart_item_key, $values, $order ) {
    // Get the Custom Field
    $value = $values['data']->get_meta( '_text_field' );

    if( ! empty( $value ) ) {
        // Save it and display it
        $item->update_meta_data( __( 'Custom Field', 'woocommerce' ), $value );
    }
}

代码放在您的活动子主题(或活动主题)的functions.php文件中。经过测试并起作用。

在“管理编辑订单”页面上:

 类似资料: