/
custom-order-form.php
78 lines (62 loc) · 2.29 KB
/
custom-order-form.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
<?php
/*
Plugin Name: WC Custom Order Form
Description: Remove required fields on checkout for certain products
Author: James Fry
Author URI: https://github.com/jfbuildstuff
Version: 1.0
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
/**
* Check if WooCommerce is active.
*/
if (in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))) {
add_filter( 'woocommerce_checkout_fields' , 'woo_remove_billing_checkout_fields' );
/**
* Remove unwanted checkout fields
*
* @return $fields array
*/
function woo_remove_billing_checkout_fields( $fields ) {
if( woo_cart_has_product_ID() == true ) {
unset($fields['billing']['billing_first_name']);
unset($fields['billing']['billing_last_name']);
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_country']);
unset($fields['billing']['billing_state']);
unset($fields['billing']['billing_phone']);
unset($fields['order']['order_comments']);
unset($fields['billing']['billing_address_2']);
unset($fields['billing']['billing_postcode']);
unset($fields['billing']['billing_company']);
unset($fields['billing']['billing_city']);
}
//blows away all fields except email address
return $fields;
}
/**
* Check if the cart contains product ID
*
* @return bool
*/
function woo_cart_has_product_ID() {
global $woocommerce;
//set default ID to false
$is_product_ID = false;
// Get all products in cart
$products = $woocommerce->cart->get_cart();
// Loop through cart products
foreach( $products as $product ) {
// Check if specific product IDs match
if( $product['product_id'] == 125 || $product['product_id'] == 471 ) {
$is_product_ID = true;
}
}
return $is_product_ID;
}
}