woocommerce/includes/shipping/local-delivery/class-wc-shipping-local-del...

154 lines
4.8 KiB
PHP
Raw Normal View History

2012-05-26 16:25:07 +00:00
<?php
2013-02-20 17:14:46 +00:00
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
2013-02-20 17:14:46 +00:00
2012-05-26 16:25:07 +00:00
/**
2015-11-03 13:31:20 +00:00
* Local Delivery Shipping Method.
2012-08-14 22:43:48 +00:00
*
2015-11-03 13:31:20 +00:00
* A simple shipping method allowing local delivery as a shipping method.
2012-05-26 16:25:07 +00:00
*
* @class WC_Shipping_Local_Delivery
* @version 2.3.0
2012-08-14 22:43:48 +00:00
* @package WooCommerce/Classes/Shipping
* @author WooThemes
*/
class WC_Shipping_Local_Delivery extends WC_Shipping_Local_Pickup {
2012-08-14 22:43:48 +00:00
/**
2015-11-03 13:31:20 +00:00
* Constructor.
2012-08-14 22:43:48 +00:00
*/
public function __construct() {
$this->id = 'local_delivery';
$this->method_title = __( 'Local Delivery', 'woocommerce' );
$this->method_description = __( 'Local delivery is a simple shipping method for delivering orders locally.', 'woocommerce' );
2012-05-26 16:25:07 +00:00
$this->init();
2012-08-14 22:43:48 +00:00
}
/**
* init function.
*/
public function init() {
2012-08-14 22:43:48 +00:00
2012-05-26 16:25:07 +00:00
// Load the settings.
2013-01-02 13:38:33 +00:00
$this->init_form_fields();
2012-05-26 16:25:07 +00:00
$this->init_settings();
2012-08-14 22:43:48 +00:00
2012-05-26 16:25:07 +00:00
// Define user set variables
$this->title = $this->get_option( 'title' );
2014-04-01 21:55:36 +00:00
$this->type = $this->get_option( 'type' );
$this->fee = $this->get_option( 'fee' );
$this->type = $this->get_option( 'type' );
$this->codes = $this->get_option( 'codes' );
$this->availability = $this->get_option( 'availability' );
$this->countries = $this->get_option( 'countries' );
2012-08-14 22:43:48 +00:00
add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
2012-05-26 16:25:07 +00:00
}
2012-08-14 22:43:48 +00:00
/**
* calculate_shipping function.
*
* @param array $package (default: array())
*/
public function calculate_shipping( $package = array() ) {
2012-05-26 16:25:07 +00:00
$shipping_total = 0;
2012-08-14 22:43:48 +00:00
switch ( $this->type ) {
case 'fixed' :
$shipping_total = $this->fee;
break;
case 'percent' :
$shipping_total = $package['contents_cost'] * ( $this->fee / 100 );
break;
case 'product' :
foreach ( $package['contents'] as $item_id => $values ) {
if ( $values['quantity'] > 0 && $values['data']->needs_shipping() ) {
$shipping_total += $this->fee * $values['quantity'];
}
}
break;
2012-05-26 16:25:07 +00:00
}
2012-08-14 22:43:48 +00:00
2012-05-26 16:25:07 +00:00
$rate = array(
2014-04-01 21:55:36 +00:00
'id' => $this->id,
'label' => $this->title,
'cost' => $shipping_total
2012-05-26 16:25:07 +00:00
);
2012-08-14 22:43:48 +00:00
$this->add_rate( $rate );
2012-05-26 16:25:07 +00:00
}
2012-08-14 22:43:48 +00:00
/**
2015-07-16 19:55:48 +00:00
* Init form fields.
2012-08-14 22:43:48 +00:00
*/
public function init_form_fields() {
$this->form_fields = array(
2012-05-26 16:25:07 +00:00
'enabled' => array(
'title' => __( 'Enable', 'woocommerce' ),
'type' => 'checkbox',
'label' => __( 'Enable local delivery', 'woocommerce' ),
'default' => 'no'
2012-08-14 22:43:48 +00:00
),
2012-05-26 16:25:07 +00:00
'title' => array(
2014-04-01 21:55:36 +00:00
'title' => __( 'Title', 'woocommerce' ),
'type' => 'text',
'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ),
'default' => __( 'Local Delivery', 'woocommerce' ),
'desc_tip' => true,
2012-05-26 16:25:07 +00:00
),
'type' => array(
2014-04-01 21:55:36 +00:00
'title' => __( 'Fee Type', 'woocommerce' ),
'type' => 'select',
2015-01-12 17:45:47 +00:00
'class' => 'wc-enhanced-select',
2014-04-01 21:55:36 +00:00
'description' => __( 'How to calculate delivery charges', 'woocommerce' ),
'default' => 'fixed',
'options' => array(
'fixed' => __( 'Fixed amount', 'woocommerce' ),
2014-04-01 21:55:36 +00:00
'percent' => __( 'Percentage of cart total', 'woocommerce' ),
'product' => __( 'Fixed amount per product', 'woocommerce' ),
2012-05-26 16:25:07 +00:00
),
2014-04-01 21:55:36 +00:00
'desc_tip' => true,
2012-05-26 16:25:07 +00:00
),
'fee' => array(
2014-04-01 21:55:36 +00:00
'title' => __( 'Delivery Fee', 'woocommerce' ),
'type' => 'price',
'description' => __( 'What fee do you want to charge for local delivery, disregarded if you choose free. Leave blank to disable.', 'woocommerce' ),
'default' => '',
'desc_tip' => true,
'placeholder' => wc_format_localized_price( 0 )
2012-05-26 16:25:07 +00:00
),
'codes' => array(
'title' => __( 'Allowed Zip/Post Codes', 'woocommerce' ),
'type' => 'text',
'desc_tip' => __( 'What zip/post codes are available for local delivery?', 'woocommerce' ),
2014-04-01 21:55:36 +00:00
'default' => '',
'description' => __( 'Separate codes with a comma. Accepts wildcards, e.g. <code>P*</code> will match a postcode of PE30. Also accepts a pattern, e.g. <code>NG1___</code> would match NG1 1AA but not NG10 1AA', 'woocommerce' ),
'placeholder' => 'e.g. 12345, 56789'
2012-05-26 16:25:07 +00:00
),
'availability' => array(
2014-04-01 21:55:36 +00:00
'title' => __( 'Method availability', 'woocommerce' ),
'type' => 'select',
'default' => 'all',
2015-01-12 17:45:47 +00:00
'class' => 'availability wc-enhanced-select',
'options' => array(
2014-04-01 21:55:36 +00:00
'all' => __( 'All allowed countries', 'woocommerce' ),
'specific' => __( 'Specific Countries', 'woocommerce' )
)
),
2012-05-26 16:25:07 +00:00
'countries' => array(
2014-04-01 21:55:36 +00:00
'title' => __( 'Specific Countries', 'woocommerce' ),
'type' => 'multiselect',
2015-01-12 16:29:01 +00:00
'class' => 'wc-enhanced-select',
2014-04-01 21:55:36 +00:00
'css' => 'width: 450px;',
'default' => '',
'options' => WC()->countries->get_shipping_countries(),
'custom_attributes' => array(
'data-placeholder' => __( 'Select some countries', 'woocommerce' )
)
)
2012-05-26 16:25:07 +00:00
);
}
2014-04-01 21:55:36 +00:00
}