Skip to content
This repository was archived by the owner on Mar 17, 2022. It is now read-only.

Commit 7101bcf

Browse files
committed
fixes #432 default attributes
1 parent 245f20d commit 7101bcf

File tree

3 files changed

+40
-50
lines changed

3 files changed

+40
-50
lines changed

src/Hyyan/WPI/Product/Meta.php

+21-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
*
77
* For the full copyright and license information, please view the LICENSE
88
* file that was distributed with this source code.
9+
* TODO: consider a version of PLL_Sync_Post_Metas for products,
10+
* suppress the postmeta actions and use the woocommerce api
911
*/
1012
namespace Hyyan\WPI\Product;
1113

@@ -82,7 +84,7 @@ public function newProductAttribute($insert_id, $attribute)
8284
public function onImport($object, $data)
8385
{
8486
// sync product meta with polylang
85-
add_filter('pll_copy_post_metas', array(__CLASS__, 'getProductMetaToCopy'));
87+
add_filter('pll_copy_post_metas', array( __CLASS__, 'wpi_filter_pll_metas' ), 10, 5 );
8688

8789
//sync taxonomies
8890
$ProductID = $object->get_id();
@@ -93,6 +95,22 @@ public function onImport($object, $data)
9395
$this->syncTaxonomiesAndProductAttributes($ProductID, $object, true);
9496
}
9597
}
98+
99+
/*
100+
* filter pll_copy_post_metas
101+
*
102+
* @param array $keys keys Polylang thinks should be copied
103+
* @param int $from Id of the post from which we copy informations
104+
* @param int $to Id of the post to which we paste informations
105+
* @param string $lang Language slug
106+
* @param bool $sync True if it is synchronization, false if it is a copy
107+
*/
108+
public static function wpi_filter_pll_metas( $keys, $from, $to, $lang, $sync ) {
109+
$wpi_keys = static::getProductMetaToCopy( $keys );
110+
$wpi_keys = array_diff( $wpi_keys, array( '_default_attributes' ) );
111+
return $wpi_keys;
112+
}
113+
96114
/**
97115
* catch save from QuickEdit
98116
*
@@ -101,7 +119,7 @@ public function onImport($object, $data)
101119
public function saveQuickEdit(\WC_Product $product)
102120
{
103121
// sync product meta with polylang
104-
add_filter('pll_copy_post_metas', array(__CLASS__, 'getProductMetaToCopy'));
122+
add_filter('pll_copy_post_metas', array( __CLASS__, 'wpi_filter_pll_metas' ), 10, 5 );
105123

106124
//some taxonomies can actually be changed in the QuickEdit
107125
$this->syncTaxonomiesAndProductAttributes($product->get_id(), $product, true);
@@ -121,7 +139,7 @@ public function syncProductsMeta()
121139
}
122140

123141
// sync product meta with polylang
124-
add_filter('pll_copy_post_metas', array(__CLASS__, 'getProductMetaToCopy'));
142+
add_filter('pll_copy_post_metas', array( __CLASS__, 'wpi_filter_pll_metas' ), 10, 5 );
125143

126144
// new code to synchronise Taxonomies and Product attributes applied to WooCommerce 3.0
127145
// which now moves product_visibility from meta to taxonomy

src/Hyyan/WPI/Product/Variable.php

+11-39
Original file line numberDiff line numberDiff line change
@@ -146,55 +146,27 @@ public function skipDefaultAttributesMeta($check, $object_id, $meta_key, $meta_v
146146
// Ignore if not 'default attribute' meta
147147
if ('_default_attributes' === $meta_key) {
148148
$product = wc_get_product($object_id);
149+
$current_filter = current_filter();
149150

150151
// Don't let anyone delete the meta. NO ONE!
151-
if ($product && current_filter() === 'delete_post_metadata') {
152+
if ( $product && $current_filter === 'delete_post_metadata' ) {
152153
return false;
153154
}
154155

155156
// _default_attributes meta should be unique
156-
if ($product && current_filter() === 'add_post_metadata') {
157+
if ($product && $current_filter === 'add_post_metadata') {
157158
$old_value = get_post_meta($product->get_id(), '_default_attributes');
158159
return empty($old_value) ? $check : false;
159160
}
160161

161-
// Maybe is Variable Product
162-
// New translations of Variable Products are first created as simple
163-
if ($product && Utilities::maybeVariableProduct($product)) {
164-
// Try Polylang first
165-
$lang = pll_get_post_language($product->get_id());
166162

167-
if (!$lang) {
168-
// Must be a new translation and Polylang doesn't stored the language yet
169-
$lang = isset($_GET['new_lang']) ? $_GET['new_lang'] : '';
170-
}
171-
172-
foreach ($meta_value as $key => $value) {
173-
//TODO JM: get_term_by is filtered by Polylang, so
174-
//will not retrieve data if the term is not in the correct language
175-
//so the rest of the check does not execute as expected
176-
//(it is not possible to get the term without knowing the language,
177-
// and not possible to get the translation without getting the term)
178-
// the fix is the additional return false which prevents save of the incorrect version when Polylang attempts to synchronise it
179-
$term = get_term_by('slug', $value, $key);
180-
181-
if ($term && pll_is_translated_taxonomy($term->taxonomy)) {
182-
if ($translated_term_id = pll_get_term($term->term_id, $lang)) {
183-
$translated_term = get_term_by('id', $translated_term_id, $term->taxonomy);
184-
185-
// If meta is taxonomy managed by Polylang and is in the
186-
// correct language continue, otherwise return false to
187-
// stop execution
188-
return ($value === $translated_term->slug) ? $check : false;
189-
} else {
190-
// Attribute term has no translation
191-
return false;
192-
}
193-
}
194-
// Attribute is in wrong language and must not be saved
195-
return false;
196-
}
197-
}
163+
/* #432: this check was partially incorrect and
164+
* is no longer needed after removing _default_attributes
165+
* from the list of meta synchronised by Polylang.
166+
* (another way of doing it would be to hook the polylang filter
167+
* in sync-metas maybe_translate_value
168+
* and translate the default attributes there, but that is bigger change to this plugin
169+
*/
198170
}
199171

