How to pass added custom “key=>value” into getCollection()?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
On product lisitng page i want to get one more parameter "latitude" for all product,how can i achieve?
Here is my code:-
public function setCollection($collection)
{
$this->_collection = $collection;
$mpJoinConditions = 'marketplace_product.mageproduct_id = e.entity_id';
$this->_collection->getSelect()->join(
$this->_collection->getTable('marketplace_product'),
$mpJoinConditions,
)->columns("marketplace_product.seller_id")->where ("marketplace_product.store_id = '0'");
echo "<pre>" ; print_r($this->_collection->getData()) ; exit;
}
Note:-As per my requirement,i don't need to create product custom attribute OR use joins.
I tried to use addData and setData() but didn't get success.
After print_r(),added custom "key=>value" must be there for each product.

Update:-
By doing following i'm able to do set Latitude seprately and getting the same on phtml file:-
foreach($this->_collection as $key=>$prod_data){
$prod_data->setData('latitude',$prod_data->getId());
}
I've to sort(DESC) the product based on latitude value,but when i'm printing
echo "<pre>" ; print_r($this->_collection->getData()) ; exit;
then not getting 'latitude' parameter $this->_collection->getData() here due to which i can't apply setOrder().on phtml file by using $product->getLatitude() it's showing so please help me to resolve this issue.
collection product-collection magento2.2.4 category-view
add a comment |
On product lisitng page i want to get one more parameter "latitude" for all product,how can i achieve?
Here is my code:-
public function setCollection($collection)
{
$this->_collection = $collection;
$mpJoinConditions = 'marketplace_product.mageproduct_id = e.entity_id';
$this->_collection->getSelect()->join(
$this->_collection->getTable('marketplace_product'),
$mpJoinConditions,
)->columns("marketplace_product.seller_id")->where ("marketplace_product.store_id = '0'");
echo "<pre>" ; print_r($this->_collection->getData()) ; exit;
}
Note:-As per my requirement,i don't need to create product custom attribute OR use joins.
I tried to use addData and setData() but didn't get success.
After print_r(),added custom "key=>value" must be there for each product.

Update:-
By doing following i'm able to do set Latitude seprately and getting the same on phtml file:-
foreach($this->_collection as $key=>$prod_data){
$prod_data->setData('latitude',$prod_data->getId());
}
I've to sort(DESC) the product based on latitude value,but when i'm printing
echo "<pre>" ; print_r($this->_collection->getData()) ; exit;
then not getting 'latitude' parameter $this->_collection->getData() here due to which i can't apply setOrder().on phtml file by using $product->getLatitude() it's showing so please help me to resolve this issue.
collection product-collection magento2.2.4 category-view
would you like to return product from above function array (like displaying) or object?
– Hassan Ali Shahzad
yesterday
thanks for the reply! I want to add in a way so that after doing print_r() "latitude" field added for each product. we've to add this filed for entire product collection.
– wakar Ahamad
yesterday
add a comment |
On product lisitng page i want to get one more parameter "latitude" for all product,how can i achieve?
Here is my code:-
public function setCollection($collection)
{
$this->_collection = $collection;
$mpJoinConditions = 'marketplace_product.mageproduct_id = e.entity_id';
$this->_collection->getSelect()->join(
$this->_collection->getTable('marketplace_product'),
$mpJoinConditions,
)->columns("marketplace_product.seller_id")->where ("marketplace_product.store_id = '0'");
echo "<pre>" ; print_r($this->_collection->getData()) ; exit;
}
Note:-As per my requirement,i don't need to create product custom attribute OR use joins.
I tried to use addData and setData() but didn't get success.
After print_r(),added custom "key=>value" must be there for each product.

