class-wc-gateway-cod.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. <?php
  2. /**
  3. * Class WC_Gateway_COD file.
  4. *
  5. * @package WooCommerce\Gateways
  6. */
  7. if ( ! defined( 'ABSPATH' ) ) {
  8. exit; // Exit if accessed directly.
  9. }
  10. /**
  11. * Cash on Delivery Gateway.
  12. *
  13. * Provides a Cash on Delivery Payment Gateway.
  14. *
  15. * @class WC_Gateway_COD
  16. * @extends WC_Payment_Gateway
  17. * @version 2.1.0
  18. * @package WooCommerce/Classes/Payment
  19. */
  20. class WC_Gateway_COD extends WC_Payment_Gateway {
  21. /**
  22. * Constructor for the gateway.
  23. */
  24. public function __construct() {
  25. // Setup general properties.
  26. $this->setup_properties();
  27. // Load the settings.
  28. $this->init_form_fields();
  29. $this->init_settings();
  30. // Get settings.
  31. $this->title = $this->get_option( 'title' );
  32. $this->description = $this->get_option( 'description' );
  33. $this->instructions = $this->get_option( 'instructions' );
  34. $this->enable_for_methods = $this->get_option( 'enable_for_methods', array() );
  35. $this->enable_for_virtual = $this->get_option( 'enable_for_virtual', 'yes' ) === 'yes';
  36. add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
  37. add_action( 'woocommerce_thankyou_' . $this->id, array( $this, 'thankyou_page' ) );
  38. add_filter( 'woocommerce_payment_complete_order_status', array( $this, 'change_payment_complete_order_status' ), 10, 3 );
  39. // Customer Emails.
  40. add_action( 'woocommerce_email_before_order_table', array( $this, 'email_instructions' ), 10, 3 );
  41. }
  42. /**
  43. * Setup general properties for the gateway.
  44. */
  45. protected function setup_properties() {
  46. $this->id = 'cod';
  47. $this->icon = apply_filters( 'woocommerce_cod_icon', '' );
  48. $this->method_title = __( 'Cash on delivery', 'woocommerce' );
  49. $this->method_description = __( 'Have your customers pay with cash (or by other means) upon delivery.', 'woocommerce' );
  50. $this->has_fields = false;
  51. }
  52. /**
  53. * Initialise Gateway Settings Form Fields.
  54. */
  55. public function init_form_fields() {
  56. $options = array();
  57. $data_store = WC_Data_Store::load( 'shipping-zone' );
  58. $raw_zones = $data_store->get_zones();
  59. foreach ( $raw_zones as $raw_zone ) {
  60. $zones[] = new WC_Shipping_Zone( $raw_zone );
  61. }
  62. $zones[] = new WC_Shipping_Zone( 0 );
  63. foreach ( WC()->shipping()->load_shipping_methods() as $method ) {
  64. $options[ $method->get_method_title() ] = array();
  65. // Translators: %1$s shipping method name.
  66. $options[ $method->get_method_title() ][ $method->id ] = sprintf( __( 'Any &quot;%1$s&quot; method', 'woocommerce' ), $method->get_method_title() );
  67. foreach ( $zones as $zone ) {
  68. $shipping_method_instances = $zone->get_shipping_methods();
  69. foreach ( $shipping_method_instances as $shipping_method_instance_id => $shipping_method_instance ) {
  70. if ( $shipping_method_instance->id !== $method->id ) {
  71. continue;
  72. }
  73. $option_id = $shipping_method_instance->get_rate_id();
  74. // Translators: %1$s shipping method title, %2$s shipping method id.
  75. $option_instance_title = sprintf( __( '%1$s (#%2$s)', 'woocommerce' ), $shipping_method_instance->get_title(), $shipping_method_instance_id );
  76. // Translators: %1$s zone name, %2$s shipping method instance name.
  77. $option_title = sprintf( __( '%1$s &ndash; %2$s', 'woocommerce' ), $zone->get_id() ? $zone->get_zone_name() : __( 'Other locations', 'woocommerce' ), $option_instance_title );
  78. $options[ $method->get_method_title() ][ $option_id ] = $option_title;
  79. }
  80. }
  81. }
  82. $this->form_fields = array(
  83. 'enabled' => array(
  84. 'title' => __( 'Enable/Disable', 'woocommerce' ),
  85. 'label' => __( 'Enable cash on delivery', 'woocommerce' ),
  86. 'type' => 'checkbox',
  87. 'description' => '',
  88. 'default' => 'no',
  89. ),
  90. 'title' => array(
  91. 'title' => __( 'Title', 'woocommerce' ),
  92. 'type' => 'text',
  93. 'description' => __( 'Payment method description that the customer will see on your checkout.', 'woocommerce' ),
  94. 'default' => __( 'Cash on delivery', 'woocommerce' ),
  95. 'desc_tip' => true,
  96. ),
  97. 'description' => array(
  98. 'title' => __( 'Description', 'woocommerce' ),
  99. 'type' => 'textarea',
  100. 'description' => __( 'Payment method description that the customer will see on your website.', 'woocommerce' ),
  101. 'default' => __( 'Pay with cash upon delivery.', 'woocommerce' ),
  102. 'desc_tip' => true,
  103. ),
  104. 'instructions' => array(
  105. 'title' => __( 'Instructions', 'woocommerce' ),
  106. 'type' => 'textarea',
  107. 'description' => __( 'Instructions that will be added to the thank you page.', 'woocommerce' ),
  108. 'default' => __( 'Pay with cash upon delivery.', 'woocommerce' ),
  109. 'desc_tip' => true,
  110. ),
  111. 'enable_for_methods' => array(
  112. 'title' => __( 'Enable for shipping methods', 'woocommerce' ),
  113. 'type' => 'multiselect',
  114. 'class' => 'wc-enhanced-select',
  115. 'css' => 'width: 400px;',
  116. 'default' => '',
  117. 'description' => __( 'If COD is only available for certain methods, set it up here. Leave blank to enable for all methods.', 'woocommerce' ),
  118. 'options' => $options,
  119. 'desc_tip' => true,
  120. 'custom_attributes' => array(
  121. 'data-placeholder' => __( 'Select shipping methods', 'woocommerce' ),
  122. ),
  123. ),
  124. 'enable_for_virtual' => array(
  125. 'title' => __( 'Accept for virtual orders', 'woocommerce' ),
  126. 'label' => __( 'Accept COD if the order is virtual', 'woocommerce' ),
  127. 'type' => 'checkbox',
  128. 'default' => 'yes',
  129. ),
  130. );
  131. }
  132. /**
  133. * Check If The Gateway Is Available For Use.
  134. *
  135. * @return bool
  136. */
  137. public function is_available() {
  138. $order = null;
  139. $needs_shipping = false;
  140. // Test if shipping is needed first.
  141. if ( WC()->cart && WC()->cart->needs_shipping() ) {
  142. $needs_shipping = true;
  143. } elseif ( is_page( wc_get_page_id( 'checkout' ) ) && 0 < get_query_var( 'order-pay' ) ) {
  144. $order_id = absint( get_query_var( 'order-pay' ) );
  145. $order = wc_get_order( $order_id );
  146. // Test if order needs shipping.
  147. if ( 0 < count( $order->get_items() ) ) {
  148. foreach ( $order->get_items() as $item ) {
  149. $_product = $item->get_product();
  150. if ( $_product && $_product->needs_shipping() ) {
  151. $needs_shipping = true;
  152. break;
  153. }
  154. }
  155. }
  156. } elseif ( WC()->cart && WC()->cart->needs_shipping() ) {
  157. $needs_shipping = true;
  158. }
  159. $needs_shipping = apply_filters( 'woocommerce_cart_needs_shipping', $needs_shipping );
  160. // Virtual order, with virtual disabled.
  161. if ( ! $this->enable_for_virtual && ! $needs_shipping ) {
  162. return false;
  163. }
  164. // Only apply if all packages are being shipped via chosen method, or order is virtual.
  165. if ( ! empty( $this->enable_for_methods ) && $needs_shipping ) {
  166. $canonical_rate_ids = array();
  167. $order_shipping_items = is_object( $order ) ? $order->get_shipping_methods() : false;
  168. $chosen_shipping_methods_session = WC()->session->get( 'chosen_shipping_methods' );
  169. if ( $order_shipping_items ) {
  170. $canonical_rate_ids = $this->get_canonical_order_shipping_item_rate_ids( $order_shipping_items );
  171. } else {
  172. $canonical_rate_ids = $this->get_canonical_package_rate_ids( $chosen_shipping_methods_session );
  173. }
  174. if ( ! count( $this->get_matching_rates( $canonical_rate_ids ) ) ) {
  175. return false;
  176. }
  177. }
  178. return parent::is_available();
  179. }
  180. /**
  181. * Converts the chosen rate IDs generated by Shipping Methods to a canonical 'method_id:instance_id' format.
  182. *
  183. * @since 3.4.0
  184. *
  185. * @param array $order_shipping_items Array of WC_Order_Item_Shipping objects.
  186. * @return array $canonical_rate_ids Rate IDs in a canonical format.
  187. */
  188. private function get_canonical_order_shipping_item_rate_ids( $order_shipping_items ) {
  189. $canonical_rate_ids = array();
  190. foreach ( $order_shipping_items as $order_shipping_item ) {
  191. $canonical_rate_ids[] = $order_shipping_item->get_method_id() . ':' . $order_shipping_item->get_instance_id();
  192. }
  193. return $canonical_rate_ids;
  194. }
  195. /**
  196. * Converts the chosen rate IDs generated by Shipping Methods to a canonical 'method_id:instance_id' format.
  197. *
  198. * @since 3.4.0
  199. *
  200. * @param array $chosen_package_rate_ids Rate IDs as generated by shipping methods. Can be anything if a shipping method doesn't honor WC conventions.
  201. * @return array $canonical_rate_ids Rate IDs in a canonical format.
  202. */
  203. private function get_canonical_package_rate_ids( $chosen_package_rate_ids ) {
  204. $shipping_packages = WC()->shipping->get_packages();
  205. $canonical_rate_ids = array();
  206. if ( ! empty( $chosen_package_rate_ids ) && is_array( $chosen_package_rate_ids ) ) {
  207. foreach ( $chosen_package_rate_ids as $package_key => $chosen_package_rate_id ) {
  208. if ( ! empty( $shipping_packages[ $package_key ]['rates'][ $chosen_package_rate_id ] ) ) {
  209. $chosen_rate = $shipping_packages[ $package_key ]['rates'][ $chosen_package_rate_id ];
  210. $canonical_rate_ids[] = $chosen_rate->get_method_id() . ':' . $chosen_rate->get_instance_id();
  211. }
  212. }
  213. }
  214. return $canonical_rate_ids;
  215. }
  216. /**
  217. * Indicates whether a rate exists in an array of canonically-formatted rate IDs that activates this gateway.
  218. *
  219. * @since 3.4.0
  220. *
  221. * @param array $rate_ids Rate ids to check.
  222. * @return boolean
  223. */
  224. private function get_matching_rates( $rate_ids ) {
  225. // First, match entries in 'method_id:instance_id' format. Then, match entries in 'method_id' format by stripping off the instance ID from the candidates.
  226. return array_unique( array_merge( array_intersect( $this->enable_for_methods, $rate_ids ), array_intersect( $this->enable_for_methods, array_unique( array_map( 'wc_get_string_before_colon', $rate_ids ) ) ) ) );
  227. }
  228. /**
  229. * Process the payment and return the result.
  230. *
  231. * @param int $order_id Order ID.
  232. * @return array
  233. */
  234. public function process_payment( $order_id ) {
  235. $order = wc_get_order( $order_id );
  236. if ( $order->get_total() > 0 ) {
  237. // Mark as processing or on-hold (payment won't be taken until delivery).
  238. $order->update_status( apply_filters( 'woocommerce_cod_process_payment_order_status', $order->has_downloadable_item() ? 'on-hold' : 'processing', $order ), __( 'Payment to be made upon delivery.', 'woocommerce' ) );
  239. } else {
  240. $order->payment_complete();
  241. }
  242. // Reduce stock levels.
  243. wc_reduce_stock_levels( $order_id );
  244. // Remove cart.
  245. WC()->cart->empty_cart();
  246. // Return thankyou redirect.
  247. return array(
  248. 'result' => 'success',
  249. 'redirect' => $this->get_return_url( $order ),
  250. );
  251. }
  252. /**
  253. * Output for the order received page.
  254. */
  255. public function thankyou_page() {
  256. if ( $this->instructions ) {
  257. echo wp_kses_post( wpautop( wptexturize( $this->instructions ) ) );
  258. }
  259. }
  260. /**
  261. * Change payment complete order status to completed for COD orders.
  262. *
  263. * @since 3.1.0
  264. * @param string $status Current order status.
  265. * @param int $order_id Order ID.
  266. * @param WC_Order|false $order Order object.
  267. * @return string
  268. */
  269. public function change_payment_complete_order_status( $status, $order_id = 0, $order = false ) {
  270. if ( $order && 'cod' === $order->get_payment_method() ) {
  271. $status = 'completed';
  272. }
  273. return $status;
  274. }
  275. /**
  276. * Add content to the WC emails.
  277. *
  278. * @access public
  279. * @param WC_Order $order Order object.
  280. * @param bool $sent_to_admin Sent to admin.
  281. * @param bool $plain_text Email format: plain text or HTML.
  282. */
  283. public function email_instructions( $order, $sent_to_admin, $plain_text = false ) {
  284. if ( $this->instructions && ! $sent_to_admin && $this->id === $order->get_payment_method() ) {
  285. echo wp_kses_post( wpautop( wptexturize( $this->instructions ) ) . PHP_EOL );
  286. }
  287. }
  288. }