200172
return $check;
@@ -250,7 +222,7 @@ public function syncDefaultAttributes($post_id, $post, $update)
250222
foreach ($langs as $lang) {
251223
$translation_id = pll_get_post($product->get_id(), $lang);
252224

253-
if ($translation_id != $product->get_id()) {
225+
if ( $translation_id && $translation_id != $product->get_id()) {
254226
update_post_meta($translation_id, '_default_attributes', $attributes_translation[$lang]);
255227
}
256228
}

src/Hyyan/WPI/Utilities.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -266,14 +266,14 @@ public static function getDefaultAttributesTranslation($product_id, $lang = '')
266266
'slug' => $value,
267267
'lang' => pll_get_post_language( $product_id )
268268
);
269-
$terms = get_terms( $args );
270-
if ( $terms && ( ! is_wp_error( $terms ) ) ) {
271-
$term = array_shift( $terms );
272-
273-
if ($term && pll_is_translated_taxonomy($term->taxonomy)) {
274-
$terms[] = $term;
275-
} else {
276-
$terms[] = array($key => $value);
269+
$this_attr_terms = get_terms( $args );
270+
if ( $this_attr_terms && ( ! is_wp_error( $this_attr_terms ) ) ) {
271+
$term = array_shift( $this_attr_terms );
272+
273+
if ($term && pll_is_translated_taxonomy($term->taxonomy)) {
274+
$terms[] = $term;
275+
} else {
276+
$terms[] = array($key => $value);
277277
}
278278
} else {
279279
$terms = array( array( $key => $value ) );

0 commit comments

Comments
 (0)