• Resolved aaser1

    (@aaser1)


    Er det mulig ? sette inn fast transport p? produkter som g?r innen norgespakkene – dvs den st?rste p? maks 35 kg.?
    kan vi sette fastpris p? f.eks:
    under 10 kg -129kr
    inntil 25kg – 229kr
    inntil 35kg – 299kr

    • This topic was modified 1 year, 5 months ago by aaser1.
Viewing 1 replies (of 1 total)
  • Plugin Author Eivin Landa

    (@forsvunnet)

    Hei, det er ikke mulig via innstillingene, men du kan legge til en egen kode enten som en plugin eller i functions.php filen i themet du bruker.

    <?php
    
    /**
     * Filter the bring shipping rates.
     * Sets the price of the chosen services based on weight
     */
    add_filter( 'bring_shipping_rates', function( $rates ) {
    	// Configure services and prices.
    	$services = [ 'SERVICEPAKKE', '5800' ];
    	$prices   = [
    		'0kg'  => 129,
    		'10kg' => 229,
    		'25kg' => 299,
    	];
    
    	// Calculate the weight of items in cart.
    	$total_weight = 0;
    	$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 = wc_get_weight( $total_weight, 'g' );
    
    	// 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_unit = $matches[2];
    		$weight      = wc_get_weight( $matches[1], 'g', $weight_unit );
    		if ( $total_weight > $weight ) {
    			$price = $cost;
    		}
    	}
    
    	// Return early if the total weight is less than the lowest price threshold.
    	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 );
Viewing 1 replies (of 1 total)
  • The topic ‘Sette fastpris p? frakt og KG.’ is closed to new replies.