# makecommerce/3.0.2/shipping/method/method.php

MakeCommerce for WooCommerce, version 3.0.2. 439 lines.

- Page: https://pluginprobe.com/plugins/makecommerce/3.0.2/code/shipping/method/method.php
- Raw: https://pluginprobe.com/plugins/makecommerce/3.0.2/raw/shipping/method/method.php
- Modified: 2021-03-01T12:29:34+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/makecommerce/3.0.2/code/shipping/method/method.php#L10-L20`.

```php
<?php

namespace MakeCommerce\Shipping;

/**
 * Creates minimal functionality needed for WooCommerce shipping method
 * 
 * @since 3.0.0
 */

abstract class Method extends \WC_Shipping_Method {

    public $id;
    public $instance_id;

    public $supports = array(
        'shipping-zones',
        'instance-settings',
        'instance-settings-modal',
        'settings'
    );
    
    /**
     * Construct WooCommerce shipping method
     * 
     * @since 3.0.0
     */
    public function __construct( $instance_id = 0 ) {

        //set required properties
        $this->instance_id  = absint( $instance_id );
        $this->id           = $this->identifier . '_' . mb_strtolower( $this->carrier );
        $this->ext          = mb_strtolower( $this->carrier );
        $this->title        = $this->return_method_title();
        $this->method_title = $this->title;
        $this->name_ext     = $this->title;

        //get settings from WC_Shipping_Method
        $this->init_settings();

        //set default settings used by all shipping methods
        $this->set_default_settings();

        //initialize form fields for admin
        $this->initialize_form_fields();

        //set hooks
        $this->set_hooks();

        //initilize method type
        $this->initialize();
    }

    /**
     * Set hooks used by all WooCommerce shipping methods
     * 
     * @since 3.0.0
     */
    final private function set_hooks() {
        
        //update options
        add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
        
        // Woocommerce Multilingual overrides (change some titles)
        if ( \MakeCommerce\i18n::using_language_plugins() ) {
            add_filter( 'woocommerce_package_rates', array( $this, 'override_label_translation' ), 50 );
            add_filter( 'woocommerce_order_get_items', array( $this, 'override_shipping_title_translation' ), 50, 2 );
        }
    }

    /**
     * Label translation override for WPML (woocommerce_package_rates)
     * 
     * @since 3.0.0
     */
    public function override_label_translation( $rates ) {

        $id_instance = $this->id . ':' . $this->instance_id;
        
        if ( array_key_exists( $id_instance, $rates ) ) {
            $rates[$id_instance]->label = $this->title;
        }

        return $rates;
    }

    /**
     * Title translation override for WPML (woocommerce_order_get_items)
     * 
     * @since 3.0.0
     */
    public function override_shipping_title_translation( $items, $order ) {

        foreach ( $items as $key => $item ) {

            if ( $item['type'] == 'shipping' ) {
                foreach ( $item['item_meta_array'] as $meta ) {

                    if ( $meta->key === 'method_id' ) {

                        $tmp = explode( ':', $meta->value );
                        
                        if ( $tmp[0] == $this->id ) {
                            $items[$key]['name'] = $this->title;
                        }
                    }
                }
            }
        }

        return $items;
    }
    
    /**
     * Sets default settings used by all shipping methods
     * 
     * @since 3.0.0
     */
    final private function set_default_settings() {

        $this->enable                   = !empty( $this->settings['active'] ) ?  $this->settings['active'] : null;
        $this->availability             = 'specific';
        $this->order_country            = 'unknown';
        $this->maximum_weight           = !empty( $this->settings['maximum_weight']) ? ( double )$this->settings['maximum_weight'] : 0;
        $this->free_shipping_min_amount = !empty( $this->settings['free_shipping_min_amount'] ) ? $this->settings['free_shipping_min_amount'] : 0;
        $this->countries                = !empty( $this->settings['countries'] ) ? $this->settings['countries'] : array();

        //Overrides title if its set in settings already
        $this->override_title();
    }

    /**
     * Check if the shipping method is available (automatically called by parent \WC_Shipping_Method)
     * 
     * @since 3.0.0
     */
    public function is_available( $package ) {

        //check if cart weight exceeds max weight
        if ( WC()->cart->cart_contents_weight > $this->maximum_weight ) {
            return false;
        }

        $is_available = $this->enable === 'yes';

        if ( !$is_available || !\MakeCommerce::is_api_set() ) {
            return false;
        }

        $this->order_country = $package['destination']['country'];

        return apply_filters( 'woocommerce_shipping_' . $this->id . '_is_available', $is_available, $package );
    }

    /**
     * Calculate price for shipping
     * 
     * @since 3.0.0
     */
    public function calculate_shipping( $package = array() ) {

        $price = isset( $this->instance_settings['price'] ) ? $this->instance_settings['price'] : ( isset( $this->settings['price_'.$package['destination']['country']] ) ? $this->settings['price_'.$package['destination']['country']] : 0 );

        $free_shipping_min_amount = $this->instance_settings['free_shipping_min_amount'];

        if ( $free_shipping_min_amount && $package['contents_cost'] >= $free_shipping_min_amount ) {
            $price = 0;
        }

        //check only for parcelmachines
        if ( $this->type == "apt" ) {

            $free_shipping = true;
            foreach ( $package['contents'] as $line ) {
                
                $free_shipping = get_post_meta( $line['product_id'], '_no_shipping_cost', true ) === 'yes' ? $free_shipping : false;
            }

            if ( $free_shipping ) {
                $price = 0;
            }
        }

        //check if there is a free shipping coupon (if it's free then allow free shipping)
        if ( isset( $package['applied_coupons'] ) && $this->instance_settings["allow_free_shipping_coupons"] === "yes" ) {
            foreach ( $package['applied_coupons'] as $coupon_code ) {

                $coupon = new \WC_Coupon( $coupon_code );
                if ( $coupon->get_free_shipping() === true ) {
                    $price = 0;
                    break;
                }
            }
        }

        $rate = array(
            'id' 	=> $this->get_rate_id(),
            'label' => $this->title,
            'cost' 	=> $price,
            'package' => $package,
            'calc_tax' => 'per_order',
        );
        $this->add_rate( $rate );
    }
    
    /**
     * This function overrides shipping method title
     * if you have set language specific titles in admin.
     * 
     * @since 3.0.0
     */
    final private function override_title() {

        $language_code = \MakeCommerce\i18n::get_two_char_locale();

        //default (no language)
        if ( !empty( $this->settings['method_name'] ) ) {
            $this->title = $this->settings['method_name'];
        }

        //if method name setting has been set with current language code
        if ( !empty( $this->settings['method_name_' . $language_code] ) ) {
            $this->title = $this->settings['method_name_' . $language_code];
        }
    }

    /**
     * Initializes basic fields used by all shipping methods.
     * 
     * @since 3.0.0
     */
    public function initialize_form_fields() {

        //Initialize instance form fields first
        $this->initialize_instance_form_fields();

        //Initialize basic form fields
        $this->initialize_basic_form_fields();

        //Initialize method specific form fields
        $this->initialize_method_type_form_fields();

        //Initialize return address fields
        $this->initalize_return_addres_form_fields();
    }

    /**
     * Initializes basic shared form fields
     * Used by all shipping methods
     * 
     * since 3.0.0
     */
    final private function initialize_basic_form_fields() {

        $this->form_fields = array();

        $this->form_fields['logo'] = array(
                'type'          => 'title', 
                'description'   => \MakeCommerce::get_logo_html()
        );
        
        $this->form_fields['generic'] = array(
                'type' 			=> 'title', 
                'title'			=> __( 'Generic and pricing options', 'wc_makecommerce_domain' ),
        );

        $this->form_fields['active'] = array(
                'title'         => __( 'Enable', 'wc_makecommerce_domain' ),
                'type'          => 'checkbox',
                'label'         => __( 'enabled', 'wc_makecommerce_domain' ),
                'default'       => 'no',
        );

        $this->form_fields['maximum_weight'] = array(
                'title'         => sprintf( __( 'Maximum weight allowed for shipping (%s)', 'wc_makecommerce_domain' ), get_option( 'woocommerce_weight_unit' ) ),
                'type'          => 'text',
                'default'       => $this->default_max_weight
        );
        
        $this->form_fields['look_and_feel'] = array(
                'type' => 'title',
                'title' => '<hr><br>'.__( 'Look and feel options', 'wc_makecommerce_domain' ),
                'description' => __( 'Options for presentation on check-out page', 'wc_makecommerce_domain' )
        );

        //get a list of all active languages
        $languages = \MakeCommerce\i18n::get_active_languages();

        if ( empty( $languages ) ) {
            
            $this->form_fields['method_name'] = array(
                'title' => __( 'Shipping Method Title', 'wc_makecommerce_domain' ),
                'type' => 'text',
                'default' => $this->carrier . " " . \MakeCommerce\i18n::get_string_from_mo( $this->identifier, 'wc_makecommerce_domain', \MakeCommerce\i18n::get_two_char_locale() )
            );
        } else {
            foreach ( $languages as $language_code => $language ) {
                $this->form_fields['method_name_'.substr( $language_code, 0, 2 )] = array(
                    'title' => __('Shipping Method Title', 'wc_makecommerce_domain').sprintf(' (%s)', substr($language_code, 0, 2)),
                    'type' => 'text',
                    'default' => $this->carrier . " " . \MakeCommerce\i18n::get_string_from_mo( $this->identifier, 'wc_makecommerce_domain', substr( $language_code, 0, 2 ) )
                );
            }
        }

        $this->initialize_method_type_checkout_fields();

        if ( \MakeCommerce::new_woo_version() ) {
            $a = 'admin.php?page=wc-settings&tab=advanced&section=mk_api';
        } else {
            $a = 'admin.php?page=wc-settings&tab=api&section=mk_api';
        }

        $this->form_fields['api_access'] = array(
            'type' => 'title',
            'title' => '<hr><br>'.__('API access for', 'wc_makecommerce_domain').' '.$this->ext,
            'description' => sprintf(__('You can automatically create shipments into %s system and print the out the package labels right here, at the shop orders view. <br> Please set your %s web services account credentials below here. <br>(see more on <a href="https://makecommerce.net/en/integration-modules/makecommerce-woocommerce-payment-plugin/#carriers-integration">MakeCommerce plugin page</a>. Don\'t forget to enable also <a href="%s">MC API keys</a>!)', 'wc_makecommerce_domain' ), $this->carrier, $this->carrier, admin_url( $a ) )
        );
    }

    /**
     * Initializes basic instance fields used by all shipping methods.
     * Automatically called by \WC_Shipping_Method
     * 
     * @since 3.0.0
     */
    final private function initialize_instance_form_fields() {

        $this->instance_form_fields = array();
        
        $this->instance_form_fields['logo'] = array(
            'type'        => 'title',
            'description' => \MakeCommerce::get_logo_html()
        );
        
        $this->instance_form_fields['price'] = array(
            'title'       => __('Shipping price', 'wc_makecommerce_domain'),
            'type'        => 'text',
            'default'     => $this->default_price
        );
        
        $this->instance_form_fields['free_shipping_min_amount'] = array(
            'title'       => __('Free shipping amount', 'wc_makecommerce_domain'),
            'type'        => 'number',
            'default'     => '0',
            'desc_tip'    => __( '(0 means no free shipping)', 'wc_makecommerce_domain' ),
        );
        
        $this->instance_form_fields['allow_free_shipping_coupons'] = array(
            'title'       => __( 'Free shipping coupons', 'wc_makecommerce_domain' ),
            'type'        => 'checkbox',
            'default'     => 'no',
            'desc_tip'	  => __( 'Allow using free shipping coupons to be used with this method', 'wc_makecommerce_domain' ),
        );
    }

    /**
     * Initializes return address form fields
     * Used by all shipping methods
     * 
     * @since 3.0.0
     */
    final private function initalize_return_addres_form_fields() {

        $this->form_fields['return_address'] = array(
            'type' => 'title',
            'title' => __( 'Return address', 'wc_makecommerce_domain' ),
            'description' => __( 'Please define return address for Omniva shipments', 'wc_makecommerce_domain' )
        );
        
        $this->form_fields['shop_name'] = array(
            'type' => 'text',
            'title' => __( 'Shop name', 'wc_makecommerce_domain' ),
            'class' => 'input-text regular-input',
        );
        
        $this->form_fields['shop_phone'] = array(
            'type' => 'text',
            'title' => __( 'Shop phone', 'wc_makecommerce_domain' ),
            'class' => 'input-text regular-input',
        );
        
        $this->form_fields['shop_email'] = array(
            'type' => 'text',
            'title' => __( 'Shop email', 'wc_makecommerce_domain' ),
            'class' => 'input-text regular-input',
        );
        
        $this->form_fields['shop_address_country'] = array(
            'type' => 'select',
            'title' => __( 'Shop address country', 'wc_makecommerce_domain' ),
            'options' => array(
                'EE' => 'EE',
                'LV' => 'LV',
                'LT' => 'LT',
            ),
            'default' => 'EE',
        );
        
        $this->form_fields['shop_address_city'] = array(
            'type' => 'text',
            'title' => __( 'Shop address city', 'wc_makecommerce_domain' ),
            'class' => 'input-text regular-input',
        );					
        
        $this->form_fields['shop_address_street'] = array(
            'type' => 'text',
            'title' => __( 'Shop address street', 'wc_makecommerce_domain' ),
            'class' => 'input-text regular-input',
        );					
        
        $this->form_fields['shop_postal_code'] = array(
            'type' => 'text',
            'title' => __( 'Shop postal code', 'wc_makecommerce_domain' ),
            'class' => 'input-text regular-input',
        );
    }

    /**
     * Initializes method type, required function for all method types
     * 
     * @since 3.0.0
     */
    abstract public function initialize();

    /**
     * Initializes method type form fields
     * 
     * @since 3.0.0
     */
    abstract public function initialize_method_type_form_fields();

    /**
     * Sets title of the method
     * 
     * @since 3.0.0
     */
    abstract public function return_method_title();
}
```
