Skip to content

Instantly share code, notes, and snippets.

@stefansl
Last active February 3, 2019 14:04
Show Gist options
  • Save stefansl/b6cdce5ab7093bafe718ecb33c220bdd to your computer and use it in GitHub Desktop.
Save stefansl/b6cdce5ab7093bafe718ecb33c220bdd to your computer and use it in GitHub Desktop.
Magnalister Hook / Amazon - merge description with open text fields in engine/Shopware/Plugins/Community/Backend/RedMagnalister/Lib/Codepool/10_Customer/Hooks/Hook/additem_1.php
<?php
// Merge Shopware description w/ Attribute 5
if ($sMarketplaceName === 'amazon') {
// $aProduct = MLProduct::factory()->set('id', $iMagnalisterProductsId);
// MLMessage::gi()->addInfo($aAddItemData['Variations'], get_defined_vars());
// MLMessage::gi()->addInfo($aProduct, get_defined_vars());
// MLMessage::gi()->addInfo($iMagnalisterProductsId, get_defined_vars());
foreach ($aAddItemData['Variations'] as $sVariant) {
if ($sVariant['EAN'] === $aAddItemData['EAN']) {
$oProduct = MLProduct::factory()->getByMarketplaceSKU($sVariant['SKU']);
if ($oProduct->exists() && isset($aAddItemData['ShippingTime'])) {
$attr5 = AmazonConverter::convertHtml($oProduct->getAttributeValue('a_attr5'));
$description = $aAddItemData['Description'];
$aAddItemData['Description'] = $attr5 . $description;
}
}
}
}
/**
* Class AmazonConverter
* provides methods to convert HTML to valid Amazon HTML
*/
class AmazonConverter
{
/**
* Convert Shopware desc to valid Amazon Desc
*
* @param string $str complete product description in html
*
* @return string
*/
public static function convertHtml($str)
{
// Remove divs and convert to html line breaks
$str_replaced = preg_replace('/<div class=".*">/i', '', $str);
$str_replaced = preg_replace('/<\/div>|<p>|<\/p>/i', '<br>', $str_replaced);
// Remove double and more whitespaces, tabs an line breaks
$str_replaced = self::removeHidden($str_replaced);
// Remove not allowed tags
$str_replaced = strip_tags($str_replaced, '<br><br/>');
$str = '<b>Technische Daten:</b><br>' . $str_replaced;
return $str;
}
/**
* Convert data to html table
*
* @param string $str complete product description in html
*
* @return string
*/
public static function convertHtmlToTable($str)
{
$str_replaced = $str;
// Remove double and more whitespaces, tabs an line breaks
$str_replaced = self::removeHidden($str_replaced);
// Convert to table
$str_replaced = preg_replace('/<div class="description--variant">/i', '<table>', $str_replaced);
$str_replaced = preg_replace('/<div class="row">/i', '<tr>', $str_replaced);
$str_replaced = preg_replace('/<div class="key">(.+?)<\/div>/i', '<td>$1</td>', $str_replaced);
$str_replaced = preg_replace('/<div class="value">(.+?)<\/div>/i', '<td>$1</td>', $str_replaced);
$str_replaced = preg_replace('/<\/td><\/div>/i', '</td></tr>', $str_replaced);
$str_replaced = preg_replace('/<\/tr><\/div>/i', '</tr></table>', $str_replaced);
return $str_replaced;
}
/**
* Remove double and more whitespaces, tabs an line breaks
*
* @param string $str complete product description in html
*
* @return string
*/
public static function removeHidden($str)
{
$str_replaced = trim(
preg_replace(
array('/\s{2,}/', '/\r/', '/\n/', '/[\r\n]/', '/[\t\n]/'),
'',
$str
)
);
return $str_replaced;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment