logo

Pablo Guides

איך להסיר סוגי משלוח לפי קטגוריות המוצר בעמוד לתשלום בווקומרס ?

 

המקטע קוד הנל יסיר את שיטת המשלוח "method1" כאשר מתקיים תנאי בו יש מוצרים בעגלה ששיכיים לקטגוית המוצרים : cellphones_and_pc בעלת השם : Cellphones And PC

 

/**
 * remove shipping methods in the checkout based on products in cart categories
 */
add_filter( 'woocommerce_package_rates', 'pablo_filter_shipping_methods', 10, 2 );
function pablo_filter_shipping_methods( $rates, $package ) {
	// Find method1 shipping method
	$method1_shipping_method_key = FALSE;
	foreach ( $rates as $rate_key => $rate ) {
		if ( is_object( $rate ) && method_exists( $rate, 'get_label' ) && $rate->get_label() === "Some" ) {
			$method1_shipping_method_key = $rate_key;
		}
	}

	// go through all products in the cart and check their categories
	if ( $method1_shipping_method_key !== FALSE ) {
		$cellphones_and_pc_found = FALSE;
		foreach ( $package['contents'] as $key => $item ) {
			$categories = get_the_terms( $item['product_id'], 'product_cat' );

			if ( $categories && ! is_wp_error( $categories ) && is_array( $categories ) ) {
				foreach ( $categories as $category ) {
					if ( "Cellphones And PC" === $category->name ) {
						$cellphones_and_pc_found = TRUE;
					}
				}
			}
		}

		//mehod Cellphones And PC category products have been found, so we will disable the method1 shipping
		if ( $cellphones_and_pc_found === TRUE ) {
			unset( $rates[$method1_shipping_method_key] );
		}
	}

	return $rates;
}

Pablo Guides