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.

enter image description here

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.

  • would you like to return product from above function array (like displaying) or object? – Hassan Ali Shahzad Apr 10 at 12:22
  • 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 Apr 10 at 12:24
0

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 Apr 10 at 12:29
  • The question was not about persisting data or did I miss something? – HelgeB Apr 10 at 12:29
  • 1
    you are right. I just wanted to mention this – Philipp Sander Apr 10 at 12:31
  • @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 Apr 10 at 12:31
  • 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 Apr 10 at 12:36
0

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 Apr 10 at 12:48
  • 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 Apr 10 at 12:49
  • ok is it possible for you to return array instead of object from function ? like: return $this->_collection->getData(); – Hassan Ali Shahzad Apr 10 at 12:52
  • Actually i've to do sorting using "latitude" so i don't think so. – wakar Ahamad Apr 10 at 12:53
  • how you will add value to your latitude? – Hassan Ali Shahzad Apr 10 at 12:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.