1. Home
  2. Knowledge Base
  3. WooCommerce Product Add-Ons Ultimate
  4. Advanced Topics for Add-Ons Ultimate
  5. Assigning a weight to product add-ons
  1. Home
  2. Knowledge Base
  3. WooCommerce Product Add-Ons Ultimate
  4. Assigning a weight to product add-ons

Assigning a weight to product add-ons

There are many occasions where you would like to be able to assign a weight to your WooCommerce product add-on field. This is particularly true if you need to consider the weight of add-ons when calculating shipping.

Fortunately there are several ways to do this with WooCommerce Product Add-Ons Ultimate.

See this article for a complete tutorial on selling by weight in WooCommerce.

Assigning weight to add-ons using child products

The simplest, no-code way to assign weight to your add-ons is to use ‘child products’. This feature is available in the Pro version of the plugin.

Child products allow you to use other products as add-on options in your WooCommerce product. For example, if you are selling build-your-own computers, the casing, motherboard, graphics card, etc might all be standalone products.

This means that you can track the stock levels of your options and you can assign weight and size. When the user selects the different options available, the weight of each child product will be added to the cart and the shipping will be calculated correctly.

If you don’t want the user to be able to purchase your product add-ons separately, then it’s possible to hide them from the rest of the store, ensuring they’re only available as extra fields as part of the main product.

Using this method in conjunction with a weight based shipping plugin will mean you can assign weight to your add-ons and these weights will affect the shipping price.

It might be that some of your product add-ons will affect the product weight. One way to do this is to use child products, which will have their own weights.

Setting overall product weight using calculation fields

Calculation fields are available in the Pro version of the plugin. They allow you to calculate values based on user inputs, prices, and other variables.

You can use calculation fields to set a product’s price or even to set a product’s weight. There’s a document on how to use calculation fields here.

Assigning weight to product add-ons using code

If you don’t want to use child products, you can use the following code to add a new weight parameter to radio or select fields.

<?php
/**
* Add a custom weight parameter to add-on field settings
*/
function prefix_add_option_param( $option_count, $group_id, $item_key, $item, $key ) {
$name = '_product_extra_groups_' . esc_attr( $group_id ) . '_' . esc_attr( $item_key ) . '[field_options][' . esc_attr( $option_count ) . ']';
$weight = isset( $item['field_options'][esc_attr( $key )]['weight'] ) ? $item['field_options'][esc_attr( $key )]['weight'] : '';
?>
<td class="pewc-option-extra">
<input type="number" class="pewc-field-option-weight" name="<?php echo $name; ?>[weight]" value="<?php echo esc_attr( $weight ); ?>" step="0.01">
</td>
<?php }
add_action( 'pewc_after_option_params', 'prefix_add_option_param', 10, 5 );
/**
* Add the new weight param to the cart item data
*/
function prefix_end_add_cart_item_data( $cart_item_data, $item, $group_id, $field_id, $value ) {
if( ! empty( $item['field_options'] ) ) {
// Check if $value was originally an array
if( strpos( $value, ' | ' ) !== false ) {
$value_arr = explode( ' | ', $value );
} else {
$value_arr = array( $value );
}
foreach( $value_arr as $value ) {
foreach( $item['field_options'] as $option ) {
$option_len = strlen( $option['value'] );
if( substr( $value, 0, $option_len ) == $option['value'] ) {
$weight = isset( $cart_item_data['product_extras']['weight'] ) ? $cart_item_data['product_extras']['weight'] : 0;
$weight += floatval( $option['weight'] );
$cart_item_data['product_extras']['weight'] = $weight;
}
}
}
}
return $cart_item_data;
}
add_filter( 'pewc_filter_end_add_cart_item_data', 'prefix_end_add_cart_item_data', 10, 5 );
// Update the weight
function prefix_get_item_data( $item_data, $cart_item ) {
if( ! empty( $cart_item['product_extras']['weight'] ) ) {
$item_weight = $cart_item['data']->get_weight();
$item_weight = $cart_item['product_extras']['weight'];
$item_data[] = array(
'key' => __('Weight', 'woocommerce'),
'value' => $item_weight,
'display' => $item_weight . ' ' . get_option('woocommerce_weight_unit')
);
}
return $item_data;
}
add_filter( 'woocommerce_get_item_data', 'prefix_get_item_data', 10, 2 );

Was this article helpful?

Related Articles