Update:-
By doing following i'm able to do set Latitude seprately and getting the same on phtml file:-
foreach($this->_collection as $key=>$prod_data){
$prod_data->setData('latitude',$prod_data->getId());
}
I've to sort(DESC) the product based on latitude value,but when i'm printing
echo "<pre>" ; print_r($this->_collection->getData()) ; exit;
then not getting 'latitude' parameter $this->_collection->getData() here due to which i can't apply setOrder().on phtml file by using $product->getLatitude() it's showing so please help me to resolve this issue.
collection product-collection magento2.2.4 category-view
On product lisitng page i want to get one more parameter "latitude" for all product,how can i achieve?
Here is my code:-
public function setCollection($collection)
{
$this->_collection = $collection;
$mpJoinConditions = 'marketplace_product.mageproduct_id = e.entity_id';
$this->_collection->getSelect()->join(
$this->_collection->getTable('marketplace_product'),
$mpJoinConditions,
)->columns("marketplace_product.seller_id")->where ("marketplace_product.store_id = '0'");
echo "<pre>" ; print_r($this->_collection->getData()) ; exit;
}
Note:-As per my requirement,i don't need to create product custom attribute OR use joins.
I tried to use addData and setData() but didn't get success.
After print_r(),added custom "key=>value" must be there for each product.

