/
class-ThreeKit.php
97 lines (76 loc) · 2.99 KB
/
class-ThreeKit.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
<?php
/**
* Copyright: © 2017 Exocortex
* License: MIT License
* License URI: https://opensource.org/licenses/MIT
**/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
global $product;
class ThreeKit {
protected $clarauuid;
protected $JSONConfig;
protected $attributes;
public function __construct() {
}
/*********** Callback Functions *****************/
public function enable_threekit_by_checking_clarauuid_attribute() {
global $product;
$product_type = $product->get_type();
// only works for variable product for now
if (strcmp($product_type, 'variable')) {
return;
}
$attributes = $product->get_attributes();
foreach ( $attributes as $attribute_name => $options ) {
// enable clara player and configurator when attribute clarauuid exist
if (!strcmp($attribute_name, 'pa_clarauuid')) {
$this->clarauuid = $product->get_attribute($attribute_name);
if (!empty($this->clarauuid)) {
// generate varations JSON config
$this->JSONConfig = $this->variation_attributes_to_JSON($product->get_available_variations());
$this->attributes = $product->get_variation_attributes();
// load template
$this->replace_product_template_with_clara();
}
}
}
}
public function embed_clara_player() {
load_template(rtrim(plugin_dir_path(__FILE__),'/') . '/templates/single-product/clara-player.php');
}
public function embed_clara_configurator() {
load_template(rtrim(plugin_dir_path(__FILE__),'/') . '/templates/single-product/add-to-cart/clara-variation.php');
// load scripts to init clara player
wp_enqueue_script( 'claraConfigurator', rtrim(plugin_dir_url(__FILE__),'/') . '/assets/js/threekit/claraConfigurator.js');
$dataToBePassed = array(
'clarauuid' => $this->clarauuid,
'available_attributes' => $this->JSONConfig,
'attributes' => $this->attributes,
'basePrice' => $product->get_price(),
'usingAddons' => false
);
// variables will be json encoded here
wp_localize_script('claraConfigurator', 'php_vars', $dataToBePassed);
}
/*********** Internal Functions *****************/
/* Rearrange templates by modifying hook
* woocommerce_before_single_product_summary
* woocommerce_single_product_summary
*/
protected function replace_product_template_with_clara() {
// remove existing contents
remove_all_actions('woocommerce_before_single_product_summary');
remove_all_actions('woocommerce_single_product_summary');
// add clara player
wp_enqueue_script( 'claraplayer', 'https://clara.io/js/claraplayer.min.js');
add_action('woocommerce_before_single_product_summary', array($this, 'embed_clara_player'), 40);
add_action('woocommerce_before_single_product_summary', array($this, 'embed_clara_configurator'), 50);
}
// convert $product->get_variation_attributes() to JSON
protected function variation_attributes_to_JSON($variations) {
return $variations;
}
}
?>