Skip to content

Commit

Permalink
Merge branch 'feature/CO-2704_gpsr_data_with_germanized' into 'master'
Browse files Browse the repository at this point in the history
feature/CO-2704_gpsr_data_with_germanized

See merge request connector/woocommerce!86
  • Loading branch information
cem-jtl committed Nov 6, 2024
2 parents 599e13b + bece014 commit 1a94cd0
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ This is the changelog of our "JTL WooCommerce Connector".
## Unreleased
- CO-2305 - update sql queries
- CO-2507 - pass tracking url from jtl-wawi
- CO-2704 - add germanized gpsr push logic

## 2.0.6.1 _2024-10-24_
- HOTFIX Update release files
Expand Down
155 changes: 155 additions & 0 deletions src/Controllers/Product/ProductGermanizedFieldsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ private function setGermanizedAttributes(ProductModel &$product, \WC_Product $wc
/**
* @param ProductModel $product
* @return void
* @throws TranslatableAttributeException
*/
public function pushData(ProductModel $product): void
{
Expand All @@ -133,6 +134,7 @@ public function pushData(ProductModel $product): void
/**
* @param ProductModel $product
* @return void
* @throws TranslatableAttributeException
*/
private function updateGermanizedAttributes(ProductModel &$product): void
{
Expand All @@ -141,6 +143,7 @@ private function updateGermanizedAttributes(ProductModel &$product): void
\update_post_meta($id, '_ts_mpn', (string)$product->getManufacturerNumber());

$this->updateGermanizedBasePriceAndUnits($product, $id);
$this->updateGermanizedGpsrData($product);

if ($this->isGermanizedProFoodProduct($product)) {
$this->updateGermanizedProFoodProductData($product);
Expand Down Expand Up @@ -266,6 +269,158 @@ private function getGermanizedProFoodMetaKeys(): array
];
}

/**
* @param ProductModel $product
* @return void
* @throws TranslatableAttributeException
*/
private function updateGermanizedGpsrData(ProductModel $product): void
{
$gpsrManufacturerName = '';
$gpsrManufacturerTitleform = '';

$manufacturerData = [
'street' => '',
'housenumber' => '',
'postalcode' => '',
'city' => '',
'state' => '',
'country' => '',
'email' => '',
'homepage' => ''
];

$responsiblePersonData = [
'name' => '',
'street' => '',
'housenumber' => '',
'postalcode' => '',
'city' => '',
'state' => '',
'country' => '',
'email' => '',
'homepage' => ''
];

foreach ($product->getAttributes() as $attribute) {
foreach ($attribute->getI18ns() as $i18n) {
if ($this->util->isWooCommerceLanguage($i18n->getLanguageIso())) {
switch ($i18n->getName()) {
case 'gpsr_manufacturer_name':
$gpsrManufacturerName = $i18n->getValue();
$gpsrManufacturerTitleform = \strtolower(
\str_replace(' ', '', $i18n->getValue())
) . '-gpsr';
break;
case 'gpsr_manufacturer_street':
$manufacturerData['street'] = $i18n->getValue();
break;
case 'gpsr_manufacturer_housenumber':
$manufacturerData['housenumber'] = $i18n->getValue();
break;
case 'gpsr_manufacturer_postalcode':
$manufacturerData['postalcode'] = $i18n->getValue();
break;
case 'gpsr_manufacturer_city':
$manufacturerData['city'] = $i18n->getValue();
break;
case 'gpsr_manufacturer_state':
$manufacturerData['state'] = $i18n->getValue();
break;
case 'gpsr_manufacturer_country':
$manufacturerData['country'] = $i18n->getValue();
break;
case 'gpsr_manufacturer_email':
$manufacturerData['email'] = $i18n->getValue();
break;
case 'gpsr_manufacturer_homepage':
$manufacturerData['homepage'] = $i18n->getValue();
break;
case 'gpsr_responsibleperson_name':
$responsiblePersonData['name'] = $i18n->getValue();
break;
case 'gpsr_responsibleperson_street':
$responsiblePersonData['street'] = $i18n->getValue();
break;
case 'gpsr_responsibleperson_housenumber':
$responsiblePersonData['housenumber'] = $i18n->getValue();
break;
case 'gpsr_responsibleperson_postalcode':
$responsiblePersonData['postalcode'] = $i18n->getValue();
break;
case 'gpsr_responsibleperson_city':
$responsiblePersonData['city'] = $i18n->getValue();
break;
case 'gpsr_responsibleperson_state':
$responsiblePersonData['state'] = $i18n->getValue();
break;
case 'gpsr_responsibleperson_country':
$responsiblePersonData['country'] = $i18n->getValue();
break;
case 'gpsr_responsibleperson_email':
$responsiblePersonData['email'] = $i18n->getValue();
break;
case 'gpsr_responsibleperson_homepage':
$responsiblePersonData['homepage'] = $i18n->getValue();
break;
}
}
}
}

if ($gpsrManufacturerName === '') {
\wp_delete_object_term_relationships($product->getId()->getEndpoint(), 'product_manufacturer');
\update_post_meta($product->getId()->getEndpoint(), '_manufacturer_slug', '');

return;
}

$existingTerm = \get_term_by('slug', $gpsrManufacturerTitleform, 'product_manufacturer');
if (!$existingTerm) {
$newTerm = \wp_insert_term(
$gpsrManufacturerName,
'product_manufacturer',
[
'description' => '',
'slug' => $gpsrManufacturerTitleform,
]
);

$termId = $newTerm['term_id'];
} else {
$termId = $existingTerm->term_id;
}

$gpsrManufacturerAddress = $manufacturerData['street'] . ' ' . $manufacturerData['housenumber'] . "\n"
. $manufacturerData['postalcode'] . ' ' . $manufacturerData['city'] . "\n"
. $manufacturerData['state'] . ' ' . $manufacturerData['country'] . "\n"
. $manufacturerData['email'] . "\n"
. $manufacturerData['homepage'];

$gpsrResponsibleAddress = $responsiblePersonData['name'] . "\n"
. $responsiblePersonData['street'] . ' ' . $responsiblePersonData['housenumber'] . "\n"
. $responsiblePersonData['postalcode'] . ' ' . $responsiblePersonData['city'] . "\n"
. $responsiblePersonData['state'] . ' ' . $responsiblePersonData['country'] . "\n"
. $responsiblePersonData['email'] . "\n"
. $responsiblePersonData['homepage'];

if (!empty(\str_replace([' ', "\n"], '', $gpsrManufacturerAddress))) {
\update_term_meta($termId, 'formatted_address', $gpsrManufacturerAddress);
}

if (!empty(\str_replace([' ', "\n"], '', $gpsrResponsibleAddress))) {
\update_term_meta($termId, 'formatted_eu_address', $gpsrResponsibleAddress);
}

#remove existing product to gpsr manufacturer link
\wp_delete_object_term_relationships($product->getId()->getEndpoint(), 'product_manufacturer');

#link product to gpsr manufacturer
\wp_set_object_terms($product->getId()->getEndpoint(), $termId, 'product_manufacturer');
\update_post_meta($product->getId()->getEndpoint(), '_manufacturer_slug', $gpsrManufacturerTitleform);
}


/**
* @param $product ProductModel
* @param $value
Expand Down

0 comments on commit 1a94cd0

Please sign in to comment.