Custom Woocommerce Fields for Delivery Calculator

I am creating a custom delivery method for Woocommerce, and one thing I am completely hung up on is to pass the custom values ​​to the calculate_shipping () function when it is used on the Cart page or on the Checkout page.

I need to pass in a few user-defined variables that will affect the quote, that is, β€œIs the address live,” β€œIs the exhibition,” etc. etc.

calculate_shipping gets the $ package array, which contains the 'destination' array, but this only includes the standard data add1, add2, city, state, zip, country. I have added custom fields to the verification page for both billing and delivery, but I still cannot figure out how to make these values ​​available to the calculate_shipping function.

I added a custom field, for example:

add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );

// Our hooked in function - $fields is passed via the filter!
function custom_override_checkout_fields( $fields ) {
  $fields['shipping']['is_residential'] = array(
    'label'     => __('Residential Address?', 'woocommerce'),
 'type'      => 'checkbox',
 'required'  => false,
 'class'     => array('form-row-wide'),
 'clear'     => true
  );

  return $fields;
}

And I see that this field is displayed in the "Delivery" section of the statement form. However, I do not see how I can access it anywhere. Even the execution print_r($_POST)on the validation page does not display this field as part of the message data, even after I know that the form has been updated and resubmitted.

, $package, Woocommerce calculate_shipping().

, .

+4
1

.

- .

function get_shipping_packages() class-wc-cart.php

public function get_shipping_packages() {
        // Packages array for storing 'carts'
        $packages = array();

        $packages[0]['contents']                 = $this->get_cart();       // Items in the package
        $packages[0]['contents_cost']            = 0;                       // Cost of items in the package, set below
        $packages[0]['applied_coupons']          = $this->applied_coupons;
        $packages[0]['destination']['country']   = WC()->customer->get_shipping_country();
        $packages[0]['destination']['state']     = WC()->customer->get_shipping_state();
        $packages[0]['destination']['postcode']  = WC()->customer->get_shipping_postcode();
        $packages[0]['destination']['city']      = WC()->customer->get_shipping_city();
        $packages[0]['destination']['address']   = WC()->customer->get_shipping_address();
        $packages[0]['destination']['address_2'] = WC()->customer->get_shipping_address_2();

        foreach ( $this->get_cart() as $item )
            if ( $item['data']->needs_shipping() )
                if ( isset( $item['line_total'] ) )
                    $packages[0]['contents_cost'] += $item['line_total'];

        return apply_filters( 'woocommerce_cart_shipping_packages', $packages );
    }

woocommerce_cart_shipping_packages .

, ( ) .

, .

+2

Source: https://habr.com/ru/post/1527037/


All Articles