English 中文(简体)
Remove shipping for specific products in WooCommerce
原标题:

I am looking to remove the shipping from few variable products in WooCommerce. I need that few products from cart directly go to PayPal.

I added the following code in my theme s functions.php file:

add_filter(  woocommerce_checkout_fields ,  disable_address_validation_for_specific_products  );
function disable_address_validation_for_specific_products( $fields ) {
    // Enter the product IDs of the variable products for which you want to disable address validation
    $product_ids = array(12, 34, 56);

    if (!empty(WC()->cart)) {
        foreach (WC()->cart->get_cart() as $cart_item) {
            $product_id = $cart_item[ product_id ];

            if (in_array($product_id, $product_ids)) {
                unset($fields[ billing ][ billing_address_1 ][ validate ]);
                unset($fields[ billing ][ billing_city ][ validate ]);
                unset($fields[ billing ][ billing_postcode ][ validate ]);
                unset($fields[ billing ][ billing_country ][ validate ]);
                unset($fields[ billing ][ billing_state ][ validate ]);
                break; // Stop the loop after finding the first matching product
            }
        }
    }

    return $fields;
}
 
add_filter(  woocommerce_cart_needs_shipping_address ,  __return_false  );

Now, for all products, shipping is disabled, what I don t want.

Also, I get those errors on the checkout page:

  • Error# 1 No shipping method has been selected. Please double-check your address, or contact us if you need any help.
  • Error # 2 Please enter an address to continue.

What I am doing wrong? How can I fix this?

问题回答

There are some mistakes a and missing things to get that working… Try the following instead:

// Custom conditional function that check for specific cart items
function has_unshippable_cart_items(){
    // Add the product IDs that will disable address validation
    $product_ids = array(12, 34, 56);

    // Loop through cart items
    foreach (WC()->cart->get_cart() as $item) {
        if ( count( array_intersect([$item[ product_id ], $item[ variation_id ]], $product_ids) ) > 0 ) {
            return true;
        }
    } 
    return false; 
}

// Acting on checkout billing fields
add_filter(  woocommerce_billing_fields ,  disable_address_validation_for_specific_products  );
function disable_address_validation_for_specific_products( $billing_fields ) {
    // Only on checkout page
    if ( is_checkout() && ! is_wc_endpoint_url() ) {
        // The related targeted billing fields
        $field_keys = [ address_1 ,  city ,  postcode ,  country ,  state ];

        if (has_unshippable_cart_items()) {
            // Loop through the field keys array
            foreach ($field_keys as $field_key) {
                $billing_fields["billing_{$field_key}"][ required ] = false;
                unset($billing_fields["billing_{$field_key}"][ validate ]);
            }
        }
    }
    return $billing_fields;
}
 
add_filter(  woocommerce_cart_needs_shipping_address ,  filter_cart_needs_shipping_address  );
function filter_cart_needs_shipping_address( $needs_shipping ) {
    return has_unshippable_cart_items() ? false : $needs_shipping;
}

Tested and works.

Code goes in the functions.php file of your child theme, or in a plugin file.





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签