Update:-
By doing following i'm able to do set Latitude seprately and getting the same on phtml file:-
foreach($this->_collection as $key=>$prod_data){
$prod_data->setData('latitude',$prod_data->getId());
}
I've to sort(DESC) the product based on latitude value,but when i'm printing
echo "<pre>" ; print_r($this->_collection->getData()) ; exit;
then not getting 'latitude' parameter $this->_collection->getData() here due to which i can't apply setOrder().on phtml file by using $product->getLatitude() it's showing so please help me to resolve this issue.
collection product-collection magento2.2.4 category-view
collection product-collection magento2.2.4 category-view
edited yesterday
wakar Ahamad
asked yesterday
wakar Ahamadwakar Ahamad
329113
329113
would you like to return product from above function array (like displaying) or object?
– Hassan Ali Shahzad
yesterday
thanks for the reply! I want to add in a way so that after doing print_r() "latitude" field added for each product. we've to add this filed for entire product collection.
– wakar Ahamad
yesterday
add a comment |
would you like to return product from above function array (like displaying) or object?
– Hassan Ali Shahzad
yesterday
thanks for the reply! I want to add in a way so that after doing print_r() "latitude" field added for each product. we've to add this filed for entire product collection.
– wakar Ahamad
yesterday
would you like to return product from above function array (like displaying) or object?
– Hassan Ali Shahzad
yesterday
would you like to return product from above function array (like displaying) or object?
– Hassan Ali Shahzad
yesterday
thanks for the reply! I want to add in a way so that after doing print_r() "latitude" field added for each product. we've to add this filed for entire product collection.
– wakar Ahamad
yesterday
thanks for the reply! I want to add in a way so that after doing print_r() "latitude" field added for each product. we've to add this filed for entire product collection.
– wakar Ahamad
yesterday
add a comment |
2 Answers
2
active
oldest
votes
You can loop through the collection and set the data for each item like this:
foreach ($this->_collection as $item){
$item->setData('latitude',1);
}
That should give you the required output.
keep in mind that just doing this will not persist the data in the DB
– Philipp Sander
yesterday
The question was not about persisting data or did I miss something?
– HelgeB
yesterday
1
you are right. I just wanted to mention this
– Philipp Sander
yesterday
@HelgeB thanks for the reply,i already tried but it doesn't work. i've to carry this filed somehere for further calculation.i've to use the same in product sorting.
– wakar Ahamad
yesterday
1
@wakar Ahamad please update the question with the concrete requirement: Adding a fixed field to the collection works as mentioned in the answer, that data can be used everywhere where you use the collection data. "Further calculation" is quite unclear and sorting a collection is again a different thing.
– HelgeB
yesterday
|
show 5 more comments
Try this code & let me know:
public function setCollection($collection)
{
$this->_collection = $collection;
$mpJoinConditions = 'marketplace_product.mageproduct_id = e.entity_id';
$this->_collection->getSelect()->join(
$this->_collection->getTable('marketplace_product'),
$mpJoinConditions,
)->columns("marketplace_product.seller_id")->where ("marketplace_product.store_id = '0'");
//add custom column
$this->_collection->getSelect()->columns(['latitude' => 1]);
echo "<pre>" ; print_r($this->_collection->getData()) ; exit;
}
@wakar-ahmad Is this working ?
– Hassan Ali Shahzad
yesterday
thanks for the reply,it's not working,getting some error.sorry my mistake 'latitude' value can be vary based on some calculation.
– wakar Ahamad
yesterday
ok is it possible for you to return array instead of object from function ? like: return $this->_collection->getData();
– Hassan Ali Shahzad
yesterday
Actually i've to do sorting using "latitude" so i don't think so.
– wakar Ahamad
yesterday
how you will add value to your latitude?
– Hassan Ali Shahzad
yesterday
|
show 5 more comments
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "479"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f269535%2fhow-to-pass-added-custom-key-value-into-getcollection%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can loop through the collection and set the data for each item like this:
foreach ($this->_collection as $item){
$item->setData('latitude',1);
}
That should give you the required output.
keep in mind that just doing this will not persist the data in the DB
– Philipp Sander
yesterday
The question was not about persisting data or did I miss something?
– HelgeB
yesterday
1
you are right. I just wanted to mention this
– Philipp Sander
yesterday
@HelgeB thanks for the reply,i already tried but it doesn't work. i've to carry this filed somehere for further calculation.i've to use the same in product sorting.
– wakar Ahamad
yesterday
1
@wakar Ahamad please update the question with the concrete requirement: Adding a fixed field to the collection works as mentioned in the answer, that data can be used everywhere where you use the collection data. "Further calculation" is quite unclear and sorting a collection is again a different thing.
– HelgeB
yesterday
|
show 5 more comments
You can loop through the collection and set the data for each item like this:
foreach ($this->_collection as $item){
$item->setData('latitude',1);
}
That should give you the required output.
keep in mind that just doing this will not persist the data in the DB
– Philipp Sander
yesterday
The question was not about persisting data or did I miss something?
– HelgeB
yesterday
1
you are right. I just wanted to mention this
– Philipp Sander
yesterday
@HelgeB thanks for the reply,i already tried but it doesn't work. i've to carry this filed somehere for further calculation.i've to use the same in product sorting.
– wakar Ahamad
yesterday
1
@wakar Ahamad please update the question with the concrete requirement: Adding a fixed field to the collection works as mentioned in the answer, that data can be used everywhere where you use the collection data. "Further calculation" is quite unclear and sorting a collection is again a different thing.
– HelgeB
yesterday
|
show 5 more comments
You can loop through the collection and set the data for each item like this:
foreach ($this->_collection as $item){
$item->setData('latitude',1);
}
That should give you the required output.
You can loop through the collection and set the data for each item like this:
foreach ($this->_collection as $item){
$item->setData('latitude',1);
}
That should give you the required output.
answered yesterday
HelgeBHelgeB
3,1931323
3,1931323
keep in mind that just doing this will not persist the data in the DB
– Philipp Sander
yesterday
The question was not about persisting data or did I miss something?
– HelgeB
yesterday
1
you are right. I just wanted to mention this
– Philipp Sander
yesterday
@HelgeB thanks for the reply,i already tried but it doesn't work. i've to carry this filed somehere for further calculation.i've to use the same in product sorting.
– wakar Ahamad
yesterday
1
@wakar Ahamad please update the question with the concrete requirement: Adding a fixed field to the collection works as mentioned in the answer, that data can be used everywhere where you use the collection data. "Further calculation" is quite unclear and sorting a collection is again a different thing.
– HelgeB
yesterday
|
show 5 more comments
keep in mind that just doing this will not persist the data in the DB
– Philipp Sander
yesterday
The question was not about persisting data or did I miss something?
– HelgeB
yesterday
1
you are right. I just wanted to mention this
– Philipp Sander
yesterday
@HelgeB thanks for the reply,i already tried but it doesn't work. i've to carry this filed somehere for further calculation.i've to use the same in product sorting.
– wakar Ahamad
yesterday
1
@wakar Ahamad please update the question with the concrete requirement: Adding a fixed field to the collection works as mentioned in the answer, that data can be used everywhere where you use the collection data. "Further calculation" is quite unclear and sorting a collection is again a different thing.
– HelgeB
yesterday
keep in mind that just doing this will not persist the data in the DB
– Philipp Sander
yesterday
keep in mind that just doing this will not persist the data in the DB
– Philipp Sander
yesterday
The question was not about persisting data or did I miss something?
– HelgeB
yesterday
The question was not about persisting data or did I miss something?
– HelgeB
yesterday
1
1
you are right. I just wanted to mention this
– Philipp Sander
yesterday
you are right. I just wanted to mention this
– Philipp Sander
yesterday
@HelgeB thanks for the reply,i already tried but it doesn't work. i've to carry this filed somehere for further calculation.i've to use the same in product sorting.
– wakar Ahamad
yesterday
@HelgeB thanks for the reply,i already tried but it doesn't work. i've to carry this filed somehere for further calculation.i've to use the same in product sorting.
– wakar Ahamad
yesterday
1
1
@wakar Ahamad please update the question with the concrete requirement: Adding a fixed field to the collection works as mentioned in the answer, that data can be used everywhere where you use the collection data. "Further calculation" is quite unclear and sorting a collection is again a different thing.
– HelgeB
yesterday
@wakar Ahamad please update the question with the concrete requirement: Adding a fixed field to the collection works as mentioned in the answer, that data can be used everywhere where you use the collection data. "Further calculation" is quite unclear and sorting a collection is again a different thing.
– HelgeB
yesterday
|
show 5 more comments
Try this code & let me know:
public function setCollection($collection)
{
$this->_collection = $collection;
$mpJoinConditions = 'marketplace_product.mageproduct_id = e.entity_id';
$this->_collection->getSelect()->join(
$this->_collection->getTable('marketplace_product'),
$mpJoinConditions,
)->columns("marketplace_product.seller_id")->where ("marketplace_product.store_id = '0'");
//add custom column
$this->_collection->getSelect()->columns(['latitude' => 1]);
echo "<pre>" ; print_r($this->_collection->getData()) ; exit;
}
@wakar-ahmad Is this working ?
– Hassan Ali Shahzad
yesterday
thanks for the reply,it's not working,getting some error.sorry my mistake 'latitude' value can be vary based on some calculation.
– wakar Ahamad
yesterday
ok is it possible for you to return array instead of object from function ? like: return $this->_collection->getData();
– Hassan Ali Shahzad
yesterday
Actually i've to do sorting using "latitude" so i don't think so.
– wakar Ahamad
yesterday
how you will add value to your latitude?
– Hassan Ali Shahzad
yesterday
|
show 5 more comments
Try this code & let me know:
public function setCollection($collection)
{
$this->_collection = $collection;
$mpJoinConditions = 'marketplace_product.mageproduct_id = e.entity_id';
$this->_collection->getSelect()->join(
$this->_collection->getTable('marketplace_product'),
$mpJoinConditions,
)->columns("marketplace_product.seller_id")->where ("marketplace_product.store_id = '0'");
//add custom column
$this->_collection->getSelect()->columns(['latitude' => 1]);
echo "<pre>" ; print_r($this->_collection->getData()) ; exit;
}
@wakar-ahmad Is this working ?
– Hassan Ali Shahzad
yesterday
thanks for the reply,it's not working,getting some error.sorry my mistake 'latitude' value can be vary based on some calculation.
– wakar Ahamad
yesterday
ok is it possible for you to return array instead of object from function ? like: return $this->_collection->getData();
– Hassan Ali Shahzad
yesterday
Actually i've to do sorting using "latitude" so i don't think so.
– wakar Ahamad
yesterday
how you will add value to your latitude?
– Hassan Ali Shahzad
yesterday
|
show 5 more comments
Try this code & let me know:
public function setCollection($collection)
{
$this->_collection = $collection;
$mpJoinConditions = 'marketplace_product.mageproduct_id = e.entity_id';
$this->_collection->getSelect()->join(
$this->_collection->getTable('marketplace_product'),
$mpJoinConditions,
)->columns("marketplace_product.seller_id")->where ("marketplace_product.store_id = '0'");
//add custom column
$this->_collection->getSelect()->columns(['latitude' => 1]);
echo "<pre>" ; print_r($this->_collection->getData()) ; exit;
}
Try this code & let me know:
public function setCollection($collection)
{
$this->_collection = $collection;
$mpJoinConditions = 'marketplace_product.mageproduct_id = e.entity_id';
$this->_collection->getSelect()->join(
$this->_collection->getTable('marketplace_product'),
$mpJoinConditions,
)->columns("marketplace_product.seller_id")->where ("marketplace_product.store_id = '0'");
//add custom column
$this->_collection->getSelect()->columns(['latitude' => 1]);
echo "<pre>" ; print_r($this->_collection->getData()) ; exit;
}
answered yesterday
Hassan Ali ShahzadHassan Ali Shahzad
734317
734317
@wakar-ahmad Is this working ?
– Hassan Ali Shahzad
yesterday
thanks for the reply,it's not working,getting some error.sorry my mistake 'latitude' value can be vary based on some calculation.
– wakar Ahamad
yesterday
ok is it possible for you to return array instead of object from function ? like: return $this->_collection->getData();
– Hassan Ali Shahzad
yesterday
Actually i've to do sorting using "latitude" so i don't think so.
– wakar Ahamad
yesterday
how you will add value to your latitude?
– Hassan Ali Shahzad
yesterday
|
show 5 more comments
@wakar-ahmad Is this working ?
– Hassan Ali Shahzad
yesterday
thanks for the reply,it's not working,getting some error.sorry my mistake 'latitude' value can be vary based on some calculation.
– wakar Ahamad
yesterday
ok is it possible for you to return array instead of object from function ? like: return $this->_collection->getData();
– Hassan Ali Shahzad
yesterday
Actually i've to do sorting using "latitude" so i don't think so.
– wakar Ahamad
yesterday
how you will add value to your latitude?
– Hassan Ali Shahzad
yesterday
@wakar-ahmad Is this working ?
– Hassan Ali Shahzad
yesterday
@wakar-ahmad Is this working ?
– Hassan Ali Shahzad
yesterday
thanks for the reply,it's not working,getting some error.sorry my mistake 'latitude' value can be vary based on some calculation.
– wakar Ahamad
yesterday
thanks for the reply,it's not working,getting some error.sorry my mistake 'latitude' value can be vary based on some calculation.
– wakar Ahamad
yesterday
ok is it possible for you to return array instead of object from function ? like: return $this->_collection->getData();
– Hassan Ali Shahzad
yesterday
ok is it possible for you to return array instead of object from function ? like: return $this->_collection->getData();
– Hassan Ali Shahzad
yesterday
Actually i've to do sorting using "latitude" so i don't think so.
– wakar Ahamad
yesterday
Actually i've to do sorting using "latitude" so i don't think so.
– wakar Ahamad
yesterday
how you will add value to your latitude?
– Hassan Ali Shahzad
yesterday
how you will add value to your latitude?
– Hassan Ali Shahzad
yesterday
|
show 5 more comments
Thanks for contributing an answer to Magento Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f269535%2fhow-to-pass-added-custom-key-value-into-getcollection%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
would you like to return product from above function array (like displaying) or object?
– Hassan Ali Shahzad
yesterday
thanks for the reply! I want to add in a way so that after doing print_r() "latitude" field added for each product. we've to add this filed for entire product collection.
– wakar Ahamad
yesterday