Update WooCommerce Delivery cost with condition

Change Delivery price base on what item in cart

<?php
/**
 * Add extra price to shipping for a product
 */
add_filter( 'woocommerce_shipping_method_add_rate_args', 'snippet_charge_shipping_extra_per_product', 100, 2 );
function snippet_charge_shipping_extra_per_product( $args, $shipping_method ) {
	// Check that cart contains a product named "Bananas"
	$contains = false;
	foreach ( WC()->cart->get_cart() as $key => $item ) {
		if ( isset( $item['data'] ) && method_exists( $item['data'], 'get_title' ) && $item['data']->get_title() === "Bananas" ) {
			$contains = true;
			break;
		}
	}
	
	// Check that we are processing Economy Shipping method
	if ( $contains && $shipping_method && $shipping_method->get_title() === "Economy Shipping" ) {
		// Add extra of $ 3 to the shipping method. Extra price is added to $args['cost']
		// Taxes will be calculated from the new price.
		$args['cost'] += 3;
	}

	return $args;
}
Previous Code

JS set interval for an event until element show

Sometimes we need to active an event when a specif ...

0 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

17 − nine =

Get WooCommerce product info

Show product info in the place as you wish to. It ...

WooCommerce Checkout Conflict with Bootstrap 4.x

WooCommerce Checkout Conflict with Bootstrap 4.x. ...

Remove WooCommerce checkout fields

Remove WooCommerce Checkout field with simple filt ...

ACF

JavaScript

jQuery

Others

PHP

WooCommerce

WordPress

skype

Need Coding Help?

Connect Skype
top