No Description

class-wc-gateway-cod.php 13KB

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