Quantcast
Channel: WordPress.org Forums » All Topics
Viewing all articles
Browse latest Browse all 59525

Add a custom fee from an attribute

$
0
0

Replies: 0

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 all articles
Browse latest Browse all 59525

Trending Articles