admin管理员组

文章数量:1332889

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 4 years ago.

Improve this question

Can you please tell me how can I adapt this snippet (it disables the free shipping method for a certain product) to make it possible to disable the free shipping method when the cart has product/s which assigned a certain shipping class? (WooCommerce 4.2+)

Any help appreciated. Thanks.

function my_free_shipping( $is_available ) {
    global $woocommerce;

    // set the product ids that are ineligible
    $ineligible = array( '4743' );

    // get cart contents
    $cart_items = $woocommerce->cart->get_cart();
    
    // loop through the items looking for one in the ineligible array
    foreach ( $cart_items as $key => $item ) {
        if( in_array( $item['product_id'], $ineligible ) ) {
            return false;
        }
    }

    // nothing found return the default value
    return $is_available;
}
add_filter( 'woocommerce_shipping_free_shipping_is_available', 'my_free_shipping', 20 );  

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 4 years ago.

Improve this question

Can you please tell me how can I adapt this snippet (it disables the free shipping method for a certain product) to make it possible to disable the free shipping method when the cart has product/s which assigned a certain shipping class? (WooCommerce 4.2+)

Any help appreciated. Thanks.

function my_free_shipping( $is_available ) {
    global $woocommerce;

    // set the product ids that are ineligible
    $ineligible = array( '4743' );

    // get cart contents
    $cart_items = $woocommerce->cart->get_cart();
    
    // loop through the items looking for one in the ineligible array
    foreach ( $cart_items as $key => $item ) {
        if( in_array( $item['product_id'], $ineligible ) ) {
            return false;
        }
    }

    // nothing found return the default value
    return $is_available;
}
add_filter( 'woocommerce_shipping_free_shipping_is_available', 'my_free_shipping', 20 );  

Share Improve this question asked Jul 2, 2020 at 22:01 Ugur TerziUgur Terzi 357 bronze badges 7
  • WooCommerce is a 3rd party plugin, 3rd party plugin dev support is offtopic on this site. For help with WooCommerce you should contact Woo support or ask in WooCommerce communities and groups. If you were directed here for WooCommerce help let us know as that is a violation of Stack Exchange TOC's – Tom J Nowell Commented Jul 2, 2020 at 22:23
  • @TomJNowell as fas I know Woo is a plugin of WP. There are tons of woo related questions here in this forum and other plugins as well such as ACF etc. So, What is your problem with my question?? – Ugur Terzi Commented Jul 2, 2020 at 22:30
  • WooCommerce is owned by Automattic, a 3rd party company, it isn't 1st party. The only 1st party plugins are core feature plugins such as Hello Dolly, plugins for functionality to be merged into WP itself, like the sitemaps coming in 5.5. As an ex-Automattic employee, I understand that people can confuse wp the paid service, and wordpress the open source project. I'd also note this isn't a discussion forum. Your question on its own is fine, it just doesn't fit into the scope of this site. You should ask in a WooCommerce forum or group – Tom J Nowell Commented Jul 2, 2020 at 22:41
  • Take a look at wordpress/support/article/wordpress-vs-wordpress-com. As for other questions on this site about WooCommerce, that doesn't mean they're on topic. Moderation on this site is done by volunteers and we can't catch all questions. Also sometimes questions are related to but not about WooCommerce, and users can create tags so deleting the woocommerce tag just means it gets recreated. The description of the tag does say the subject is offtopic though, and directs users to wordpress/support/plugin/woocommerce – Tom J Nowell Commented Jul 2, 2020 at 22:43
  • @TomJNowell I understood your point but can you please search questions about ACF in here you can see the 2,545 results. Or just try woocommerce, the result is 3,769 questions. So, I really don't understand why you locked my question? You can notify me, I could understand that but there are thousands of questions here regarding 3rd party plugins. On the other hand, somebody already tried to help me, which means users don't get this question as you did. – Ugur Terzi Commented Jul 2, 2020 at 23:11
 |  Show 2 more comments

1 Answer 1

Reset to default 1

Try this:

function hide_shipping_methods( $available_shipping_methods, $package ) {
    $shipping_classes = array( 'some-shipping-class-1', 'some-shipping-class-2' );
    $excluded_methods = array( 'free_shipping' );
    $shipping_class_exists = false;
    foreach( $package['contents'] as $key => $value )
        if ( in_array( $value['data']->get_shipping_class(), $shipping_classes ) ) {
            $shipping_class_exists = true;
            break;
        }
    if ( $shipping_class_exists ) {
        $methods_to_exclude = array();
        foreach( $available_shipping_methods as $method => $method_obj )
            if ( in_array( $method_obj->method_id, $excluded_methods ) )
                $methods_to_exclude[] = $method;
        if ( $methods_to_exclude )
            foreach ( $methods_to_exclude as $method )
                unset( $available_shipping_methods[$method] );
    }
    return $available_shipping_methods;
}
add_filter( 'woocommerce_package_rates', 'hide_shipping_methods', 10, 2 );

Here $shipping_classes is array of the shipping classes slugs and $excluded_methods is array of excluded shipping methods if at least one of the products in the cart belongs to one of these shipping classes.

本文标签: