-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsetup.php
More file actions
142 lines (121 loc) · 4.54 KB
/
Copy pathsetup.php
File metadata and controls
142 lines (121 loc) · 4.54 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
<?php
use ImportWP\Common\Importer\ParsedData;
use ImportWPAddon\WooCommerce\Importer\Mapper\CustomerMapper;
use ImportWPAddon\WooCommerce\Importer\Mapper\OrderMapper;
use ImportWPAddon\WooCommerce\Importer\Mapper\ProductMapper;
use ImportWPAddon\WooCommerce\Importer\Template\CustomerTemplate;
use ImportWPAddon\WooCommerce\Importer\Template\OrderTemplate;
use ImportWPAddon\WooCommerce\Importer\Template\ProductTemplate;
// TODO: Try disabling term recount to speed up import: 'woocommerce_product_recount_terms'
add_action('iwp/register_events', function ($event_handler) {
$event_handler->listen('templates.register', 'iwp_woocommerce_register_templates');
$event_handler->listen('mappers.register', 'iwp_woocommerce_register_mappers');
$event_handler->listen('template.post_process', 'iwp_woocommerce_register_template_post_process');
});
/**
* Remove default woocommerce category on insert when other categories have been added.
* Apply WooCommerce customer billing/shipping addresses after user import.
*
* @param int $post_id
* @param ParsedData $data
* @param mixed $template
* @return void
*/
function iwp_woocommerce_register_template_post_process($post_id, $data, $template)
{
if ($template instanceof CustomerTemplate) {
$template->apply_customer_addresses($post_id, $data);
return $post_id;
}
if (!($template instanceof ProductTemplate)) {
return;
}
// check importer product categories
$tax = 'product_cat';
$imported_taxonomies = $template->get_importer_taxonomies();
$product_cats = isset($imported_taxonomies[$tax]) ? $imported_taxonomies[$tax] : [];
if (!empty($product_cats)) {
$default_product_cat = intval(get_option('default_product_cat'));
$terms = wp_get_object_terms($post_id, $tax);
if (count($terms) >= count($product_cats)) {
$found = false;
foreach ($terms as $i => $term) {
if ($term->term_id === intval($default_product_cat)) {
$found = true;
}
}
if ($found === true) {
wp_remove_object_terms($post_id, $default_product_cat, $tax, true);
}
}
}
return $post_id;
}
function iwp_woocommerce_register_templates($templates)
{
$templates['woocommerce-product'] = ProductTemplate::class;
$templates['woocommerce-customer'] = CustomerTemplate::class;
$templates['woocommerce-order'] = OrderTemplate::class;
return $templates;
}
function iwp_woocommerce_register_mappers($mappers)
{
$mappers['woocommerce-product'] = ProductMapper::class;
$mappers['woocommerce-customer'] = CustomerMapper::class;
$mappers['woocommerce-order'] = OrderMapper::class;
return $mappers;
}
function iwp_woocommerce_mapper_unique_fields($unique_fields, $mapper_id)
{
if ($mapper_id == 'woocommerce-product') {
return ['ID', '_sku', 'post_name'];
}
if ($mapper_id == 'woocommerce-customer') {
return ['ID', 'user_email', 'user_login'];
}
if ($mapper_id == 'woocommerce-order') {
return ['ID', '_order_key'];
}
return $unique_fields;
}
add_filter('iwp/mapper/unique_fields', 'iwp_woocommerce_mapper_unique_fields', 10, 2);
/**
* Map exporter unique identifier field names onto importer unique field ids.
*
* Exporter product fields use `sku`; the importer unique field is `_sku`.
* Customer/order exporter ids already match importer unique fields.
*
* @param string $unique_identifier
* @param string $exporter_unique_identifier
* @param \ImportWP\Common\Model\ImporterModel $importer
* @return string
*/
function iwp_woocommerce_from_exporter_unique_identifier($unique_identifier, $exporter_unique_identifier, $importer)
{
if (!is_object($importer) || !method_exists($importer, 'getTemplate')) {
return $unique_identifier;
}
$aliases = [
'woocommerce-product' => [
'sku' => '_sku',
],
];
$template = $importer->getTemplate();
if (isset($aliases[$template][$exporter_unique_identifier])) {
return $aliases[$template][$exporter_unique_identifier];
}
return $unique_identifier;
}
add_filter('iwp/importer/from_exporter/unique_identifier', 'iwp_woocommerce_from_exporter_unique_identifier', 10, 3);
/**
* Add WooCommerce plugin to compatability whitelist
*
* @param string[] $plugins
* @return string[]
*/
function iwp_woocommerce_compat_whitelist($plugins)
{
$plugins[] = 'woocommerce/woocommerce.php';
return $plugins;
}
add_filter('iwp/compat/whitelist', 'iwp_woocommerce_compat_whitelist');