woocommerce/includes/abstracts/abstract-wc-shipping-method...

512 lines
15 KiB
PHP
Raw Normal View History

2011-08-09 15:16:18 +00:00
<?php
2013-02-20 17:14:46 +00:00
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
2013-02-20 17:14:46 +00:00
2011-08-10 17:11:11 +00:00
/**
* WooCommerce Shipping Method Class.
2012-08-12 14:12:52 +00:00
*
2011-08-10 17:11:11 +00:00
* Extended by shipping methods to handle shipping calculations etc.
*
* @class WC_Shipping_Method
2017-03-15 16:36:53 +00:00
* @version 3.0.0
* @package WooCommerce/Abstracts
* @category Abstract Class
* @author WooThemes
2012-08-12 14:12:52 +00:00
*/
abstract class WC_Shipping_Method extends WC_Settings_API {
2012-08-12 14:12:52 +00:00
/**
* Features this method supports. Possible features used by core:
2015-12-16 13:52:46 +00:00
* - shipping-zones Shipping zone functionality + instances
* - instance-settings Instance settings screens.
* - settings Non-instance settings screens. Enabled by default for BW compatibility with methods before instances existed.
2016-03-24 18:31:39 +00:00
* - instance-settings-modal Allows the instance settings to be loaded within a modal in the zones UI.
* @var array
*/
2016-02-10 13:21:16 +00:00
public $supports = array( 'settings' );
2012-08-14 22:43:48 +00:00
/**
* Unique ID for the shipping method - must be set.
* @var string
*/
2016-02-10 13:21:16 +00:00
public $id = '';
2012-08-14 22:43:48 +00:00
/**
* Method title.
* @var string
*/
2016-02-10 13:21:16 +00:00
public $method_title = '';
/**
* Method description.
* @var string
*/
2016-02-10 13:21:16 +00:00
public $method_description = '';
/**
* yes or no based on whether the method is enabled.
* @var string
*/
2016-02-10 13:21:16 +00:00
public $enabled = 'yes';
2015-12-14 14:03:46 +00:00
/**
* Shipping method title for the frontend.
* @var string
*/
public $title;
2012-08-12 14:12:52 +00:00
2015-12-14 16:56:39 +00:00
/**
* This is an array of rates - methods must populate this array to register shipping costs.
* @var array
*/
2016-02-10 13:21:16 +00:00
public $rates = array();
2012-08-12 14:12:52 +00:00
2015-12-14 16:56:39 +00:00
/**
* If 'taxable' tax will be charged for this method (if applicable).
* @var string
*/
2016-02-10 13:21:16 +00:00
public $tax_status = 'taxable';
2012-08-12 14:12:52 +00:00
2015-12-14 16:56:39 +00:00
/**
* Fee for the method (if applicable).
* @var string
*/
2016-02-10 13:21:16 +00:00
public $fee = null;
2012-08-14 22:43:48 +00:00
2015-12-14 16:56:39 +00:00
/**
* Minimum fee for the method (if applicable).
* @var string
*/
2016-02-10 13:21:16 +00:00
public $minimum_fee = null;
2012-08-14 22:43:48 +00:00
2015-12-14 16:56:39 +00:00
/**
* Instance ID if used.
* @var int
*/
2016-02-10 13:21:16 +00:00
public $instance_id = 0;
2015-12-16 13:52:46 +00:00
/**
* Instance form fields.
* @var array
*/
public $instance_form_fields = array();
2015-12-14 16:56:39 +00:00
/**
* Instance settings.
2015-12-14 14:03:46 +00:00
* @var array
*/
2016-02-10 13:21:16 +00:00
public $instance_settings = array();
2015-12-18 13:12:20 +00:00
/**
* Availability - legacy. Used for method Availability.
* No longer useful for instance based shipping methods.
* @deprecated 2.6.0
* @var string
*/
public $availability;
/**
* Availability countries - legacy. Used for method Availability.
* No longer useful for instance based shipping methods.
* @deprecated 2.6.0
2016-02-10 13:21:16 +00:00
* @var array
2015-12-18 13:12:20 +00:00
*/
2016-02-10 13:21:16 +00:00
public $countries = array();
2015-12-18 13:12:20 +00:00
/**
* Constructor.
2015-12-14 16:56:39 +00:00
* @param int $instance_id
*/
public function __construct( $instance_id = 0 ) {
$this->instance_id = absint( $instance_id );
}
2012-08-12 14:12:52 +00:00
/**
* Check if a shipping method supports a given feature.
*
* Methods should override this to declare support (or lack of support) for a feature.
*
* @param $feature string The name of a feature to test support for.
* @return bool True if the shipping method supports the feature, false otherwise.
*/
public function supports( $feature ) {
return apply_filters( 'woocommerce_shipping_method_supports', in_array( $feature, $this->supports ), $feature, $this );
}
/**
* Called to calculate shipping rates for this method. Rates can be added using the add_rate() method.
*
* @param array $package
*/
2015-12-18 17:10:58 +00:00
public function calculate_shipping( $package = array() ) {}
/**
* Whether or not we need to calculate tax on top of the shipping rate.
* @return boolean
*/
public function is_taxable() {
return wc_tax_enabled() && 'taxable' === $this->tax_status && ! WC()->customer->get_is_vat_exempt();
}
2015-12-14 16:56:39 +00:00
/**
* Whether or not this method is enabled in settings.
* @since 2.6.0
* @return boolean
*/
public function is_enabled() {
return 'yes' === $this->enabled;
}
2015-12-15 15:00:45 +00:00
/**
* Return the shipping method instance ID.
* @since 2.6.0
* @return int
*/
public function get_instance_id() {
return $this->instance_id;
}
/**
* Return the shipping method title.
* @since 2.6.0
* @return string
*/
public function get_method_title() {
return apply_filters( 'woocommerce_shipping_method_title', $this->method_title, $this );
}
/**
* Return the shipping method description.
* @since 2.6.0
* @return string
*/
public function get_method_description() {
return apply_filters( 'woocommerce_shipping_method_description', $this->method_description, $this );
}
/**
* Return the shipping title which is user set.
*
* @return string
*/
public function get_title() {
return apply_filters( 'woocommerce_shipping_method_title', $this->title, $this->id );
}
/**
* Return calculated rates for a package.
* @since 2.6.0
* @param object $package
* @return array
*/
public function get_rates_for_package( $package ) {
$this->rates = array();
if ( $this->is_available( $package ) && ( empty( $package['ship_via'] ) || in_array( $this->id, $package['ship_via'] ) ) ) {
$this->calculate_shipping( $package );
}
return $this->rates;
}
/**
* Returns a rate ID based on this methods ID and instance, with an optional
* suffix if distinguishing between multiple rates.
* @since 2.6.0
* @param string $suffix
* @return string
*/
public function get_rate_id( $suffix = '' ) {
$rate_id = array( $this->id );
if ( $this->instance_id ) {
$rate_id[] = $this->instance_id;
}
if ( $suffix ) {
$rate_id[] = $suffix;
}
return implode( ':', $rate_id );
}
/**
2012-08-14 22:43:48 +00:00
* Add a shipping rate. If taxes are not set they will be calculated based on cost.
* @param array $args (default: array())
*/
public function add_rate( $args = array() ) {
2015-09-07 15:02:23 +00:00
$args = wp_parse_args( $args, array(
'id' => $this->get_rate_id(), // ID for the rate. If not passed, this id:instance default will be used.
2016-01-08 11:42:32 +00:00
'label' => '', // Label for the rate
'cost' => '0', // Amount or array of costs (per item shipping)
'taxes' => '', // Pass taxes, or leave empty to have it calculated for you, or 'false' to disable calculations
'calc_tax' => 'per_order', // Calc tax per_order or per_item. Per item needs an array of costs
2016-04-20 14:02:30 +00:00
'meta_data' => array(), // Array of misc meta data to store along with this rate - key value pairs.
'package' => false, // Package array this rate was generated for @since 2.6.0
2015-09-07 15:02:23 +00:00
) );
2012-08-12 14:12:52 +00:00
2015-12-14 16:56:39 +00:00
// ID and label are required
2015-09-07 15:02:23 +00:00
if ( ! $args['id'] || ! $args['label'] ) {
2014-11-17 16:30:15 +00:00
return;
}
2012-08-12 14:12:52 +00:00
// Total up the cost
2015-09-07 15:02:23 +00:00
$total_cost = is_array( $args['cost'] ) ? array_sum( $args['cost'] ) : $args['cost'];
$taxes = $args['taxes'];
2012-08-12 14:12:52 +00:00
2015-12-18 13:12:20 +00:00
// Taxes - if not an array and not set to false, calc tax based on cost and passed calc_tax variable. This saves shipping methods having to do complex tax calculations.
2016-09-07 22:32:24 +00:00
if ( ! is_array( $taxes ) && false !== $taxes && $total_cost > 0 && $this->is_taxable() ) {
2015-12-18 13:12:20 +00:00
$taxes = 'per_item' === $args['calc_tax'] ? $this->get_taxes_per_item( $args['cost'] ) : WC_Tax::calc_shipping_tax( $total_cost, WC_Tax::get_shipping_tax_rates() );
2015-12-14 16:56:39 +00:00
}
// Round the total cost after taxes have been calculated.
$total_cost = wc_format_decimal( $total_cost, wc_get_price_decimals() );
2012-08-12 14:12:52 +00:00
2016-01-08 11:42:32 +00:00
// Create rate object
$rate = new WC_Shipping_Rate( $args['id'], $args['label'], $total_cost, $taxes, $this->id );
if ( ! empty( $args['meta_data'] ) ) {
foreach ( $args['meta_data'] as $key => $value ) {
$rate->add_meta_data( $key, $value );
}
}
// Store package data
2016-04-20 14:02:30 +00:00
if ( $args['package'] ) {
$items_in_package = array();
2016-04-20 14:02:30 +00:00
foreach ( $args['package']['contents'] as $item ) {
$product = $item['data'];
WIP - Product CRUD (#12065) * Created function to get the catalog visibility options * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * Hardcode the get_type per product class * Initial look through getters and setters and abstract data * Missing var * Add related product functions and deprecate those in class. * No need to exclude ID * Fixed coding standards and improved the docblocks * Get cached terms from wc_get_related_terms() * Fixed wrong variable in wc_get_related_terms * Use count() instead of sizeof() * Sanitize ids later * Remove unneeded comments * wc_get_product_term_ids instead of related wording and use in other places. get_the_terms is used here and also handles caching, something wp_get_post_terms does not. * Clean up the abstract product class a bit, deprecate two functions we have renamed, make update & create work properly, and add some tests for it. * Bump template version * Handle PR feedback: Remove duplicate regular_price update, allow changing of post status for products, remove deprecation for get_title since we might still offer it as a function * Made abstract function useful * External Product CRUD * _virtual meta should be 'no', not taxable, in product unit test helper * Grouped product class * Tests * Move children to meta and update test * Use get_upsell_ids * Spacing in query * Moving and refactoring methods * Availability html * Tidy/add todos * Rename method * Put back review functions (still todo) * missing $this * get_price_including_tax/excluding_tax functions * wc_get_price_to_display * Price handling * [Product CRUD] Variable (#12146) * [Product CRUD] Variable Products * Handle PR feedback. * [Product CRUD] Grouped Handling (#12151) * Handle grouped product saving * Update routine * [Product CRUD] Product crud terms (#12149) * Category and tag id handling * Replace template functions * Remove todo * Handle default name in save function * Product crud admin save routine (#12174) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Spacing * Fix comment * wc_implode_text_attributes helper function * [Product CRUD] Product crud admin use getters (#12196) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Move settings into new files * Refactor panels and use getters * Use getters for variation panel * Revert save variation changes for now * Add todos * Fix downloads * REST API CRUD Updates * Additional API updates/fixes. Added some todos * Fix final failing tests and implementing setters/getters and attributes functionality. * Fix comparison for is_on_sale and remove download_type from WC_Product. * Add a wc_get_products wrapper. * Remove the download type input from the product data metabox for downloadable products. (#12221) * [Product CRUD] Variations - setters, getters and admin. (#12228) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * Feedback fixes * Implement CRUD in the legacy REST API * Handle PR feedback * [Product CRUD] Getter setter proxy methods (#12236) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * get_prop implementation in abstract and data classes * Implement set_prop * Change handling * Array key exists * set_object_read * Use get_the_terms() instead of wp_get_post_terms() wp_get_post_terms() is a wrapper around wp_get_object_terms() which does not use the object cache, and generates a database query every time it is used. get_the_terms() however can use data from the object cache if present. * Allow WP_Query to preload post data, and meta in wc_get_products() Allow WP_Query to bulk query for post data and meta if more than just IDs are requested from wc_get_products(). Reduces query count significantly. * [Product CRUD] Variable, variation, notices, and stock handling (#12277) * No longer needed * Remove old todos * Use getters in admin list * Related and upsells update for CRUD * Fix notice in gallery * Variable fixes and todos * Context * Price sync * Revert variation attributes change * Return parent data in view context * Defer term counting * wc_find_matching_product_variation * Stock manage tweaks * Stock fixes * Correct id * correct id * Better sync * Data logic setter fix * feedback * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * Hardcode the get_type per product class * Initial look through getters and setters and abstract data * Missing var * Fixed coding standards and improved the docblocks * Get cached terms from wc_get_related_terms() * Fixed wrong variable in wc_get_related_terms * Use count() instead of sizeof() * Add related product functions and deprecate those in class. * No need to exclude ID * Sanitize ids later * Clean up the abstract product class a bit, deprecate two functions we have renamed, make update & create work properly, and add some tests for it. * Remove unneeded comments * wc_get_product_term_ids instead of related wording and use in other places. get_the_terms is used here and also handles caching, something wp_get_post_terms does not. * Handle PR feedback: Remove duplicate regular_price update, allow changing of post status for products, remove deprecation for get_title since we might still offer it as a function * External Product CRUD * _virtual meta should be 'no', not taxable, in product unit test helper * Bump template version * Made abstract function useful * Grouped product class * Tests * Move children to meta and update test * Use get_upsell_ids * Spacing in query * Moving and refactoring methods * Availability html * Tidy/add todos * Rename method * Put back review functions (still todo) * missing $this * get_price_including_tax/excluding_tax functions * wc_get_price_to_display * Price handling * [Product CRUD] Variable (#12146) * [Product CRUD] Variable Products * Handle PR feedback. * [Product CRUD] Grouped Handling (#12151) * Handle grouped product saving * Update routine * [Product CRUD] Product crud terms (#12149) * Category and tag id handling * Replace template functions * Remove todo * Handle default name in save function * Product crud admin save routine (#12174) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Spacing * Fix comment * wc_implode_text_attributes helper function * [Product CRUD] Product crud admin use getters (#12196) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Move settings into new files * Refactor panels and use getters * Use getters for variation panel * Revert save variation changes for now * Add todos * Fix downloads * REST API CRUD Updates * Additional API updates/fixes. Added some todos * Fix final failing tests and implementing setters/getters and attributes functionality. * Fix comparison for is_on_sale and remove download_type from WC_Product. * Add a wc_get_products wrapper. * Remove the download type input from the product data metabox for downloadable products. (#12221) * [Product CRUD] Variations - setters, getters and admin. (#12228) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * Feedback fixes * Implement CRUD in the legacy REST API * Handle PR feedback * [Product CRUD] Getter setter proxy methods (#12236) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * get_prop implementation in abstract and data classes * Implement set_prop * Change handling * Array key exists * set_object_read * Use get_the_terms() instead of wp_get_post_terms() wp_get_post_terms() is a wrapper around wp_get_object_terms() which does not use the object cache, and generates a database query every time it is used. get_the_terms() however can use data from the object cache if present. * [Product CRUD] Variable, variation, notices, and stock handling (#12277) * No longer needed * Remove old todos * Use getters in admin list * Related and upsells update for CRUD * Fix notice in gallery * Variable fixes and todos * Context * Price sync * Revert variation attributes change * Return parent data in view context * Defer term counting * wc_find_matching_product_variation * Stock manage tweaks * Stock fixes * Correct id * correct id * Better sync * Data logic setter fix * feedback * Prevent notices * Handle image_id from parent * Fix error * Remove _wc_save_product_price * Remove todo * Fixed wrong variation URLs * Fixed undefined $image_id in WC_Product_Variation::get_image_id() * Allow wc_rest_prepare_date_response() handle timestamps * Updated get methods on REST API for variations * Use variations CRUD to save variations metadata * [Product CRUD] Abstract todos (#12305) * Get dimensions and weights, with soft deprecation * Product attributes * Ratings * Fix read method * Downloads * Feedback * Revert "[Product CRUD] Abstract todos (#12305)" This reverts commit 9a6136fcf88fec16f97457b7c8a4388f7587bfa2. * Remove deprecated get_variation_id() * New default attributes method * [Product CRUD] Product Datastore (#12317) * Fix up tests in the product/* folder. * Handle data store updates for grouped, variable, external, simple, and general data store updates for products. * Variations & variable changes. * Update -functions.php calls to use data store. * Add an interface for the public product data store methods. * Finished product factory tests * Correctly delete in the api, fix up some comments, and implement an interface for the public variable methods. * Fix up delete in all versions of the api * Handle feedback * Match protected decloration to parent * Product crud abstract todos (#12316) * Get dimensions and weights, with soft deprecation * Product attributes * Ratings * Fix read method * Downloads * Feedback * Fix up store * Fixed method returning in write context * Fix error in variation admin * Check for parent value - fixes tax class * Remove old/complete todos * Allow set tax class as "parent" * Removed duplicated sync * Fixed wrong variation URLs * Fixed undefined $image_id in WC_Product_Variation::get_image_id() * Allow wc_rest_prepare_date_response() handle timestamps * Updated get methods on REST API for variations * Use variations CRUD to save variations metadata * Remove deprecated get_variation_id() * New default attributes method * Fixed method returning in write context * Allow set tax class as "parent" * Removed duplicated sync * Fixed coding standards * TODO is not accurate. * Should pass WC_Product instancies to WC_Comments methods (#12327) * Use new method in abstract order class to prevent headers sent issue in tests * Fixed variable description in REST API * Updated how create initial product variation * Fixed a few fatal errors and warnings in Products CRUD (#12329) * Fixed a few fatal errors and warnings in Products CRUD * Fixed sync functions * Add variations CRUD to legacy API (#12331) * Apply crud to variable products in legacy API v1 * New REST API do not need fallback for default attributes * Apply variations CRUD to legacy API v2 * Legacy v2 - save default attributes * Variations in legacy API v2 do not have descriptions * Fixed legacy API v2 variations params * Applied variations CRUD to legacy API v3 * Sync before save in legacy apis * Punc * Removed API todos * Removed test * Products endpoint tweaks (#12354) * Var type already normalized on CRUD * Let Product CRUD handle with validation, sanitization and conditional checks * Set downloads using WC_Product_Download * Stop try catch exceptions more than one time * Handle WC_Data_Exception in legacy API * Complete remove products when fails on creating * On creating I mean! * Already have a method to complete delete products * Fixed standards using WP CodeSniffer * get_the_terms() returns false when empty * get_manage_stock returns boolean @claudiosanches * Merge conflict * Variations API endpoint fixes * Product CRUD improvements (#12359) * args is not used any more - remove todo * Added test for attributes * wc_get_price_excluding_tax usage * parent usage * Fix rating counts * Test fixes * Cleanup after tests * Make sure status transition code runs even during API calls, not just in admin. * Default visibility * Fix attribute setting in API * Use get name instead of get title * variation id usage * Improved cross sell templates * variation_data * Grouped product sync * Notices * Sync is not needed in API * Delete * Rename interfaces * Update counts in data store
2016-11-16 12:38:24 +00:00
$items_in_package[] = $product->get_name() . ' &times; ' . $item['quantity'];
}
$rate->add_meta_data( __( 'Items', 'woocommerce' ), implode( ', ', $items_in_package ) );
}
2016-01-08 11:42:32 +00:00
$this->rates[ $args['id'] ] = $rate;
2015-12-14 16:56:39 +00:00
}
2012-08-12 14:12:52 +00:00
2015-12-14 16:56:39 +00:00
/**
* Calc taxes per item being shipping in costs array.
* @since 2.6.0
* @access protected
* @param array $costs
* @return array of taxes
*/
protected function get_taxes_per_item( $costs ) {
$taxes = array();
2012-08-12 14:12:52 +00:00
2015-12-14 16:56:39 +00:00
// If we have an array of costs we can look up each items tax class and add tax accordingly
if ( is_array( $costs ) ) {
2012-08-12 14:12:52 +00:00
2015-12-14 16:56:39 +00:00
$cart = WC()->cart->get_cart();
2012-08-12 14:12:52 +00:00
2015-12-14 16:56:39 +00:00
foreach ( $costs as $cost_key => $amount ) {
if ( ! isset( $cart[ $cost_key ] ) ) {
continue;
}
2012-08-12 14:12:52 +00:00
2015-12-14 16:56:39 +00:00
$item_taxes = WC_Tax::calc_shipping_tax( $amount, WC_Tax::get_shipping_tax_rates( $cart[ $cost_key ]['data']->get_tax_class() ) );
2012-08-12 14:12:52 +00:00
2015-12-14 16:56:39 +00:00
// Sum the item taxes
foreach ( array_keys( $taxes + $item_taxes ) as $key ) {
$taxes[ $key ] = ( isset( $item_taxes[ $key ] ) ? $item_taxes[ $key ] : 0 ) + ( isset( $taxes[ $key ] ) ? $taxes[ $key ] : 0 );
}
}
// Add any cost for the order - order costs are in the key 'order'
if ( isset( $costs['order'] ) ) {
$item_taxes = WC_Tax::calc_shipping_tax( $costs['order'], WC_Tax::get_shipping_tax_rates() );
// Sum the item taxes
foreach ( array_keys( $taxes + $item_taxes ) as $key ) {
$taxes[ $key ] = ( isset( $item_taxes[ $key ] ) ? $item_taxes[ $key ] : 0 ) + ( isset( $taxes[ $key ] ) ? $taxes[ $key ] : 0 );
}
2012-05-29 12:32:54 +00:00
}
}
2015-12-14 16:56:39 +00:00
return $taxes;
}
2012-08-12 14:12:52 +00:00
/**
2015-12-18 13:12:20 +00:00
* Is this method available?
* @param array $package
* @return bool
*/
public function is_available( $package ) {
2015-12-18 13:12:20 +00:00
$available = $this->is_enabled();
2012-08-12 14:12:52 +00:00
2015-12-18 13:12:20 +00:00
// Country availability (legacy, for non-zone based methods)
2016-06-17 15:20:44 +00:00
if ( ! $this->instance_id && $available ) {
2015-12-18 13:12:20 +00:00
$countries = is_array( $this->countries ) ? $this->countries : array();
2012-08-12 14:12:52 +00:00
2015-12-18 13:12:20 +00:00
switch ( $this->availability ) {
case 'specific' :
case 'including' :
$available = in_array( $package['destination']['country'], array_intersect( $countries, array_keys( WC()->countries->get_shipping_countries() ) ) );
break;
case 'excluding' :
$available = in_array( $package['destination']['country'], array_diff( array_keys( WC()->countries->get_shipping_countries() ), $countries ) );
break;
default :
$available = in_array( $package['destination']['country'], array_keys( WC()->countries->get_shipping_countries() ) );
break;
}
}
2012-08-12 14:12:52 +00:00
2015-12-18 13:12:20 +00:00
return apply_filters( 'woocommerce_shipping_' . $this->id . '_is_available', $available, $package );
}
/**
2015-12-18 13:12:20 +00:00
* Get fee to add to shipping cost.
* @param string|float $fee
* @param float $total
* @return float
*/
public function get_fee( $fee, $total ) {
2014-11-17 16:30:15 +00:00
if ( strstr( $fee, '%' ) ) {
$fee = ( $total / 100 ) * str_replace( '%', '', $fee );
2014-11-17 16:30:15 +00:00
}
if ( ! empty( $this->minimum_fee ) && $this->minimum_fee > $fee ) {
$fee = $this->minimum_fee;
}
return $fee;
2012-08-12 14:12:52 +00:00
}
2015-12-14 16:56:39 +00:00
/**
* Does this method have a settings page?
* @return bool
*/
public function has_settings() {
return $this->instance_id ? $this->supports( 'instance-settings' ) : $this->supports( 'settings' );
}
/**
2016-03-24 17:26:40 +00:00
* Return admin options as a html string.
* @return string
2015-12-14 16:56:39 +00:00
*/
2016-03-24 17:26:40 +00:00
public function get_admin_options_html() {
2015-12-14 16:56:39 +00:00
if ( $this->instance_id ) {
2016-03-24 19:06:51 +00:00
$settings_html = $this->generate_settings_html( $this->get_instance_form_fields(), false );
2015-12-14 16:56:39 +00:00
} else {
2016-03-24 19:06:51 +00:00
$settings_html = $this->generate_settings_html( $this->get_form_fields(), false );
2016-03-24 17:26:40 +00:00
}
2016-03-24 18:31:39 +00:00
return '<table class="form-table">' . $settings_html . '</table>';
2016-03-24 17:26:40 +00:00
}
/**
* Output the shipping settings screen.
*/
public function admin_options() {
if ( ! $this->instance_id ) {
2015-12-14 16:56:39 +00:00
echo '<h2>' . esc_html( $this->get_method_title() ) . '</h2>';
}
2016-03-24 17:26:40 +00:00
echo wp_kses_post( wpautop( $this->get_method_description() ) );
echo $this->get_admin_options_html();
2015-12-14 16:56:39 +00:00
}
/**
* get_option function.
*
* Gets and option from the settings API, using defaults if necessary to prevent undefined notices.
*
* @param string $key
* @param mixed $empty_value
* @return mixed The value specified for the option or a default value for the option.
*/
public function get_option( $key, $empty_value = null ) {
2015-12-15 12:39:52 +00:00
// Instance options take priority over global options
if ( $this->instance_id && array_key_exists( $key, $this->get_instance_form_fields() ) ) {
2015-12-14 16:56:39 +00:00
return $this->get_instance_option( $key, $empty_value );
}
// Return global option
return parent::get_option( $key, $empty_value );
}
/**
* Gets an option from the settings API, using defaults if necessary to prevent undefined notices.
2015-12-14 16:56:39 +00:00
*
* @param string $key
* @param mixed $empty_value
* @return mixed The value specified for the option or a default value for the option.
*/
public function get_instance_option( $key, $empty_value = null ) {
if ( empty( $this->instance_settings ) ) {
$this->init_instance_settings();
}
// Get option default if unset.
if ( ! isset( $this->instance_settings[ $key ] ) ) {
$form_fields = $this->get_instance_form_fields();
$this->instance_settings[ $key ] = $this->get_field_default( $form_fields[ $key ] );
}
if ( ! is_null( $empty_value ) && '' === $this->instance_settings[ $key ] ) {
$this->instance_settings[ $key ] = $empty_value;
}
return $this->instance_settings[ $key ];
}
/**
* Get settings fields for instances of this shipping method (within zones).
* Should be overridden by shipping methods to add options.
* @since 2.6.0
* @return array
*/
public function get_instance_form_fields() {
return apply_filters( 'woocommerce_shipping_instance_form_fields_' . $this->id, array_map( array( $this, 'set_defaults' ), $this->instance_form_fields ) );
}
/**
2015-12-14 16:56:39 +00:00
* Return the name of the option in the WP DB.
* @since 2.6.0
2015-12-14 16:56:39 +00:00
* @return string
*/
2015-12-14 16:56:39 +00:00
public function get_instance_option_key() {
2015-12-18 17:10:58 +00:00
return $this->instance_id ? $this->plugin_id . $this->id . '_' . $this->instance_id . '_settings' : '';
}
/**
2015-12-14 16:56:39 +00:00
* Initialise Settings for instances.
* @since 2.6.0
*/
public function init_instance_settings() {
$this->instance_settings = get_option( $this->get_instance_option_key(), null );
// If there are no settings defined, use defaults.
if ( ! is_array( $this->instance_settings ) ) {
$form_fields = $this->get_instance_form_fields();
$this->instance_settings = array_merge( array_fill_keys( array_keys( $form_fields ), '' ), wp_list_pluck( $form_fields, 'default' ) );
}
}
/**
* Processes and saves options.
* If there is an error thrown, will continue to save and validate fields, but will leave the erroring field out.
* @since 2.6.0
* @return bool was anything saved?
*/
2015-12-14 16:56:39 +00:00
public function process_admin_options() {
if ( $this->instance_id ) {
$this->init_instance_settings();
$post_data = $this->get_post_data();
2016-03-24 17:26:40 +00:00
2015-12-14 16:56:39 +00:00
foreach ( $this->get_instance_form_fields() as $key => $field ) {
2016-02-10 13:21:16 +00:00
if ( 'title' !== $this->get_field_type( $field ) ) {
2015-12-14 16:56:39 +00:00
try {
2016-03-24 17:26:40 +00:00
$this->instance_settings[ $key ] = $this->get_field_value( $key, $field, $post_data );
2015-12-14 16:56:39 +00:00
} catch ( Exception $e ) {
$this->add_error( $e->getMessage() );
}
}
}
2015-12-14 16:56:39 +00:00
return update_option( $this->get_instance_option_key(), apply_filters( 'woocommerce_shipping_' . $this->id . '_instance_settings_values', $this->instance_settings, $this ) );
} else {
2015-12-14 16:56:39 +00:00
return parent::process_admin_options();
}
}
2013-11-28 13:12:08 +00:00
}