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

Commit

Permalink
fixes #432 default attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon007 committed Jun 23, 2019
1 parent 245f20d commit 7101bcf
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 50 deletions.
24 changes: 21 additions & 3 deletions src/Hyyan/WPI/Product/Meta.php
Expand Up @@ -6,6 +6,8 @@
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
* TODO: consider a version of PLL_Sync_Post_Metas for products,
* suppress the postmeta actions and use the woocommerce api
*/
namespace Hyyan\WPI\Product;

Expand Down Expand Up @@ -82,7 +84,7 @@ public function newProductAttribute($insert_id, $attribute)
public function onImport($object, $data)
{
// sync product meta with polylang
add_filter('pll_copy_post_metas', array(__CLASS__, 'getProductMetaToCopy'));
add_filter('pll_copy_post_metas', array( __CLASS__, 'wpi_filter_pll_metas' ), 10, 5 );

//sync taxonomies
$ProductID = $object->get_id();
Expand All @@ -93,6 +95,22 @@ public function onImport($object, $data)
$this->syncTaxonomiesAndProductAttributes($ProductID, $object, true);
}
}

/*
* filter pll_copy_post_metas
*
* @param array $keys keys Polylang thinks should be copied
* @param int $from Id of the post from which we copy informations
* @param int $to Id of the post to which we paste informations
* @param string $lang Language slug
* @param bool $sync True if it is synchronization, false if it is a copy
*/
public static function wpi_filter_pll_metas( $keys, $from, $to, $lang, $sync ) {
$wpi_keys = static::getProductMetaToCopy( $keys );
$wpi_keys = array_diff( $wpi_keys, array( '_default_attributes' ) );
return $wpi_keys;
}

/**
* catch save from QuickEdit
*
Expand All @@ -101,7 +119,7 @@ public function onImport($object, $data)
public function saveQuickEdit(\WC_Product $product)
{
// sync product meta with polylang
add_filter('pll_copy_post_metas', array(__CLASS__, 'getProductMetaToCopy'));
add_filter('pll_copy_post_metas', array( __CLASS__, 'wpi_filter_pll_metas' ), 10, 5 );

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

// sync product meta with polylang
add_filter('pll_copy_post_metas', array(__CLASS__, 'getProductMetaToCopy'));
add_filter('pll_copy_post_metas', array( __CLASS__, 'wpi_filter_pll_metas' ), 10, 5 );

// new code to synchronise Taxonomies and Product attributes applied to WooCommerce 3.0
// which now moves product_visibility from meta to taxonomy
Expand Down
50 changes: 11 additions & 39 deletions src/Hyyan/WPI/Product/Variable.php
Expand Up @@ -146,55 +146,27 @@ public function skipDefaultAttributesMeta($check, $object_id, $meta_key, $meta_v
// Ignore if not 'default attribute' meta
if ('_default_attributes' === $meta_key) {
$product = wc_get_product($object_id);
$current_filter = current_filter();

// Don't let anyone delete the meta. NO ONE!
if ($product && current_filter() === 'delete_post_metadata') {
if ( $product && $current_filter === 'delete_post_metadata' ) {
return false;
}

// _default_attributes meta should be unique
if ($product && current_filter() === 'add_post_metadata') {
if ($product && $current_filter === 'add_post_metadata') {
$old_value = get_post_meta($product->get_id(), '_default_attributes');
return empty($old_value) ? $check : false;
}

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

if (!$lang) {
// Must be a new translation and Polylang doesn't stored the language yet
$lang = isset($_GET['new_lang']) ? $_GET['new_lang'] : '';
}

foreach ($meta_value as $key => $value) {
//TODO JM: get_term_by is filtered by Polylang, so
//will not retrieve data if the term is not in the correct language
//so the rest of the check does not execute as expected
//(it is not possible to get the term without knowing the language,
// and not possible to get the translation without getting the term)
// the fix is the additional return false which prevents save of the incorrect version when Polylang attempts to synchronise it
$term = get_term_by('slug', $value, $key);

if ($term && pll_is_translated_taxonomy($term->taxonomy)) {
if ($translated_term_id = pll_get_term($term->term_id, $lang)) {
$translated_term = get_term_by('id', $translated_term_id, $term->taxonomy);

// If meta is taxonomy managed by Polylang and is in the
// correct language continue, otherwise return false to
// stop execution
return ($value === $translated_term->slug) ? $check : false;
} else {
// Attribute term has no translation
return false;
}
}
// Attribute is in wrong language and must not be saved
return false;
}
}
/* #432: this check was partially incorrect and
* is no longer needed after removing _default_attributes
* from the list of meta synchronised by Polylang.
* (another way of doing it would be to hook the polylang filter
* in sync-metas maybe_translate_value
* and translate the default attributes there, but that is bigger change to this plugin
*/
}

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

if ($translation_id != $product->get_id()) {
if ( $translation_id && $translation_id != $product->get_id()) {
update_post_meta($translation_id, '_default_attributes', $attributes_translation[$lang]);
}
}
Expand Down
16 changes: 8 additions & 8 deletions src/Hyyan/WPI/Utilities.php
Expand Up @@ -266,14 +266,14 @@ public static function getDefaultAttributesTranslation($product_id, $lang = '')
'slug' => $value,
'lang' => pll_get_post_language( $product_id )
);
$terms = get_terms( $args );
if ( $terms && ( ! is_wp_error( $terms ) ) ) {
$term = array_shift( $terms );

if ($term && pll_is_translated_taxonomy($term->taxonomy)) {
$terms[] = $term;
} else {
$terms[] = array($key => $value);
$this_attr_terms = get_terms( $args );
if ( $this_attr_terms && ( ! is_wp_error( $this_attr_terms ) ) ) {
$term = array_shift( $this_attr_terms );

if ($term && pll_is_translated_taxonomy($term->taxonomy)) {
$terms[] = $term;
} else {
$terms[] = array($key => $value);
}
} else {
$terms = array( array( $key => $value ) );
Expand Down

0 comments on commit 7101bcf

Please sign in to comment.