Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // a function to check if the cart has product from SOME CATEGORY and it's subcategory id
- function cart_has_product() {
- //Check to see if user has product in cart
- global $woocommerce;
- //assigns a default negative value
- $product_in_cart = false;
- // start of the loop that fetches the cart items
- foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
- $_product = $values['data'];
- $terms = get_the_terms( $_product->id, 'product_cat' );
- // second level loop search, in case some items have several categories
- if($terms){
- foreach ($terms as $term) {
- $_categoryid = $term->term_id;
- //Add Cat Id here
- if (( $_categoryid === ( CAT_ID )|| $_categoryid === ( oTHER CAT_ID )) ) {
- //category is in cart!
- $product_in_cart = true;
- }
- }
- }
- }
- return $product_in_cart;
- }
- function cange_product_notices( ) {
- global $woocommerce;
- if( cart_has_product()){
- wc_print_notice( "CAN'T SEND BY MAIL THIS PRODUCT CATEGORY", $notice_type = 'error' );
- add_filter( 'woocommerce_shipping_methods', 'hide_shipping_method_for_CAT', 10, 2 );
- }
- }
- function hide_shipping_method_for_CAT( $rates, $package) {
- // HERE set your product categories in the array (IDs, slugs or names)
- $categories = array( CATEGORY NAME );
- $found = false;
- wp_die();
- // Loop through each cart item Checking for the defined product categories
- foreach( $package['contents'] as $cart_item ) {
- if ( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ){
- $found = true;
- break;
- }
- }
- $rates_arr = array();
- if ( $found ) {
- foreach($rates as $rate_id => $rate) {
- if ('free_shipping' === $rate->method_id) {
- $rates_arr[ $rate_id ] = $rate;
- }
- }
- }
- return !empty( $rates_arr ) ? $rates_arr : $rates;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement