• Resolved Andi

    (@atanke)


    Hi there,
    is there any way to add a packaging weight to the total weight?
    I’ve tried it with this:
    `

    add_filter( ‘woocommerce_cart_contents_weight’, ‘add_packaging_weight’, 10, 1 );
    function add_packaging_weight( $weight )
    {
    $weight = 0;
    foreach ( WC()->cart->get_cart() as $cart_item )
    {
    $product = $cart_item[‘data’];
    if ( $product->get_shipping_class() == ‘kalender’ && $product->has_weight() )
    $weight += (float) 0.2;
    elseif ( $product->get_shipping_class() == ‘klein’ && $product->has_weight() )
    $weight += (float) 0.15;
    elseif ( $product->get_shipping_class() == ‘mittel’ && $product->has_weight() )
    $weight += (float) 0.3;
    elseif ( $product->get_shipping_class() == ‘poster-gross’ && $product->has_weight() )
    $weight += (float) 0.3;
    }
    return $weight;
    }


    But it seems that the plugin doesn’t use the cart weight, it calculate it by itself.
    Is there maybe a filter hook that I can use to manipulate the shipping weight?
    I do have several different products and the parcels have different weights. But I have to add theme before I buy the shipping labels….

    Bets Regards Andreas

    • This topic was modified 3 years, 10 months ago by Andi.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Shadi Manna

    (@shadim)

    Yes, you can use the hook “pr_shipping_dhl_order_weight” to modify the calculated weight for the package for an order.

    Thread Starter Andi

    (@atanke)

    Hi Shadi,
    thanks for that. With this code it works fine

    add_filter( 'pr_shipping_dhl_order_weight', 'add_packaging_weight_dhl', 10, 2 );
    function add_packaging_weight_dhl( $weight, $order_id ) 
    	{
    		$order = wc_get_order( $order_id );
    		$ordered_items = $order->get_items( );
    		$weight = 0;
    		$usedShippingclasses=array();
    		
    		foreach ($ordered_items as $key => $item) 
    			{					
    				if( ! empty( $item['variation_id'] ) ) 
    					$product = wc_get_product($item['variation_id']);
    				else
    					$product = wc_get_product( $item['product_id'] );
    			
    				if ( $product ) 
    					{
    						$product_weight = $product->get_weight();
    						if( $product_weight ) 
    							{
    								$weight += (float) ( $item['qty'] * $product_weight );
    								if ( $product->get_shipping_class() == 'class_1' && $product->has_weight() && (!(in_array($product->get_shipping_class(),$usedShippingclasses))))
    									{
    										$usedShippingclasses[]=$product->get_shipping_class();
    										$weight += (float) 0.2;										
    									}
    								elseif ( $product->get_shipping_class() == 'class_2' && $product->has_weight() && (!(in_array($product->get_shipping_class(),$usedShippingclasses))))
    									{
    										$usedShippingclasses[]=$product->get_shipping_class();
    										$weight += (float) 0.5;										
    									}
    							}
    					}
    			}	
    		
    		return $weight;
    	}

    Best Regards

    Andreas

    Plugin Author Shadi Manna

    (@shadim)

    I am happy to hear that.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Packaging weight per Shipping class’ is closed to new replies.