forked from nextime/woocommerce-better-product-addons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
product-addons.php
188 lines (151 loc) · 5.48 KB
/
product-addons.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
/*
Plugin Name: WooCommerce Better Product Add-ons
Plugin URI: https://github.com/nextime/woocommerce-better-product-addons
Description: WooCommerce Better Product Add-ons is a fork of WooCommerce Product Add-ons 2.5.6. It lets you add extra options to products which the user can select. Add-ons can be checkboxes, a select box, or custom input. Each option can optionally be given a price which is added to the cost of the product.
Version: 2.5.7
Original Author: WooThemes
Fork Author: nextime
Original Author URI: http://woothemes.com
Fork Author URI: https://github.com/nextime/
Requires at least: 3.1
Tested up to: 3.2
Copyright for original WooCommerce Product Add-ons: © 2009-2011 WooThemes.
Copyright: © 2014 nextime.
License: GNU General Public License v3.0
License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
/**
* Required functions
*/
if ( ! function_exists( 'woothemes_queue_update' ) )
require_once( 'woo-includes/woo-functions.php' );
if ( is_woocommerce_active() ) {
/**
* Localisation
*/
load_plugin_textdomain( 'wc_product_addons', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
/**
* Init
*/
if ( ! class_exists( 'Product_Addon_Display' ) ) {
if ( is_admin() ) {
include_once( 'admin/class-product-addon-admin.php' );
$GLOBALS['Product_Addon_Admin'] = new Product_Addon_Admin();
}
include_once( 'classes/class-product-addon-display.php' );
$GLOBALS['Product_Addon_Display'] = new Product_Addon_Display();
include_once( 'classes/class-product-addon-cart.php' );
$GLOBALS['Product_Addon_Cart'] = new Product_Addon_Cart();
}
/**
* Gets addons assigned to a product by ID
*
* @param int $post_id ID of the product to get addons for
* @return array array of addons
*/
function get_product_addons( $post_id, $prefix = false ) {
if ( ! $post_id )
return array();
$addons = array();
$raw_addons = array();
$product_terms = wp_get_post_terms( $post_id, 'product_cat', array( 'fields' => 'ids' ) );
$exclude = get_post_meta( $post_id, '_product_addons_exclude_global', TRUE );
// Product level addons
$raw_addons[10][0] = array_filter( (array) get_post_meta( $post_id, '_product_addons', true ) );
if ( !isset($exclude) || $exclude != '1' ) :
// Global level addons (all products)
$args = array(
'posts_per_page' => -1,
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_key' => '_priority',
'post_type' => 'global_product_addon',
'post_status' => 'publish',
'suppress_filters' => true,
'meta_query' => array(
array(
'key' => '_all_products',
'value' => '1',
)
)
);
$global_addons = get_posts( $args );
if ( $global_addons ) :
foreach ( $global_addons as $global_addon ) {
$priority = get_post_meta( $global_addon->ID, '_priority', true );
$raw_addons[ $priority ][ $global_addon->ID ] = array_filter( (array) get_post_meta( $global_addon->ID, '_product_addons', true ) );
}
endif;
// Global level addons (categories)
if ( $product_terms ) :
$args = array(
'posts_per_page' => -1,
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_key' => '_priority',
'post_type' => 'global_product_addon',
'post_status' => 'publish',
'suppress_filters' => true,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $product_terms,
'include_children' => false
)
)
);
$global_addons = get_posts( $args );
if ( $global_addons ) :
foreach ( $global_addons as $global_addon ) {
$priority = get_post_meta( $global_addon->ID, '_priority', true );
$raw_addons[ $priority ][ $global_addon->ID ] = array_filter( (array) get_post_meta( $global_addon->ID, '_product_addons', true ) );
}
endif;
endif;
endif; // exclude from global addons
ksort( $raw_addons );
foreach ( $raw_addons as $addon_group ) {
if ( $addon_group ) {
foreach ( $addon_group as $addon )
$addons = array_merge( $addons, $addon );
}
}
// Generate field names with unqiue prefixes
if ( ! $prefix )
$prefix = apply_filters( 'product_addons_field_prefix', "{$post_id}-", $post_id );
foreach ( $addons as $addon_key => $addon ) {
$addons[ $addon_key ]['field-name'] = $prefix . $addon['name'];
}
return apply_filters( 'wc_get_product_addons', $addons );
}
/**
* Register post types for global addons
*/
function product_addons_post_types() {
register_post_type( "global_product_addon",
array(
'public' => false,
'show_ui' => false,
'capability_type' => 'product',
'map_meta_cap' => true,
'publicly_queryable' => false,
'exclude_from_search' => true,
'hierarchical' => false,
'rewrite' => false,
'query_var' => false,
'supports' => array( 'title' ),
'show_in_nav_menus' => false
)
);
register_taxonomy_for_object_type( 'product_cat', 'global_product_addon' );
}
function remove_woo_ajax_button() {
// replace the ajax button with the complete add to cart button with QTY
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10);
add_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_single_add_to_cart', 1);
}
add_action( 'init', 'product_addons_post_types', 20 );
add_action('plugins_loaded','remove_woo_ajax_button');
}