Skip to content

Commit 5030e63

Browse files
authored
feat(3d-model-viewer): add more options for model viewer, support smartstone (#166)
1 parent 2d112ca commit 5030e63

File tree

5 files changed

+95
-15
lines changed

5 files changed

+95
-15
lines changed

src/acore-wp-plugin/src/Hooks/WooCommerce/FieldElements.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,18 @@ public static function destAccount(): void {
114114
<?php
115115
}
116116

117-
public static function get3dViewer(int $itemId): void {
117+
public static function get3dViewer(int $itemId = 0): void {
118118

119119
global $post;
120120
$custom_3d_checkbox = get_post_meta($post->ID, '_custom_3d_checkbox', true);
121+
$race = get_post_meta($post->ID, '_3d_race', true);
122+
$gender = get_post_meta($post->ID, '_3d_gender', true);
123+
$gender = $gender === '2' ? rand(0, 1) : $gender;
124+
$creatureDisplayId = get_post_meta($post->ID, '_3d_displayid', true);
125+
126+
if ($itemId === 0 && $creatureDisplayId === '') {
127+
return;
128+
}
121129

122130
if ($custom_3d_checkbox !== 'yes') {
123131
return;
@@ -129,7 +137,7 @@ public static function get3dViewer(int $itemId): void {
129137
<script type="module">
130138
import { generateModels } from "<?= ACORE_URL_PLG . "web/libraries/wow-model-viewer/index.js" ?>";
131139

132-
function show3dModel(displayId, entity, inventoryType, race=1, gender=0) {
140+
function show3dModel(displayId, entity, inventoryType=0, race=1, gender=0) {
133141
let model;
134142
if (entity === 'item') {
135143
const character = {
@@ -161,12 +169,20 @@ function show3dModel(displayId, entity, inventoryType, race=1, gender=0) {
161169
generateModels(1, `#${wow3dviewerId}`, model);
162170
}
163171

172+
<?php if ($creatureDisplayId !== '') { ?>
173+
show3dModel(<?= $creatureDisplayId ?>, 'npc');
174+
<?php } else { ?>
164175
fetch('https://wowgaming.altervista.org/modelviewer/data/get-displayid.php?type=item&id=<?= $itemId ?>')
165176
.then(response => response.text())
166177
.then(data => {
167178
const [displayId, entity, inventoryType] = data.split(',');
168-
show3dModel(displayId, entity, inventoryType);
179+
<?php if ($race !== '' && $gender !== '') { ?>
180+
show3dModel(displayId, entity, inventoryType, <?= $race ?>, <?= $gender ?>);
181+
<?php } else { ?>
182+
show3dModel(displayId, entity, inventoryType);
183+
<?php } ?>
169184
});
185+
<?php } ?>
170186
</script>
171187

172188
<?php
Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,81 @@
11
<?php
22

3-
/* 3D viewer custom fields */
4-
function add_custom_3d_checkbox_field() {
3+
/* 3D viewer custom section and fields */
4+
5+
// Section 3D viewer under product settings
6+
add_filter('woocommerce_product_data_tabs', 'add_3d_viewer_tab');
7+
function add_3d_viewer_tab($tabs) {
8+
$tabs['3d_viewer_tab'] = array(
9+
'label' => __('3D viewer', 'woocommerce'), // Etichetta della scheda
10+
'target' => '3d_viewer_product_data',
11+
'class' => array('show_if_simple', 'show_if_variable'), // Tipi di prodotto in cui mostrare la scheda
12+
);
13+
return $tabs;
14+
}
15+
16+
// add 3D viewer options
17+
add_action('woocommerce_product_data_panels', 'add_custom_3d_checkbox_fields');
18+
function add_custom_3d_checkbox_fields() {
19+
echo '<div id="3d_viewer_product_data" class="panel woocommerce_options_panel hidden">';
20+
21+
// enable/disable 3D
522
woocommerce_wp_checkbox( array(
623
'id' => '_custom_3d_checkbox',
724
'label' => __('3D Viewer', 'woocommerce'),
825
'description' => __('Check this box to enable the 3D viewer for this product.', 'woocommerce'),
926
));
10-
}
11-
add_action('woocommerce_product_options_inventory_product_data', 'add_custom_3d_checkbox_field');
1227

28+
woocommerce_wp_text_input(array(
29+
'id' => '_3d_displayid',
30+
'label' => __('Force displayid', 'woocommerce'),
31+
'description' => __('Enter a specific Creature displayid.', 'woocommerce'),
32+
'desc_tip' => 'true',
33+
));
34+
35+
woocommerce_wp_select(array(
36+
'id' => '_3d_race',
37+
'label' => __('Race (optional)', 'woocommerce'),
38+
'options' => array(
39+
'1' => __('Human', 'woocommerce'),
40+
'2' => __('Orc', 'woocommerce'),
41+
'3' => __('Dwarf', 'woocommerce'),
42+
'4' => __('Nightelf', 'woocommerce'),
43+
'5' => __('Undead', 'woocommerce'),
44+
'6' => __('Tauren', 'woocommerce'),
45+
'7' => __('Gnome', 'woocommerce'),
46+
'8' => __('Troll', 'woocommerce'),
47+
// '9' => __('Goblin', 'woocommerce'),
48+
'10' => __('Bloodelf', 'woocommerce'),
49+
'11' => __('Draenei', 'woocommerce'),
50+
'0' => __('Random', 'woocommerce'),
51+
),
52+
));
53+
54+
woocommerce_wp_select(array(
55+
'id' => '_3d_gender',
56+
'label' => __('Gender (optional)', 'woocommerce'),
57+
'options' => array(
58+
'2' => __('Random', 'woocommerce'),
59+
'0' => __('Male', 'woocommerce'),
60+
'1' => __('Female', 'woocommerce'),
61+
),
62+
));
63+
64+
echo '</div>';
65+
}
1366

67+
add_action('woocommerce_process_product_meta', 'save_3d_checkbox_field');
1468
function save_3d_checkbox_field($post_id) {
1569
$custom_checkbox = isset($_POST['_custom_3d_checkbox']) ? 'yes' : 'no';
1670
update_post_meta($post_id, '_custom_3d_checkbox', $custom_checkbox);
71+
72+
$custom_text_input = isset($_POST['_3d_displayid']) ? sanitize_text_field($_POST['_3d_displayid']) : '';
73+
update_post_meta($post_id, '_3d_displayid', $custom_text_input);
74+
75+
$custom_select = isset($_POST['_3d_race']) ? sanitize_text_field($_POST['_3d_race']) : '';
76+
update_post_meta($post_id, '_3d_race', $custom_select);
77+
78+
$custom_select = isset($_POST['_3d_gender']) ? sanitize_text_field($_POST['_3d_gender']) : '';
79+
update_post_meta($post_id, '_3d_gender', $custom_select);
1780
}
18-
add_action('woocommerce_process_product_meta', 'save_3d_checkbox_field');
1981
?>

src/acore-wp-plugin/src/Hooks/WooCommerce/Smartstone.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public static function before_add_to_cart_button() {
3636
return;
3737
}
3838

39+
FieldElements::get3dViewer();
40+
3941
$current_user = wp_get_current_user();
4042

4143
if ($current_user) {

src/acore-wp-plugin/web/libraries/wow-model-viewer/character_modeling.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,12 @@ function getCharacterOptions(character, fullOptions) {
111111
}
112112
ret.push(newOption);
113113
}
114-
console.warn(
115-
`In character: `,
116-
character,
117-
`the following options are missing`,
118-
missingChoice
119-
);
114+
// console.warn(
115+
// `In character: `,
116+
// character,
117+
// `the following options are missing`,
118+
// missingChoice
119+
// );
120120

121121
return ret;
122122
}

src/acore-wp-plugin/web/libraries/wow-model-viewer/setup.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class WebP {
1111
if (!window.WH) {
1212
window.WH = {};
1313
window.WH.debug = function (...args) {
14-
console.log(args);
14+
// console.log(args);
1515
};
1616
window.WH.defaultAnimation = `Stand`;
1717
window.WH.WebP = new WebP();

0 commit comments

Comments
 (0)