在woocommerce中,我成功地为woocommerce简单产品添加了一个自定义字段,并在产品页面的附加信息选项卡上显示该值。
现在,我完全迷失在我的拼图的最后一块中,试图在购物车和结账页面中插入一行,以计算总的计算量。
自定义字段用于存储家具项目的体积(以m3为单位)。当客户将一个项目添加到购物篮中时,我想通过将所有自定义字段的值相加,并在页面上向客户显示总数,来计算发货的总立方米数。有人能告诉我如何添加这些自定义字段并显示它们吗。
到目前为止,我的代码如下所示
add_action( 'woocommerce_product_options_general_product_data', 'woocom_general_product_data_custom_field' );
function woocom_general_product_data_custom_field() {
woocommerce_wp_text_input(
array(
'id' => '_item_volume',
'label' => __( 'Item Shipping Volume', 'woocommerce' ),
'placeholder' => '',
'description' => __( 'Enter the volume her in m3.', 'woocommerce' ),
'type' => 'number',
'custom_attributes' => array(
'step' => 'any',
'min' => '0.001'
)
)
);
}
add_action( 'woocommerce_process_product_meta', 'woocom_save_general_proddata_custom_field' );
function woocom_save_general_proddata_custom_field( $post_id ) {
$number_field = $_POST['_item_volume'];
if( ! empty( $number_field ) ) {
update_post_meta( $post_id, '_item_volume', esc_attr( $number_field ) );
}
}
add_action( 'woocommerce_product_additional_information', 'custom_data_in_product_add_info_tab', 20, 1 );
function custom_data_in_product_add_info_tab( $product ) {
$product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
$custom_field1 = get_post_meta( $product_id, '_item_volume', true );
$label1 = __( 'Shipping Volume m3', 'woocommerce');
echo '<h3>'. __('Item Shipping Volume', 'woocommerce') .'</h3>
<table class="custom-fields-data">
<tbody>
<tr class="custom-field1">
<th>'. $label1 .'</th>
<td>'. $custom_field1 .'</td>
</tr>
</tbody>
</table>';
}