【发布时间】:2018-09-10 03:23:35
【问题描述】:
在 woocommerce 中,我成功地向 Woocommerce 简单产品添加了一个自定义字段,并将该值显示在产品页面的附加信息选项卡上。
现在我完全迷失了,我的拼图的最后一块试图在购物车和结帐页面中插入一个总行以计算总计算量。
自定义字段用于存储家具项目的体积(以 m3 为单位)。当客户将商品添加到购物篮时,我想通过将所有自定义字段的值相加并在页面上向客户显示总数来计算装运的总 m3。谁能指出我如何添加这些自定义字段并显示它们的正确方向。
到目前为止,我的代码是这样的
// Display Fields using WooCommerce Action Hook
add_action( 'woocommerce_product_options_general_product_data', 'woocom_general_product_data_custom_field' );
function woocom_general_product_data_custom_field() {
// Create a custom text field
// Number 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'
)
)
);
}
// Hook to save the data value from the custom fields
add_action( 'woocommerce_process_product_meta', 'woocom_save_general_proddata_custom_field' );
/** Hook callback function to save custom fields information */
function woocom_save_general_proddata_custom_field( $post_id ) {
// Save Number Field
$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 - WooCommerce compatibility
$product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
// Get your custom fields data
$custom_field1 = get_post_meta( $product_id, '_item_volume', true );
// Set your custom fields labels (or names)
$label1 = __( 'Shipping Volume m3', 'woocommerce');
// The Output
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>';
}
【问题讨论】:
标签: php wordpress woocommerce volume shipping