Skip to content

Conversation

@LiorGingi
Copy link
Contributor

No description provided.

@LiorGingi LiorGingi changed the title COR-2038 fetch product image URL COR-2038 change fetch product image URL logic in catalog cron sync Apr 25, 2022
Copy link

@DanielxC DanielxC left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work!

if (isset($attr['type']) && $attr['type'] === 'image') {
$baseUrl = $this->yotpoCoreConfig->getBaseUrl(UrlInterface::URL_TYPE_MEDIA);
$data = $data ? $baseUrl . 'catalog/product' . $data : "";
if ($key == 'group_name' && $value) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This case can be added to the switch case with inner if condition

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please explain how?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if ($key == 'group_name' && $value) {
$value = strtolower($value);
$value = str_replace(' ', '_', $value);
$value = preg_replace('/[^A-Za-z0-9_-]/', '-', $value);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this line do? It seems like everything regex search

Copy link
Contributor Author

@LiorGingi LiorGingi May 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It replaces everything that is not alphanumeric, underscore, or dash with a dash.

Copy link

@DanielxC DanielxC May 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation. Is this the expected behaviour? do we want it to replace !@#$%^&*()+= these for example to dash?


$method = $attr['method'];
$itemValue = $this->$method($item, $configKey);
$value = $itemValue ?: ($method == 'getProductPrice' ? 0.00 : $itemValue);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this can be reduced to one oneliner condition intead of nested condition

@LiorGingi LiorGingi force-pushed the COR-2038-fetch-product-image-url branch from 6d8150b to 1203c01 Compare May 7, 2022 21:39

if (isset($attr['type']) && $attr['type'] === 'url') {
$data = $item->getProductUrl();
switch ($attributeKey) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
switch ($attributeKey) {
$attributeDataValue = '';
$attributeDetailsAttributeCode = '';
$attributeDetailsMethod = $attributeDetails['method'];
if (isset($attributeDetails['attr_code']) && $attributeDetails['attr_code']) {
$attributeDetailsAttributeCode = $attributeDetails['attr_code'];
}
$itemValue = $this->$attributeDetailsMethod($item, $attributeDetailsAttributeCode);
switch ($attributeKey) {
case 'gtins':
$attributeDataValue = $this->prepareGtinsData($attributeDetails, $item);
break;
case 'custom_properties':
$attributeDataValue = $this->prepareCustomProperties($attributeDetails, $item);
break;
case 'is_discontinued':
$attributeDataValue = false;
break;
case 'url':
$attributeDataValue = $item->getProductUrl();
break;
case 'image_url':
$attributeDataValue = $this->getProductImageUrl($item);
break;
case 'group_name':
if ($itemValue) {
$attributeDataValue = substr((string)$itemValue, 0, 100);
$attributeDataValue = strtolower($attributeDataValue);
$attributeDataValue = str_replace(' ', '_', $attributeDataValue);
$attributeDataValue = preg_replace('/[^A-Za-z0-9_-]/', '-', $attributeDataValue);
}
break;
default:
if (!$attributeDetails['default'] && isset($attributeDetails['method']) && $attributeDetails['method']) {
if ($itemValue) {
$attributeDataValue = $itemValue;
} elseif ($attributeDetailsMethod == 'getProductPrice') {
$attributeDataValue = 0.00;
} else {
$attributeDataValue = $itemValue;
}
}
}
$itemAttributesData[$attributeKey] = $attributeDataValue;
if (($attributeKey == 'custom_properties' || $attributeKey == 'gtins') && !$attributeDataValue) {
unset($itemAttributesData[$attributeKey]);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't agree with this change. You've basically extracted what we had in the default clause, but it's not relevant for all of the action types.
It looks better for sure, but it degregates performance.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I disagree with that. Because default clause shouldn't check for other cases. Its a bad practice.
But I agree on the performance concern. I created a new branch with a refactor commit so it will be better performance wise (and easier for you to read than a suggestion). Let me know what you think. We can also talk about it in a call.
7f554dc

Comment on lines +409 to +415
if ($itemValue) {
$value = $itemValue;
} elseif ($method == 'getProductPrice') {
$value = 0.00;
} else {
$value = $itemValue;
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we changed the logic now no? Isn't it should be:

Suggested change
if ($itemValue) {
$value = $itemValue;
} elseif ($method == 'getProductPrice') {
$value = 0.00;
} else {
$value = $itemValue;
}
if ($method == 'getProductPrice') {
$value = 0.00;
} else {
$value = $itemValue;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that your suggestion is the one changing the logic.
The one I wrote broke down the one-liner we had there.
According to what you wrote, all getProductPrice will get a value of 0.00.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, my bad. maybe lets change it then to:

if (!$itemValue && $method == 'getProductPrice') {
    $value = 0.00;
}  else {
    $value = $itemValue;
}

the three ifs condition is confusing. wdyt?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants