Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

COR-2038 change fetch product image URL logic in catalog cron sync #190

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

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
Contributor

@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
Contributor

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
Contributor

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
Contributor

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
Contributor

@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?

Model/Sync/Catalog/Data.php Outdated Show resolved Hide resolved
Model/Sync/Catalog/Data.php Outdated Show resolved Hide resolved

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

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

Model/Sync/Catalog/Data.php Outdated Show resolved Hide resolved
Model/Sync/Catalog/Data.php Show resolved Hide resolved
@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
Contributor

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.

Copy link
Contributor

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
Contributor

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.

Copy link
Contributor

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