class-wc-shipping-legacy-local-delivery.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. /**
  3. * Class WC_Shipping_Legacy_Local_Delivery file.
  4. *
  5. * @package WooCommerce\Shipping
  6. */
  7. if ( ! defined( 'ABSPATH' ) ) {
  8. exit; // Exit if accessed directly.
  9. }
  10. /**
  11. * Local Delivery Shipping Method.
  12. *
  13. * This class is here for backwards compatibility for methods existing before zones existed.
  14. *
  15. * @deprecated 2.6.0
  16. * @version 2.3.0
  17. * @package WooCommerce/Classes/Shipping
  18. */
  19. class WC_Shipping_Legacy_Local_Delivery extends WC_Shipping_Local_Pickup {
  20. /**
  21. * Constructor.
  22. */
  23. public function __construct() {
  24. $this->id = 'legacy_local_delivery';
  25. $this->method_title = __( 'Local delivery (legacy)', 'woocommerce' );
  26. /* translators: %s: Admin shipping settings URL */
  27. $this->method_description = '<strong>' . sprintf( __( 'This method is deprecated in 2.6.0 and will be removed in future versions - we recommend disabling it and instead setting up a new rate within your <a href="%s">Shipping zones</a>.', 'woocommerce' ), admin_url( 'admin.php?page=wc-settings&tab=shipping' ) ) . '</strong>';
  28. $this->init();
  29. }
  30. /**
  31. * Process and redirect if disabled.
  32. */
  33. public function process_admin_options() {
  34. parent::process_admin_options();
  35. if ( 'no' === $this->settings['enabled'] ) {
  36. wp_redirect( admin_url( 'admin.php?page=wc-settings&tab=shipping&section=options' ) );
  37. exit;
  38. }
  39. }
  40. /**
  41. * Return the name of the option in the WP DB.
  42. *
  43. * @since 2.6.0
  44. * @return string
  45. */
  46. public function get_option_key() {
  47. return $this->plugin_id . 'local_delivery_settings';
  48. }
  49. /**
  50. * Init function.
  51. */
  52. public function init() {
  53. // Load the settings.
  54. $this->init_form_fields();
  55. $this->init_settings();
  56. // Define user set variables.
  57. $this->title = $this->get_option( 'title' );
  58. $this->type = $this->get_option( 'type' );
  59. $this->fee = $this->get_option( 'fee' );
  60. $this->type = $this->get_option( 'type' );
  61. $this->codes = $this->get_option( 'codes' );
  62. $this->availability = $this->get_option( 'availability' );
  63. $this->countries = $this->get_option( 'countries' );
  64. add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
  65. }
  66. /**
  67. * Calculate_shipping function.
  68. *
  69. * @param array $package (default: array()).
  70. */
  71. public function calculate_shipping( $package = array() ) {
  72. $shipping_total = 0;
  73. switch ( $this->type ) {
  74. case 'fixed':
  75. $shipping_total = $this->fee;
  76. break;
  77. case 'percent':
  78. $shipping_total = $package['contents_cost'] * ( $this->fee / 100 );
  79. break;
  80. case 'product':
  81. foreach ( $package['contents'] as $item_id => $values ) {
  82. if ( $values['quantity'] > 0 && $values['data']->needs_shipping() ) {
  83. $shipping_total += $this->fee * $values['quantity'];
  84. }
  85. }
  86. break;
  87. }
  88. $rate = array(
  89. 'id' => $this->id,
  90. 'label' => $this->title,
  91. 'cost' => $shipping_total,
  92. 'package' => $package,
  93. );
  94. $this->add_rate( $rate );
  95. }
  96. /**
  97. * Init form fields.
  98. */
  99. public function init_form_fields() {
  100. $this->form_fields = array(
  101. 'enabled' => array(
  102. 'title' => __( 'Enable', 'woocommerce' ),
  103. 'type' => 'checkbox',
  104. 'label' => __( 'Once disabled, this legacy method will no longer be available.', 'woocommerce' ),
  105. 'default' => 'no',
  106. ),
  107. 'title' => array(
  108. 'title' => __( 'Title', 'woocommerce' ),
  109. 'type' => 'text',
  110. 'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ),
  111. 'default' => __( 'Local delivery', 'woocommerce' ),
  112. 'desc_tip' => true,
  113. ),
  114. 'type' => array(
  115. 'title' => __( 'Fee type', 'woocommerce' ),
  116. 'type' => 'select',
  117. 'class' => 'wc-enhanced-select',
  118. 'description' => __( 'How to calculate delivery charges', 'woocommerce' ),
  119. 'default' => 'fixed',
  120. 'options' => array(
  121. 'fixed' => __( 'Fixed amount', 'woocommerce' ),
  122. 'percent' => __( 'Percentage of cart total', 'woocommerce' ),
  123. 'product' => __( 'Fixed amount per product', 'woocommerce' ),
  124. ),
  125. 'desc_tip' => true,
  126. ),
  127. 'fee' => array(
  128. 'title' => __( 'Delivery fee', 'woocommerce' ),
  129. 'type' => 'price',
  130. 'description' => __( 'What fee do you want to charge for local delivery, disregarded if you choose free. Leave blank to disable.', 'woocommerce' ),
  131. 'default' => '',
  132. 'desc_tip' => true,
  133. 'placeholder' => wc_format_localized_price( 0 ),
  134. ),
  135. 'codes' => array(
  136. 'title' => __( 'Allowed ZIP/post codes', 'woocommerce' ),
  137. 'type' => 'text',
  138. 'desc_tip' => __( 'What ZIP/post codes are available for local delivery?', 'woocommerce' ),
  139. 'default' => '',
  140. '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' ),
  141. 'placeholder' => 'e.g. 12345, 56789',
  142. ),
  143. 'availability' => array(
  144. 'title' => __( 'Method availability', 'woocommerce' ),
  145. 'type' => 'select',
  146. 'default' => 'all',
  147. 'class' => 'availability wc-enhanced-select',
  148. 'options' => array(
  149. 'all' => __( 'All allowed countries', 'woocommerce' ),
  150. 'specific' => __( 'Specific Countries', 'woocommerce' ),
  151. ),
  152. ),
  153. 'countries' => array(
  154. 'title' => __( 'Specific countries', 'woocommerce' ),
  155. 'type' => 'multiselect',
  156. 'class' => 'wc-enhanced-select',
  157. 'css' => 'width: 400px;',
  158. 'default' => '',
  159. 'options' => WC()->countries->get_shipping_countries(),
  160. 'custom_attributes' => array(
  161. 'data-placeholder' => __( 'Select some countries', 'woocommerce' ),
  162. ),
  163. ),
  164. );
  165. }
  166. }