איך להסיר סוגי משלוח לפי קטגוריות המוצר בעמוד לתשלום בווקומרס ?
המקטע קוד הנל יסיר את שיטת המשלוח "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;
}
📊 נתוני צפיות
סה"כ צפיות: 36
מבקרים ייחודיים: 36
- 🧍 172.69.166.56 ( Singapore) Singapore)
- 🧍 172.69.6.99 ( United States) United States)
- 🧍 172.69.59.179 ( United States) United States)
- 🧍 172.69.7.3 ( United States) United States)
- 🧍 172.71.194.145 ( United States) United States)
- 🧍 172.69.6.205 ( United States) United States)
- 🧍 172.71.151.175 ( United States) United States)
- 🧍 172.71.222.175 ( United States) United States)
- 🧍 172.71.194.46 ( United States) United States)
- 🧍 172.68.245.164 ( United States) United States)
- 🧍 104.23.239.2 ( Germany) Germany)
- 🧍 104.23.213.53 ( United States) United States)
- 🧍 104.23.229.78 ( France) France)
- 🧍 104.23.225.168 ( France) France)
- 🧍 104.23.229.40 ( France) France)
- 🧍 172.70.46.19 ( Netherlands) Netherlands)
- 🧍 172.70.43.44 ( United States) United States)
- 🧍 172.71.223.103 ( United States) United States)
- 🧍 172.71.135.74 ( France) France)
- 🧍 172.71.182.93 ( Netherlands) Netherlands)
- 🧍 172.68.15.168 ( United States) United States)
- 🧍 172.68.245.52 ( United States) United States)
- 🧍 172.68.15.176 ( United States) United States)
- 🧍 162.158.78.72 ( United States) United States)
- 🧍 172.70.255.92 ( United States) United States)
- 🧍 172.68.23.140 ( United States) United States)
- 🧍 172.70.43.77 ( United States) United States)
- 🧍 172.71.194.66 ( United States) United States)
- 🧍 172.70.100.233 ( United States) United States)
- 🧍 172.69.130.218 ( Canada) Canada)
- 🧍 108.162.216.55 ( United States) United States)
- 🧍 172.71.1.141 ( United States) United States)
- 🧍 104.23.209.137 ( United States) United States)
- 🧍 172.71.126.41 ( France) France)
- 🧍 108.162.245.122 ( United States) United States)
- 🧍 172.71.148.156 ( Germany) Germany)
 
								 Singapore)
Singapore) United States)
United States) Germany)
Germany) France)
France) Netherlands)
Netherlands) Canada)
Canada)