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

Commit

Permalink
Import Synchronisation option
Browse files Browse the repository at this point in the history
addresses #208 with optional Feature to synchronise translations when
using WooCommerce 3.1 CSV Import/Export to import update
addresses #183 - correctly synchronises Product Category and Product
Tag settings on save of woo-poly features
Fixes #207 - allow direct access to order, for example when customer
has the order link, avoid redirecting logon to account home page
  • Loading branch information
Jon007 committed Jul 2, 2017
1 parent b10f940 commit 3d09512
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 11 deletions.
14 changes: 12 additions & 2 deletions src/Hyyan/WPI/Admin/Features.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ protected function doGetFields()
'default' => 'on',
'label' => __('Fields Locker', 'woo-poly-integration'),
'desc' => __(
'Fields locker makes it easy for user to know which
field to translate and which to ignore ', 'woo-poly-integration'
'Locks Meta fields which are set to be synchronized.', 'woo-poly-integration'
),
),
array(
Expand Down Expand Up @@ -158,6 +157,17 @@ protected function doGetFields()
. '</a>.'
,
),
array(
'name' => 'importsync',
'type' => 'checkbox',
'default' => 'on',
'label' => __('Synchronize on Import', 'woo-poly-integration'),
'desc' => ' <a target="_blank" href="https://github.com/hyyan/woo-poly-integration/wiki/Upload,-Bulk-Edit,-Quick-Edit"> ' .
__(
'When using WooCommerce 3.1 importer to importing updates to existing items, apply synchronization rules to update any existing translations.', 'woo-poly-integration')
. '</a>.'
,
),
);
}
}
5 changes: 4 additions & 1 deletion src/Hyyan/WPI/Login.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ class Login
*/
public function __construct()
{
/*
add_filter(
'woocommerce_login_redirect', array($this, 'getLoginRedirectPermalink')
'woocommerce_login_redirect', array($this, 'getLoginRedirectPermalink'), 10, 2
);
*
*/
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Hyyan/WPI/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ protected function registerCore()
new Emails();
new Admin\Settings();
new Cart();
new Login();
// new Login();
new Order();
new Pages();
new Endpoints();
Expand Down
49 changes: 48 additions & 1 deletion src/Hyyan/WPI/Product/Meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Hyyan\WPI\HooksInterface;
use Hyyan\WPI\Utilities;
use Hyyan\WPI\Admin\Settings;
use Hyyan\WPI\Admin\Features;
use Hyyan\WPI\Admin\MetasList;
use Hyyan\WPI\Taxonomies\Attributes;

Expand Down Expand Up @@ -43,13 +44,59 @@ public function __construct()
'wc_product_has_unique_sku',
array($this, 'suppressInvalidDuplicatedSKUErrorMsg'), 100, 3
);

if ('on' === Settings::getOption('importsync', Features::getID(), 'on')) {
add_action( 'woocommerce_product_import_inserted_product_object', array($this, 'onImport'), 10, 2);
}

//if translate attributes feature is 'on',
if ('on' === Settings::getOption('attributes', Features::getID(), 'on')) {
add_action( 'woocommerce_attribute_added', array($this, 'newProductAttribute'), 10, 2);
}
}


/**
* On insert of a new product attribute, attempt to set it to translateable by default
*
* @param integer $insert_id id of attribute
* @param Array $attribute array of attribute data, see get_posted_attribute()
*/
public function newProductAttribute($insert_id, $attribute)
{
$options = get_option('polylang');
$sync = $options['taxonomies'];
$attrname = 'pa_' . $attribute['attribute_name'];
if (!in_array($attribute, $sync)) {
$options['taxonomies'][] = $attrname;
update_option('polylang', $options);
}
}

/**
* On Import, attempt synchronization of any existing translations
*
* @param [product] $object array of product ids
* @param Array $data data in import
*/
public function onImport($object, $data )
{
// sync product meta with polylang
add_filter('pll_copy_post_metas', array(__CLASS__, 'getProductMetaToCopy'));

//sync taxonomies
$ProductID = $object->get_id();
if ($ProductID){
do_action( 'pll_save_post', $ProductID, $object,
PLL()->model->post->get_translations( $ProductID ));

$this->syncTaxonomiesAndProductAttributes($ProductID, $object, true);
}
}
/**
* catch save from QuickEdit
*
* @param WC_Product $product
* @param WC_Product $product
*/
public function saveQuickEdit(\WC_Product $product)
{
Expand Down
40 changes: 34 additions & 6 deletions src/Hyyan/WPI/Taxonomies/Taxonomies.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,37 @@ public function updatePolyLangFromWooPolyMetas($old_value, $new_value, $option)
*/
public function updatePolyLangFromWooPolyFeatures($old_value, $new_value, $option)
{
$polylang_options = get_option('polylang');
$polylang_taxs = $polylang_options['taxonomies'];
$update=false;

//check Polylang is in sync for Product category and tag translation
if (( isset($new_value['categories']) ) && ( $new_value['categories']=='on' ) ) {
if (! in_array('product_cat', $polylang_taxs) ){
$polylang_options['taxonomies'][] = 'product_cat';
$update=true;
}
} else {
$key = array_search('product_cat', $polylang_taxs);
if ($key!==false){ //key may be zero which is different from false
unset($polylang_options['taxonomies'][$key]);
$update=true;
}
}
if (( isset($new_value['tags']) ) && ( $new_value['tags']=='on' ) ) {
if (! in_array('product_tag', $polylang_taxs) ){
$polylang_options['taxonomies'][] = 'product_tag';
$update=true;
}
} else {
$key = array_search('product_tag', $polylang_taxs);
if ($key!==false){
unset($polylang_options['taxonomies'][$key]);
$update=true;
}
}

//for attributes don't force on for all attributes but do force off when disabled
if (isset($old_value['attributes']) && isset($new_value['attributes'])) {
$old_attr_sync = $old_value['attributes'];
$new_attr_sync = $new_value['attributes'];
Expand All @@ -124,10 +155,7 @@ public function updatePolyLangFromWooPolyFeatures($old_value, $new_value, $optio
//now we will not force translation on, only force off, ie:
// remove from Polylang if disabling translation
if ($new_attr_sync!='on') {
$polylang_options = get_option('polylang');
$polylang_taxs = $polylang_options['taxonomies'];
$remove = Attributes::getNames();
$update=false;
foreach ($remove as $tax) {
if (in_array($tax, $polylang_taxs)) {
$polylang_options['taxonomies'] = array_flip($polylang_options['taxonomies']);
Expand All @@ -136,12 +164,12 @@ public function updatePolyLangFromWooPolyFeatures($old_value, $new_value, $optio
$update = true;
} //if Product Attribute was previously translated
} //for each Product Attribute
if ($update) {
update_option('polylang', $polylang_options);
}
} //if wooPoly Translate Product Attributes is turned On
} //if attributes setting has changed
} //if attributes are set
if ($update) {
update_option('polylang', $polylang_options);
}
}


Expand Down

0 comments on commit 3d09512

Please sign in to comment.