WC_Gateway_COD

Cash on Delivery Gateway.

Defined (1)

The class is defined in the following location(s).

/includes/gateways/cod/class-wc-gateway-cod.php  
  1. class WC_Gateway_COD extends WC_Payment_Gateway { 
  2.  
  3. /** 
  4. * Constructor for the gateway. 
  5. */ 
  6. public function __construct() { 
  7. // Setup general properties 
  8. $this->setup_properties(); 
  9.  
  10. // Load the settings 
  11. $this->init_form_fields(); 
  12. $this->init_settings(); 
  13.  
  14. // Get settings 
  15. $this->title = $this->get_option( 'title' ); 
  16. $this->description = $this->get_option( 'description' ); 
  17. $this->instructions = $this->get_option( 'instructions' ); 
  18. $this->enable_for_methods = $this->get_option( 'enable_for_methods', array() ); 
  19. $this->enable_for_virtual = $this->get_option( 'enable_for_virtual', 'yes' ) === 'yes' ? true : false; 
  20.  
  21. add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) ); 
  22. add_action( 'woocommerce_thankyou_' . $this->id, array( $this, 'thankyou_page' ) ); 
  23.  
  24. // Customer Emails 
  25. add_action( 'woocommerce_email_before_order_table', array( $this, 'email_instructions' ), 10, 3 ); 
  26.  
  27. /** 
  28. * Setup general properties for the gateway. 
  29. */ 
  30. protected function setup_properties() { 
  31. $this->id = 'cod'; 
  32. $this->icon = apply_filters( 'woocommerce_cod_icon', '' ); 
  33. $this->method_title = __( 'Cash on delivery', 'woocommerce' ); 
  34. $this->method_description = __( 'Have your customers pay with cash (or by other means) upon delivery.', 'woocommerce' ); 
  35. $this->has_fields = false; 
  36.  
  37. /** 
  38. * Initialise Gateway Settings Form Fields. 
  39. */ 
  40. public function init_form_fields() { 
  41. $shipping_methods = array(); 
  42.  
  43. foreach ( WC()->shipping()->load_shipping_methods() as $method ) { 
  44. $shipping_methods[ $method->id ] = $method->get_method_title(); 
  45.  
  46. $this->form_fields = array( 
  47. 'enabled' => array( 
  48. 'title' => __( 'Enable/Disable', 'woocommerce' ),  
  49. 'label' => __( 'Enable cash on delivery', 'woocommerce' ),  
  50. 'type' => 'checkbox',  
  51. 'description' => '',  
  52. 'default' => 'no',  
  53. ),  
  54. 'title' => array( 
  55. 'title' => __( 'Title', 'woocommerce' ),  
  56. 'type' => 'text',  
  57. 'description' => __( 'Payment method description that the customer will see on your checkout.', 'woocommerce' ),  
  58. 'default' => __( 'Cash on delivery', 'woocommerce' ),  
  59. 'desc_tip' => true,  
  60. ),  
  61. 'description' => array( 
  62. 'title' => __( 'Description', 'woocommerce' ),  
  63. 'type' => 'textarea',  
  64. 'description' => __( 'Payment method description that the customer will see on your website.', 'woocommerce' ),  
  65. 'default' => __( 'Pay with cash upon delivery.', 'woocommerce' ),  
  66. 'desc_tip' => true,  
  67. ),  
  68. 'instructions' => array( 
  69. 'title' => __( 'Instructions', 'woocommerce' ),  
  70. 'type' => 'textarea',  
  71. 'description' => __( 'Instructions that will be added to the thank you page.', 'woocommerce' ),  
  72. 'default' => __( 'Pay with cash upon delivery.', 'woocommerce' ),  
  73. 'desc_tip' => true,  
  74. ),  
  75. 'enable_for_methods' => array( 
  76. 'title' => __( 'Enable for shipping methods', 'woocommerce' ),  
  77. 'type' => 'multiselect',  
  78. 'class' => 'wc-enhanced-select',  
  79. 'css' => 'width: 400px;',  
  80. 'default' => '',  
  81. 'description' => __( 'If COD is only available for certain methods, set it up here. Leave blank to enable for all methods.', 'woocommerce' ),  
  82. 'options' => $shipping_methods,  
  83. 'desc_tip' => true,  
  84. 'custom_attributes' => array( 
  85. 'data-placeholder' => __( 'Select shipping methods', 'woocommerce' ),  
  86. ),  
  87. ),  
  88. 'enable_for_virtual' => array( 
  89. 'title' => __( 'Accept for virtual orders', 'woocommerce' ),  
  90. 'label' => __( 'Accept COD if the order is virtual', 'woocommerce' ),  
  91. 'type' => 'checkbox',  
  92. 'default' => 'yes',  
  93. ),  
  94. ); 
  95.  
  96. /** 
  97. * Check If The Gateway Is Available For Use. 
  98. * @return bool 
  99. */ 
  100. public function is_available() { 
  101. $order = null; 
  102. $needs_shipping = false; 
  103.  
  104. // Test if shipping is needed first 
  105. if ( WC()->cart && WC()->cart->needs_shipping() ) { 
  106. $needs_shipping = true; 
  107. } elseif ( is_page( wc_get_page_id( 'checkout' ) ) && 0 < get_query_var( 'order-pay' ) ) { 
  108. $order_id = absint( get_query_var( 'order-pay' ) ); 
  109. $order = wc_get_order( $order_id ); 
  110.  
  111. // Test if order needs shipping. 
  112. if ( 0 < sizeof( $order->get_items() ) ) { 
  113. foreach ( $order->get_items() as $item ) { 
  114. $_product = $item->get_product(); 
  115. if ( $_product && $_product->needs_shipping() ) { 
  116. $needs_shipping = true; 
  117. break; 
  118.  
  119. $needs_shipping = apply_filters( 'woocommerce_cart_needs_shipping', $needs_shipping ); 
  120.  
  121. // Virtual order, with virtual disabled 
  122. if ( ! $this->enable_for_virtual && ! $needs_shipping ) { 
  123. return false; 
  124.  
  125. // Check methods 
  126. if ( ! empty( $this->enable_for_methods ) && $needs_shipping ) { 
  127.  
  128. // Only apply if all packages are being shipped via chosen methods, or order is virtual 
  129. $chosen_shipping_methods_session = WC()->session->get( 'chosen_shipping_methods' ); 
  130.  
  131. if ( isset( $chosen_shipping_methods_session ) ) { 
  132. $chosen_shipping_methods = array_unique( $chosen_shipping_methods_session ); 
  133. } else { 
  134. $chosen_shipping_methods = array(); 
  135.  
  136. $check_method = false; 
  137.  
  138. if ( is_object( $order ) ) { 
  139. if ( $order->shipping_method ) { 
  140. $check_method = $order->shipping_method; 
  141. } elseif ( empty( $chosen_shipping_methods ) || sizeof( $chosen_shipping_methods ) > 1 ) { 
  142. $check_method = false; 
  143. } elseif ( sizeof( $chosen_shipping_methods ) == 1 ) { 
  144. $check_method = $chosen_shipping_methods[0]; 
  145.  
  146. if ( ! $check_method ) { 
  147. return false; 
  148.  
  149. $found = false; 
  150.  
  151. foreach ( $this->enable_for_methods as $method_id ) { 
  152. if ( strpos( $check_method, $method_id ) === 0 ) { 
  153. $found = true; 
  154. break; 
  155.  
  156. if ( ! $found ) { 
  157. return false; 
  158.  
  159. return parent::is_available(); 
  160.  
  161.  
  162. /** 
  163. * Process the payment and return the result. 
  164. * @param int $order_id 
  165. * @return array 
  166. */ 
  167. public function process_payment( $order_id ) { 
  168. $order = wc_get_order( $order_id ); 
  169.  
  170. // Mark as processing or on-hold (payment won't be taken until delivery) 
  171. $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' ) ); 
  172.  
  173. // Reduce stock levels 
  174. wc_reduce_stock_levels( $order_id ); 
  175.  
  176. // Remove cart 
  177. WC()->cart->empty_cart(); 
  178.  
  179. // Return thankyou redirect 
  180. return array( 
  181. 'result' => 'success',  
  182. 'redirect' => $this->get_return_url( $order ),  
  183. ); 
  184.  
  185. /** 
  186. * Output for the order received page. 
  187. */ 
  188. public function thankyou_page() { 
  189. if ( $this->instructions ) { 
  190. echo wpautop( wptexturize( $this->instructions ) ); 
  191.  
  192. /** 
  193. * Add content to the WC emails. 
  194. * @access public 
  195. * @param WC_Order $order 
  196. * @param bool $sent_to_admin 
  197. * @param bool $plain_text 
  198. */ 
  199. public function email_instructions( $order, $sent_to_admin, $plain_text = false ) { 
  200. if ( $this->instructions && ! $sent_to_admin && $this->id === $order->get_payment_method() ) { 
  201. echo wpautop( wptexturize( $this->instructions ) ) . PHP_EOL;