-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwc-attributes-on-page.php
More file actions
executable file
·190 lines (152 loc) · 6.42 KB
/
Copy pathwc-attributes-on-page.php
File metadata and controls
executable file
·190 lines (152 loc) · 6.42 KB
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
189
190
<?php
/*
Plugin Name: WooCommerce Variation Details on Page Product
Plugin URI: https://github.com/pereirinha/woocommerce-variation-details-on-page-product
Description: Display physical size and weight of product within product meta details.
Version: 3.4.1
Author: Marco Pereirinha
Author URI: http://www.linkedin.com/in/marcopereirinha
WC requires at least: 2.4
WC tested up to: 4.0.0
*/
// Check if WooCommerce is active
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
if ( ! class_exists( 'MP_WC_Variation_Details_On_Page_Product' ) ) {
class MP_WC_Variation_Details_On_Page_Product {
// Definition of version
const VERSION = '3.4.1';
const VERSION_OPTION_NAME = 'mp_wc_vdopp_version';
public $plugin_prefix;
private $settings;
private $variations;
private $debug;
public function __construct() {
$this->plugin_prefix = 'mp_wc_vdopp';
$this->old_option_name = 'mp_wc_vdopp_keys';
$this->debug = WP_DEBUG;
}
public function load() {
add_action( 'init', array( $this, 'load_hooks' ) );
add_filter( 'woocommerce_before_add_to_cart_form', array( &$this, 'create_vars' ), 10 );
// Load the JS
add_filter( 'wp_enqueue_scripts', array( &$this, 'register_scripts' ), 15 );
// Load text domain
add_action( 'plugins_loaded', array( &$this, 'load_plugin_textdomain' ) );
// Installation
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
$this->install();
}
}
private function includes() {
include_once( 'classes/class-mp-wc-vdopp-settings.php' );
include_once( 'classes/class-mp-wc-vdopp-shortcodes.php' );
}
public function load_hooks() {
$this->includes();
$this->settings = new MP_WC_Variation_Details_On_Page_Product_Settings();
$this->settings->load();
$this->shortcodes = new MP_WC_Variation_Details_On_Page_Product_Shortcodes();
$this->shortcodes->load();
}
public function create_vars() {
global $product;
if ( ! is_product() || ! $product->is_type( 'variable' ) ) {
return;
}
$att_data_hook = get_option( 'mp_wc_vdopp_data_hook' ); // Hook data
$att_data_sel = get_option( 'mp_wc_vdopp_data_selector' ); // Data Selector
$att_before_size = apply_filters( 'mp_wc_vdopp_before_size' , rtrim( get_option( 'mp_wc_vdopp_before_size' ) ) ) . ' ';
$att_before_weight = apply_filters( 'mp_wc_vdopp_before_weight', rtrim( get_option( 'mp_wc_vdopp_before_weight' ) ) ) . ' ';
$att_after_size = apply_filters( 'mp_wc_vdopp_after_size' , ' ' . ltrim( get_option( 'mp_wc_vdopp_after_size' ) ) );
$att_after_weight = apply_filters( 'mp_wc_vdopp_after_weight' , ' ' . ltrim( get_option( 'mp_wc_vdopp_after_weight' ) ) );
$children = null;
// New method as of WC 2.7.0
if ( method_exists( $product, 'get_visible_children' ) ) {
$children = $product->get_visible_children( true );
}
if ( null === $children) {
$children = $product->get_children( true );
}
$index = 0;
foreach ( $children as $value ) {
$product_variatons = new WC_Product_Variation( $value );
if ( $product_variatons->exists() && $product_variatons->variation_is_visible() ) {
$variations = $product_variatons->get_variation_attributes();
$dimensions = $product_variatons->get_dimensions( false );
// New method as of WC 2.7.0
if ( is_array( $dimensions ) ) {
$dimensions = wc_format_dimensions( $dimensions );
}
foreach ( $variations as $key => $variation ) {
$this->variations[ $index ][ $key ] = array(
$variation,
sanitize_title( $variation )
);
$this->variations[ $index ][ $key ] = array_filter( $this->variations[ $index ][ $key ] );
}
$weight = $product_variatons->get_weight();
if ( $weight ) {
$weight .= get_option( 'woocommerce_weight_unit' );
}
$this->variations[ $index ]['weight'] = $weight ;
$this->variations[ $index ]['dimensions'] = str_replace( ' ', '', $dimensions );
$index++;
}
}
$this->variations = wp_json_encode( $this->variations );
$params = array(
'variations' => $this->variations,
'att_data_hook' => $att_data_hook,
'att_data_sel' => $att_data_sel,
'att_before_size' => $att_before_size,
'att_before_weight' => $att_before_weight,
'att_after_size' => $att_after_size,
'att_after_weight' => $att_after_weight,
);
// enqueue the script
wp_enqueue_script( 'mp_wc_variation_details' );
wp_localize_script( 'mp_wc_variation_details', 'mp_wc_variations', $params );
}
public function register_scripts() {
$min = 'min.';
if ( $this->debug ) {
$min = '';
}
$js_url = plugins_url( 'js/wc-attributes-on-page.' . $min . 'js', __FILE__ );
$js_file = WP_PLUGIN_DIR . '/woocommerce-variation-details-on-page-product/js/wc-attributes-on-page.' . $min . 'js';
if ( file_exists( $js_file ) ) {
// register your script location, dependencies and version
wp_register_script( 'mp_wc_variation_details', $js_url, array() );
}
}
public function load_plugin_textdomain() {
load_plugin_textdomain( 'mp_wc_vdopp', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
}
/** Lifecycle methods **/
// Run every time. Used since the activation hook is not executed when updating a plugin
private function install() {
$installed_version = get_option( MP_WC_Variation_Details_On_Page_Product::VERSION_OPTION_NAME );
if ( ! $installed_version ) {
// initial install, set the version of the plugin on options table
add_option( $installed_version, MP_WC_Variation_Details_On_Page_Product::VERSION_OPTION_NAME );
}
if ( MP_WC_Variation_Details_On_Page_Product::VERSION !== $installed_version ) {
$this->upgrade( $installed_version );
// new version number
update_option( MP_WC_Variation_Details_On_Page_Product::VERSION_OPTION_NAME, MP_WC_Variation_Details_On_Page_Product::VERSION );
}
}
// Run when plugin version number changes
private function upgrade( $installed_version ) {
if ( get_option( $this->old_option_name ) ) {
delete_option( $this->old_option_name );
}
if ( get_option( 'mp_wc_vdopp_dom_selector' ) ) {
delete_option( 'mp_wc_vdopp_dom_selector' );
}
}
}
}
$mp_wc_vdopp = new MP_WC_Variation_Details_On_Page_Product;
$mp_wc_vdopp->load();
}