• Resolved an29

    (@an29)


    Hey!

    I live in norway, and our site sells batteries. We are obligated by the state to add a lead-fee. Lets say it’s 0,2$ per aH.

    Example: for a 12ah battery, the fee will be (0,2 * 12) 2,4$

    I don’t know how to add this fee. Every turtorial I find usually have a fixed or a % fee. But this fee needs to be custom for each battery.

    I have this code that adds a fee from the weight, but i don’t know how to make it gather how many “aH” the battery has.

    function shipping_weight_fee( $cart ) {
    			if ( is_admin() && ! defined( 'DOING_AJAX' ) )
    				return;
    
    			/* SETTINGS */
    
    			// Specific categories
    			$specific_categories = array( 'battery', 'batteries' );
    
    			// Initial fee
    			$fee = 0.60;
    
    			/* END SETTINGS */
    
    			// Set variable
    			$total_weight = 0;
    
    			// Loop though each cart item
    			foreach ( $cart->get_cart() as $cart_item ) {
    				// Get product id
    				$product_id = $cart_item['product_id'];
    
    				// Get weight
    				$product_weight = $cart_item['data']->get_weight();
    
    				// NOT empty & has certain category     
    				if ( ! empty( $product_weight ) && has_term( $specific_categories, 'product_cat', $product_id ) ) {
    					// Quantity
    					$product_quantity = $cart_item['quantity'];
    
    					// Add to total
    					$total_weight += $product_weight * $product_quantity;
    				}
    			}
    
    			if ( $total_weight > 0 ) {          
    				$cart->add_fee( __( 'Lead fee' ), $fee * $total_weight, false );      
    			}
    		}
    		add_action( 'woocommerce_cart_calculate_fees', 'shipping_weight_fee', 10, 1 );
Viewing 9 replies - 1 through 9 (of 9 total)
  • Rynald0s

    (@rynald0s)

    Automattic Happiness Engineer

    Hi @an29

    Would it not be easier setting the ah values up as attributes, all the way from the lowest to highest value, then using something like https://www.ads-software.com/plugins/markup-by-attribute-for-woocommerce/ to add the markup on each attribute?

    Cheers!

    Thread Starter an29

    (@an29)

    Yes the ah value is set up in the attribute. I’d want to make a similar code to the one i’ve already created. Only this get the ah value. I don’t know how to get the ah value. I used this $product_weight = $cart_item[‘data’]->get_weight(); to get the weight. Is there a simular way to get a value from the attribute?

    Thread Starter an29

    (@an29)

    This is for simple products. It don’t have product variations.

    Rynald0s

    (@rynald0s)

    Automattic Happiness Engineer

    Hi @an29

    I used this $product_weight = $cart_item[‘data’]->get_weight(); to get the weight. Is there a simular way to get a value from the attribute?

    You could try using something like the following to get attributes.

    $ah_values = get_the_terms( $product->id, 'pa_ah');

    You could also consider using a plugin like https://iconicwp.com/products/woocommerce-attribute-swatches/ to add that.

    Cheers!

    Thread Starter an29

    (@an29)

    Thanks!

    Could you see if you see anything wrong with this code?

    function shipping_weight_and_lead_fee( $cart ) {
    			if ( is_admin() && ! defined( 'DOING_AJAX' ) )
    				return;
    
    			/* SETTINGS */
    
    			// Specific categories
    			$specific_categories = array('Startbatterier', 'Buffalo Bull SHD', 'Li-ion MC batterier', 'NG – Gel Batterier', 'NH - H?yrate Batterier', 'NF – Frontmonterte Batterier', 'NM - 10?rs Batterier','NS - 5?rs Batterier', 'Running Bull', 'Power Bull PRO', 'Buffalo Bull', 'Starting Bull', 'Buffalo Bull SHD Pro', 'Power Bull', 'Bike Bull Classic 6V', 'Bike Bull Classic 12V', 'Bike Bull AGM 12V', 'Bike Bull GEL 12V', 'Bike Bull AGM PRO 12V', 'Freshpack', 'AGM', 'GEL');
    
    			// Initial fee
    			$fee = 0.60;
    			$feeah = 2.00;
    
    			/* END SETTINGS */
    
    			// Set variable
    			$total_weight = 0;
    			$total_ah = 0;
    
    			// Loop though each cart item
    			foreach ( $cart->get_cart() as $cart_item ) {
    				// Get product id
    				$product_id = $cart_item['product_id'];
    
    				// Get weight
    				$product_weight = $cart_item['data']->get_weight();
    				$ah_values = get_the_terms( $product->id, 'ah');
    
    				// NOT empty & has certain category     
    				if ( ! empty( $product_weight ) && has_term( $specific_categories, 'product_cat', $product_id ) ) {
    					// Quantity
    					$product_quantity = $cart_item['quantity'];
    
    					// Add to total
    					$total_weight += $product_weight * $product_quantity;
    				}
    				if ( ! empty( $ah_values ) && has_term( $specific_categories, 'product_cat', $product_id ) ) {
    					// Quantity
    					$product_quantity = $cart_item['quantity'];
    					$total_ah += $ah_values * $product_quantity;
    				}
    			}
    
    			if ( $total_weight > 0 ) {          
    				$cart->add_fee( __( 'Milj?tillegg' ), $fee * $total_weight, false );  
    				$cart->add_fee( __( 'Blytillegg' ), $feeah * $total_ah, false ); 
    			}
    
    		}
    		add_action( 'woocommerce_cart_calculate_fees', 'shipping_weight_and_lead_fee', 10, 1 );

    Im not that advanced yet. So i appreciate all the help i can get.

    Plugin Support Daniyal Ahmed (a11n)

    (@daniyalahmedk)

    Hi there,

    This is a fairly complex development topic. I’m going to leave it open for a bit to see if anyone is able to chime in to help you out.

    I can also recommend the WooCommerce Developer Resources Portal for resources on developing for WooCommerce.

    You can also visit the WooCommerce Facebook group or the #developers channel of the WooCommerce Community Slack. We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there, as well.

    Best,

    Thread Starter an29

    (@an29)

    Little update;

    I think my problem lays here

    // Get weight & attribute values
    $product_weight = $cart_item['data']->get_weight();
    $ah_values = get_the_terms( $product->id, 'pa_ah');

    If i duplicate the $cart_item['data'], I get the white screen of death somehow.. Is there maybe another line of code that gets the values from the AH attribute?

    I guess the $cart_item['data'] gathers all the info from the products in the cart. And it only picks out the weight.. Is it possible to use a function to get both the attribute value and the weight and assign them to $product_weight and $ah_values

    • This reply was modified 2 years, 4 months ago by an29.

    Hi @an29

    Thanks for the update, but as we explained before, support for custom coding is beyond the scope of support we are able to provide in this forum. This particular forum is for questions that are related to the WooCommerce core features.

    You can get useful inputs and insights by posting your query in the development related channels instead, that we shared above.

    Hoping for a quick resolution.

    Cheers

    Seems we’ve not had additional inputs on this thread. Thus, we encourage you to make use of the above resources.

    I’ll go ahead and mark the thread closed but please feel free to create a new thread if you have further questions.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Add a custom fee from an attribute’ is closed to new replies.