admin管理员组

文章数量:1300047

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 3 years ago.

Improve this question

Looking to adapt the following code, so currently this adds 1% to the basket if the country is US

I need £35 amount not a percentage and to only apply with Country UK and postcode BT* (all BT postcodes)

add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge() {
  global $woocommerce;
  
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;
 
    $county     = array('US');
    $percentage     = 0.01;
 
    if ( in_array( $woocommerce->customer->get_shipping_country(), $county ) ) :
        $surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;
        $woocommerce->cart->add_fee( 'Surcharge', $surcharge, true, '' );
    endif;
  
}
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 3 years ago.

Improve this question

Looking to adapt the following code, so currently this adds 1% to the basket if the country is US

I need £35 amount not a percentage and to only apply with Country UK and postcode BT* (all BT postcodes)

add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge() {
  global $woocommerce;
  
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;
 
    $county     = array('US');
    $percentage     = 0.01;
 
    if ( in_array( $woocommerce->customer->get_shipping_country(), $county ) ) :
        $surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;
        $woocommerce->cart->add_fee( 'Surcharge', $surcharge, true, '' );
    endif;
  
}
Share Improve this question edited Mar 8, 2021 at 12:46 fuxia 107k38 gold badges255 silver badges459 bronze badges asked Mar 8, 2021 at 12:14 user3593556user3593556 132 bronze badges 2
  • This is a fairly simple change to the logic. What specifically don't you understand, or need help with? – Rup Commented Mar 8, 2021 at 13:25
  • WooCommerce and other 3rd party plugins are off topic and not in this stacks scope. You should ask via their official support routes or in their communities. As a side-note, where you directed here by their support? – Tom J Nowell Commented Mar 25, 2021 at 15:39
Add a comment  | 

1 Answer 1

Reset to default 0

Here's an untested modified version of your code:

function woocommerce_bt_postcode_surcharge() {
    global $woocommerce;

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( $woocommerce->customer->get_shipping_country() === 'GB' ) {
        $postcode = $woocommerce->customer->get_shipping_postcode();
        if ( isset( $postcode )
             && strtoupper( substr( trim( $postcode ), 0, 2 ) ) === 'BT' ) {
            $woocommerce->cart->add_fee( 'Surcharge', 35, true, '' );
        }
    }
}
add_action( 'woocommerce_cart_calculate_fees', 'woocommerce_bt_postcode_surcharge' );

Note that

  • I've copied your use of the $woocommerce global, but if I was writing this myself I'd use WC() instead
  • I've also copied the true flag from the add_fee line, which means that the surcharge is taxable. Is that what you meant? See the add_fee documentation.
  • and if I were a customer I'd probably appreciate a better explanation than just 'Surcharge', since this seems a rather large amount (but then I don't know what you're selling or why you're charging this)
  • I also don't know how or when this event gets called. If it can get called multiple times for the same cart then you might need to
    • check whether or not the fee already existed before adding it again, although it's also possible that the cart itself will deduplicate fees
    • spot that you've added the fee but the customer has now changed their delivery address so that it's no longer BT, and remove the fee.

And here's a version extended to cover more postcodes:

function woocommerce_bt_postcode_surcharge() {
    global $woocommerce;

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( $woocommerce->customer->get_shipping_country() === 'GB' ) {
        $postcode = $woocommerce->customer->get_shipping_postcode();
        if ( isset( $postcode ) ) {
            $prefix = strtolower( substr ( trim ( $postcode ), 0, 2 ) );
            // 35 surcharge for BT or IV
            if ( $prefix === 'BT' || $prefix === 'IV' ) {
                $woocommerce->cart->add_fee( 'Surcharge', 35, true, '' );
            }
            // 50 surcharge for RG
            if ( $prefix === 'RG' ) {
                $woocommerce->cart->add_fee( 'Surcharge', 50, true, '' );
            }
        }
    }
}
add_action( 'woocommerce_cart_calculate_fees', 'woocommerce_bt_postcode_surcharge' );

本文标签: woocommerce offtopicAdapt shipping surcharge code