-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwoocommerce-csvimport-wpml.php
147 lines (110 loc) · 3.97 KB
/
woocommerce-csvimport-wpml.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
<?php
/*
Plugin Name: woocommerce csvimport wpml add-on
#Plugin URI: http://allaerd.org/
Description: WPML support
Version: beta
Author: Allaerd Mensonides
License: GPLv2 or later
Author URI: http://allaerd.org
*/
$woocsvWPML = new woocsvWPML();
class woocsvWPML
{
public function __construct()
{
global $woocsv_import;
//set number of rows to process to 1 else WPML screws up
$woocsv_import->options['blocksize'] = 1;
//is wpml active?
if ( function_exists('icl_object_id') ) {
//sync the products
add_action('woocsv_after_save',array($this,'woocsv_after_save'),9999);
//make sure you have the ID of the product in the main language
add_filter('woocsv_get_product_id',array($this,'woocsv_get_product_id'),10,3);
}
//set up menu
add_action('admin_menu', array($this, 'adminMenu'));
//set up make duplicates
//add_action('save_post', array($this,'save_post'), 11, 2);
}
public function woocsv_get_product_id($product_id, $sku) {
global $wpdb,$sitepress;
//get the ID for the post in the main language
$product_id = $wpdb->get_var($wpdb->prepare(
"
SELECT post_id
FROM {$wpdb->prefix}postmeta a, {$wpdb->prefix}posts b, {$wpdb->prefix}icl_translations c
WHERE a.post_id= b.id
and meta_key='_sku'
and meta_value=%s
and c.element_id = b.ID
and c.language_code = %s
",
$sku,
$sitepress->get_default_language()
));
if ($product_id) $product['ID'] = $product_id; else $product_id = false;
return $product_id;
}
public function woocsv_after_save(){
global $woocsv_product, $woocommerce_wpml,$sitepress,$iclTranslationManagement;
//make duplicates if products are new
if ( $woocsv_product->new ) {
@$iclTranslationManagement->make_duplicates_all($woocsv_product->body['ID']);
return;
}
//run sync for translations
$product_id = $woocsv_product->body['ID'];
$trid = $sitepress->get_element_trid($product_id,'post_product');
$translations = $sitepress->get_element_translations($trid);
foreach ($translations as $translation) {
//if this is the original continue to the next
if ( $translation->original ==1 )
continue;
//ok, we are in a translation....do your thing
$tr_product_id = $translation->element_id;
$lang = $translation->language_code;
//sync post_meta
$woocommerce_wpml->products->duplicate_product_post_meta($product_id,$tr_product_id);
//duplicate product attrs
$orig_product_attrs = $woocommerce_wpml->products->get_product_atributes($product_id);
add_post_meta($tr_product_id,'_product_attributes',$orig_product_attrs);
//sync default product attrs
$woocommerce_wpml->products->sync_default_product_attr($product_id, $tr_product_id, $lang);
//sync media
$woocommerce_wpml->products->sync_thumbnail_id($product_id, $tr_product_id,$lang);
$woocommerce_wpml->products->sync_product_gallery($product_id);
//sync taxonomies
$woocommerce_wpml->products->sync_product_taxonomies($product_id,$tr_product_id,$lang);
//duplicate variations
$woocommerce_wpml->products->sync_product_variations($product_id,$tr_product_id,$lang);
}
}
public function save_post($post_id) {
global $woocsv_product,$iclTranslationManagement;
//check if we are not in the regular save post flow
if (empty($woocsv_product))
return;
//remove the action else it will run in a loop!
remove_action( 'save_post', array( $this, 'save_post' ),11,2 );
//if the product is new, make duplicates
if ( true == $woocsv_product->new )
$iclTranslationManagement->make_duplicates_all($post_id);
//add the hook again for the next product
add_action('save_post', array($this,'save_post'), 11, 2);
}
public function adminMenu()
{
add_submenu_page( 'woocsv_import', 'WPML', 'WPML', 'manage_options', 'woocsvWPML', array($this, 'addToAdmin'));
}
function addToAdmin()
{
?>
<div class="wrap">
<h2>WPML support</h2>
<p>Beta version. Will copy and sync your products to all languages</p>
</div>
<?php
}
}