• Resolved fahadjamil92

    (@fahadjamil92)


    Hei,

    Er det mulig ? sette opp faste fraktpriser basert p? en eller flere vektintervall?

    Jeg ?nsker ? sette opp slik:

    1-5kg: 199 kr
    5-10kg: 299 kr
    10kg- : 399 kr

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Eivin Landa

    (@forsvunnet)

    Hei,

    Dette er desverre ikke mulig via instillingene. For ? f? til dette m? det programmeres som en egen plugin eller i theme.

    Eksempelkode:

    
    /**
     * Convert weight into grams
     */
    $weight_in_grams = function( $weight, $weight_unit ) {
    	$weight = floatval( $weight );
    	switch ( $weight_unit ) {
    		case 'g':
    			break;
    
    		case 'kg':
    			$weight = $weight / 0.0010000;
    			break;
    
    		case 'lbs':
    			$weight = $weight / 0.0022046;
    			break;
    
    		case 'oz':
    			$weight = $weight / 0.035274;
    			break;
    		/* Unknown weight unit */
    		default:
    			$weight = 0;
    	}
    	return $weight;
    };
    /**
     * Filter the bring shipping rates.
     */
    add_filter( 'bring_shipping_rates', function( $rates ) use ( $weight_in_grams ) {
    	// Configure services and prices.
    	$services = [ 'SERVICEPAKKE', '5800' ];
    	$prices   = [
    		'1kg'  => 199,
    		'5kg'  => 299,
    		'10kg' => 399,
    	];
    
    	// Calculate the weight of items in cart.
    	$total_weight = 0;
    	$weight_unit  = get_option( 'woocommerce_weight_unit' );
    	$items = WC()->cart->get_cart();
    	foreach ( $items as $item ) {
    		if ( ! $item['data']->needs_shipping() ) {
    			continue;
    		}
    		$total_weight += $item['data']->get_weight() * $item['quantity'];
    	}
    	// Convert the total weight to grams.
    	$total_weight = $weight_in_grams( $total_weight, $weight_unit );
    
    	// Calculate the price for the current weight.
    	$price = false;
    	foreach ( $prices as $threshold => $cost ) {
    		// Find the value and weight unit.
    		if ( ! preg_match( '/^(\d+)\s*(kg|g|lbs|oz)$/', $threshold, $matches ) ) {
    			continue;
    		}
    		$weight      = $matches[1];
    		$weight_unit = $matches[2];
    		$weight      = $weight_in_grams( $weight, $weight_unit );
    		if ( $total_weight > $weight ) {
    			$price = $cost;
    		}
    	}
    
    	// Return early if the total weight is less than the lowest price.
    	if ( false === $price ) {
    		return $rates;
    	}
    
    	// Set the price for the chosen services.
    	foreach ( $rates as &$rate ) {
    		if ( in_array( $rate['bring_product'], $services ) ) {
    			$rate['cost'] = $price;
    		}
    	}
    
    	return $rates;
    }, 999 );
    
    Thread Starter fahadjamil92

    (@fahadjamil92)

    Den er god, takk!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Fast fraktpris basert p? vektintervall’ is closed to new replies.