This repository has been archived by the owner on Jan 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
woocommerce-3Dconfigurator.php
164 lines (125 loc) · 4.29 KB
/
woocommerce-3Dconfigurator.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
<?php
/**
* Plugin Name: WooCommerce 3D Configurator
* Description: Custom WooCommerce plugin for treespoke.com 3D configurator products
* Plugin URI: https://github.com/TVBZ/woocommerce-3Dconfigurator
* Author: Tom F. Vanbrabant
* Version: 0.5
*/
// Restrict direct access
defined('ABSPATH') || exit;
/*********************************
*** Create custom post type ***
*********************************/
add_action('init', 'create_3Dconfigurator_custom_post_type');
function create_3Dconfigurator_custom_post_type()
{
// Set labels for custom post type
$labels = array(
'name' => __('3D Configurators'),
'singular_name' => __('3D Configurator'),
'add_new_item' => __('Add New 3D Configurator'),
'edit_item' => __('Edit 3D Configurator'),
'new_item' => __('New 3D Configurator'),
'all_items' => __('All 3D Configurators'),
'view_item' => __('View 3D Configurator'),
'menu_name' => __('3D Configurators'),
'update_item' => __('Update 3D Configurator'),
);
// Add custom post type
register_post_type('3D-configurator', array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'supports' => array(
'title',
'editor',
'thumbnail'
),
'has_archive' => false,
'rewrite' => array(
'slug' => 'configureer',
//'with_front' => false
),
'hierarchical' => false,
));
}
/****************************
*** Build custom fields ***
****************************/
add_action('woocommerce_product_options_general_product_data', 'woocommerce_create_custom_treespoke_fields');
function woocommerce_create_custom_treespoke_fields()
{
global $woocommerce, $post;
// Get posts from 3D-configurator post type
$posts_type_3Dconfigurators = get_posts(array(
'post_type' => '3D-configurator',
'post_status' => 'publish',
'posts_per_page' => -1
));
// Set active (selected) configurator option
$current_configurator = NULL;
$excisting_configurator = get_post_meta($post->ID, 'path_3Dconfigurator', true);
if ($excisting_configurator != NULL) $current_configurator = $excisting_configurator;
// Initialize array for dropdown options
$dropdown_options = array(
NULL => __('None', 'woocommerce')
);
// Add excisting posts to array
foreach ($posts_type_3Dconfigurators as $post) {
$option_value = __($post->post_title, 'woocommerce');
$dropdown_options[$post->ID] = $option_value;
};
// Open fields container
echo '<div>';
// Add configurator targets dropdown field
woocommerce_wp_select(array(
'id' => 'path_3Dconfigurator',
'label' => __('3D configurator', 'woocommerce'),
'selected' => true,
'value' => $current_configurator,
'options' => $dropdown_options,
'desc_tip' => 'false'
));
// Add product UID field
woocommerce_wp_text_input(array(
'id' => 'product_UID',
'placeholder' => '',
'label' => __('3D product UID', 'woocommerce'),
'desc_tip' => 'false'
));
// Close fields container
echo '</div>';
}
/*********************************
*** Save fields to database ***
*********************************/
add_action('woocommerce_process_product_meta', 'woocommerce_save_custom_treespoke_fields');
function woocommerce_save_custom_treespoke_fields($post_id)
{
// Save configurator path
$configurator_path = $_POST['path_3Dconfigurator'];
if (!empty($configurator_path)) update_post_meta($post_id, 'path_3Dconfigurator', esc_attr($configurator_path));
else update_post_meta($post_id, 'path_3Dconfigurator', '');
// Save UID field
$product_uid = $_POST['product_UID'];
if (!empty($product_uid)) update_post_meta($post_id, 'product_UID', esc_attr($product_uid));
else update_post_meta($post_id, 'product_UID', '');
}
/*****************************************
*** Redirect single 3D product page ***
*****************************************/
add_action('wp', 'redirect_3Dconfigurator_single_product_page');
function redirect_3Dconfigurator_single_product_page()
{
if (is_product()) { // if the page is a single product page
global $product, $post;
$id_configurator = get_post_meta($post->ID, 'path_3Dconfigurator', true);
if ($id_configurator != NULL) { // if this product has a 3D configurator target
$uid = get_post_meta($post->ID, 'product_UID', true);
$target = get_post_permalink($id_configurator) . '?uid=' . $uid;
wp_redirect($target, 301); // redirect to configurator target url
exit();
}
}
}