forked from claudiosanches/wc-digital-goods-checkout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwc-digital-goods-checkout.php
133 lines (116 loc) · 3.65 KB
/
wc-digital-goods-checkout.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
/**
* Plugin Name: Digital Goods Checkout on WooCommerce
* Plugin URI: http://github.com/claudiosmweb/wc-digital-goods-checkout
* Description: Hide billing fields when have only digital products in the cart.
* Author: Claudio Sanches
* Author URI: http://claudiosmweb.com/
* Version: 1.1.0
* License: GPLv2 or later
* Text Domain: wc-digital-goods-checkout
* Domain Path: languages/
*
* @package WC_Digital_Goods_Checkout
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'WC_Digital_Goods_Checkout' ) ) :
/**
* Plugin main class.
*
* @package WC_Digital_Goods_Checkout
*/
class WC_Digital_Goods_Checkout {
/**
* Plugin version.
*
* @var string
*/
const VERSION = '1.1.0';
/**
* Instance of this class.
*
* @var object
*/
protected static $instance = null;
/**
* Initialize the plugin public actions.
*/
private function __construct() {
add_action( 'init', array( $this, 'load_plugin_textdomain' ) );
add_action( 'woocommerce_checkout_fields', array( $this, 'checkout_fields' ) );
add_filter( 'wcbcf_disable_checkout_validation', array( $this, 'disable_checkout_validation_for_ecfb' ) );
}
/**
* Return an instance of this class.
*
* @return object A single instance of this class.
*/
public static function get_instance() {
// If the single instance hasn't been set, set it now.
if ( null == self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Load the plugin text domain for translation.
*/
public function load_plugin_textdomain() {
load_plugin_textdomain( 'wc-digital-goods-checkout', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
/**
* Check if cart contains only digital goods.
*
* @return bool
*/
protected function has_digital_goods_only() {
return ! ( WC()->cart && WC()->cart->needs_shipping() );
}
/**
* Manipule the checkout fields.
* Remove all billing fields when cart contains only digital goods.
*
* @param array $fields Checkout fields.
* @return array
*/
public function checkout_fields( $fields ) {
if ( $this->has_digital_goods_only() ) {
unset( $fields['billing']['billing_company'] );
unset( $fields['billing']['billing_address_1'] );
unset( $fields['billing']['billing_address_2'] );
unset( $fields['billing']['billing_city'] );
unset( $fields['billing']['billing_postcode'] );
unset( $fields['billing']['billing_state'] );
unset( $fields['billing']['billing_country'] );
unset( $fields['billing']['billing_phone'] );
// Extra Checkout Fields for Brazil fields.
unset( $fields['billing']['billing_persontype'] );
unset( $fields['billing']['billing_cpf'] );
unset( $fields['billing']['billing_rg'] );
unset( $fields['billing']['billing_cnpj'] );
unset( $fields['billing']['billing_ie'] );
unset( $fields['billing']['billing_number'] );
unset( $fields['billing']['billing_neighborhood'] );
unset( $fields['billing']['billing_cellphone'] );
unset( $fields['billing']['billing_birthdate'] );
unset( $fields['billing']['billing_sex'] );
// Fix email field size.
$fields['billing']['billing_email']['class'] = array( 'form-row-wide' );
}
return $fields;
}
/**
* Disable checkout validation for WooCommerce Extra Checkout Fields for Brazil
* when cart have only digital goods.
*
* @param bool $valid Cart validation.
* @return bool
*/
public function disable_checkout_validation_for_ecfb( $valid ) {
return $this->has_digital_goods_only();
}
}
add_action( 'plugins_loaded', array( 'WC_Digital_Goods_Checkout', 'get_instance' ) );
endif;