Reputation: 599
I'm trying to add a "Quantity per package" field to WooCommerce product, and I'd need this field in the Shipping tab of the product backend. Then I need to display this number under the package dimensions in the Additional Informations tab.
I could add a row to the Additional Informations tab, but I don't know how to input the value and make it get that value to display.
Currently I am using the following, someone who can help me on my way?
add_filter( 'woocommerce_display_product_attributes', 'custom_product_additional_information', 10, 2 );
function custom_product_additional_information( $product_attributes, $product ) {
$product_attributes[ 'attribute_' . 'custom-one' ] = array(
'label' => __('Quantity per Package'),
'value' => __('Value 1'),
);
return $product_attributes;
}
Upvotes: 0
Views: 1054
Reputation: 29660
Code contains:
// Add custom field to product shipping tab
function action_woocommerce_product_options_shipping(){
$args = array(
'label' => __( 'My label', 'woocommerce' ),
'placeholder' => __( 'My placeholder', 'woocommerce' ),
'id' => '_quantity_per_package',
'desc_tip' => true,
'description' => __( 'My description', 'woocommerce' ),
);
woocommerce_wp_text_input( $args );
}
add_action( 'woocommerce_product_options_shipping', 'action_woocommerce_product_options_shipping', 10, 0 );
// Save
function action_woocommerce_admin_process_product_object( $product ) {
// Isset
if( isset($_POST['_quantity_per_package']) ) {
$product->update_meta_data( '_quantity_per_package', sanitize_text_field( $_POST['_quantity_per_package'] ) );
}
}
add_action( 'woocommerce_admin_process_product_object', 'action_woocommerce_admin_process_product_object', 10, 1 );
// Display value on additional Informations tab if NOT empty
function filter_woocommerce_display_product_attributes( $product_attributes, $product ) {
// Get meta
$quantity_per_package = $product->get_meta( '_quantity_per_package' );
// NOT empty
if ( ! empty ( $quantity_per_package ) ) {
$product_attributes[ 'quantity_per_package' ] = array(
'label' => __('Quantity per Package', 'woocommerce '),
'value' => $quantity_per_package,
);
}
return $product_attributes;
}
add_filter( 'woocommerce_display_product_attributes', 'filter_woocommerce_display_product_attributes', 10, 2 );
Upvotes: 3