Скрыть способы доставки по категориям товаров, а если нет - PullRequest
0 голосов
/ 20 апреля 2020

Я пытаюсь собрать функцию, чтобы скрыть способ доставки, если в корзине есть готовая категория товара, а если этой категории нет в корзине, то скрыть другую. Это функция, которую я смог собрать, но она не работает. Можете ли вы помочь мне, пожалуйста?

function product_category_find_id_in_cart() {
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ){

    if ( has_term( 'hangers', 'product_cat', $cart_item['product_id'] ) ) {

    unset( $rates['free_shipping1'] );

    }
    else {
      unset( $rates['flat_rate:6'] );
   }

   return $rates;

}
}

Мне нужно, чтобы он также работал для различных способов доставки, а не просто скрыть один. Спасибо!

Благодаря @ 7uc1f3r у меня есть новый код, который скрывает способ доставки, если товар относится к категории, но мне нужно, чтобы, если товар не принадлежал к этой категории, другие способы доставки были скрыты. Это будет новая формула, благодаря @ LoicTheAzte c:

add_filter( 'woocommerce_package_rates', 'specific_products_shipping_methods', 10, 2 );
function specific_products_shipping_methods( $rates, $package ) {
    // HERE set the product category in the array (ID, slug or name)
    $terms = array( 'hangers' ); 
    $taxonomy = 'product_cat'; 

    // HERE set the shipping methods to be removed (like "fat_rate:5")
    $method_instances_ids = array('1');  

    $found = false;

    // Loop through cart items checking for defined product IDs
    foreach( $package['contents'] as $cart_item ) {
        if ( has_term( $terms, $taxonomy, $cart_item['product_id'] ) ){
            $found = true;
            break;
        }
    }

    if ( ! $found ) return $rates; // If not found we exit

    // Loop through your active shipping methods
    foreach( $rates as $rate_id => $rate ) {
        // Remove all other shipping methods other than your defined shipping method
        if ( in_array( $rate_id, $method_instances_ids ) ){
            unset( $rates[$rate_id] );
        }
    }    

    return $rates;
}

1 Ответ

0 голосов
/ 21 апреля 2020

Я нашел это !!

add_filter( 'woocommerce_package_rates', 'specific_products_shipping_methods', 10, 2 );
function specific_products_shipping_methods( $rates, $package ) {
    // HERE set the product category in the array (ID, slug or name)
    $terms = array( 'hangers' ); 
    $taxonomy = 'product_cat'; 

    // HERE set the shipping methods to be removed (like "fat_rate:5")
    $method_instances_ids = array('flat_rate:12','flat_rate:13','flat_rate:14');  

    $found = false;

    // Loop through cart items checking for defined product IDs
    foreach( $package['contents'] as $cart_item ) {
        if ( has_term( $terms, $taxonomy, $cart_item['product_id'] ) ){
            $found = true;
            break;
        }
    }

    if ( ! $found ) { unset( $rates['flat_rate:6'],$rates['flat_rate:7'],$rates['flat_rate:8'],$rates['flat_rate:9'],$rates['flat_rate:10'],$rates['flat_rate:11'] ); return $rates;} // If not found we exit
    else {
    // Loop through your active shipping methods
    foreach( $rates as $rate_id => $rate ) {
        // Remove all other shipping methods other than your defined shipping method
        if ( in_array( $rate_id, $method_instances_ids ) ){
            unset( $rates[$rate_id] );
        }
    }    

    return $rates;
}}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...