0

I want to update product price from the collection and use that collection again. I have written a code for that as per below:

$price = 23;
foreach ($this->_collection as $key => $value) {
    $value->setPrice($price);
    $sin = $value;
    $this->_collection->removeItemByKey($value->getId());   //I have tried to remove a object
    $this->_collection->addItem($sin);  //set a value again with updated price in a object
}    
print_r($this->_collection);exit;    
if ($this->getCurrentOrder()) {
    $this->_collection->setOrder($this->getCurrentOrder(),
    $this->getCurrentDirection());
}

But In Results when I print collection then the price is not updating the collection.

  • You can assign collection to other variable before iterate collection and make changes in it. – Chirag Rajput Apr 17 '18 at 10:03
  • @ChiragRajput can you please give me an example for that.? – Emipro Technologies Pvt. Ltd. Apr 17 '18 at 10:10
  • $price = 23; $ccollection = $this->_collection; foreach ($this->_collection as $key => $value) { $value->setPrice($price); $sin = $value; $this->_collection->removeItemByKey($value->getId()); //I have tried to remove a object $this->_collection->addItem($sin); //set a value again with updated price in a object } print_r($this->_collection);exit; if ($this->getCurrentOrder()) { $this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection()); } IN the above way you can save collection in other variable – Chirag Rajput Apr 17 '18 at 10:15
  • @ChiragRajput In your given code, you have assign collection to other variable but you did not use that anywhere. Then whats the purpose to assign a collection to other variables? – Emipro Technologies Pvt. Ltd. Apr 17 '18 at 10:22

Your Answer

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

Browse other questions tagged or ask your own question.