Admin order update price programmatically in quote loop
I have a bit of custom pricing that needs to be carried out for each product in an order which alters the end price of the item.
I am using an observer with the sales_quote_item_qty_set_after
event to ensure that the price is run each time the product is re-configured, as I haven't been able to get desired results from other events.
The issue i'm running into is the inability to change the price of the product in the quote when looping through the quote using ->getAllItems()
.
I know that the price can be overridden via the following for the product add events.
$_item = $obs->getQuoteItem();
$_item = ( $_item->getParentItem() ? $_item->getParentItem() : $_item );
$new_price = 1000;
$_item->setCustomPrice($new_price);
$_item->setOriginalCustomPrice($new_price);
$_item->getProduct()->setIsSuperMode(true);
However, I can not find a way to do it when running through the quote items within the cart. Is this possible, or would the pricing have to be done in a different observer, and if so, which?
Full code is below...
public function adminAddProduct(Varien_Event_Observer $obs) {
$quote = $this->_getSession()->getQuote();
foreach($quote->getAllItems() as $_item){
$options = $_item->getProduct()->getTypeInstance(true)->getOrderOptions($_item->getProduct());
$_product = $_item->getProduct();
$sku = $options['simple_sku'];
if ($sku) {
$tierPrices = $_product->getTierPrice();
$price = $_item->getPrice();
$qty = $options['info_buyRequest']['qty'];
$product = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku);
$runspeed = $product->getResource()->getAttribute('runspeed')->getFrontend()->getValue($product);
$prodOpts = ;
foreach($options['options'] as $option) {
$prodOpts[$option['label']] = $option['value'];
}
$index = 0;
foreach($tierPrices as $tierPrice) {
if($tierPrice['price_qty'] <= $qty && $tierPrices[$index + 1]['price_qty'] > $qty) {
$price = $tierPrice['price'];
}
//$prodTiers[$tierPrice['price_qty']] = $tierPrice['price'];
}
if($prodOpts['Quantity'] == 'Fixed Quantity' && array_key_exists('Quantity Select', $prodOpts)){
$qty = str_replace(',', '', explode(" ", $prodOpts['Quantity Select'])[0]);
} else {
$qty = 1;
}
$_item->setQty($qty);
$_item->setCustomPrice($customPrice)->setOriginalCustomPrice($customPrice);
$new_price = 999;
$_item->setPrice($new_price);
$_item->setCustomPrice($new_price);
$_item->setOriginalCustomPrice($new_price);
$_item->save();
Mage::log($price);
Mage::log($prodOpts);
}
}
}
Any help is appreciated.
admin orders price event-observer quote
bumped to the homepage by Community♦ 15 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
I have a bit of custom pricing that needs to be carried out for each product in an order which alters the end price of the item.
I am using an observer with the sales_quote_item_qty_set_after
event to ensure that the price is run each time the product is re-configured, as I haven't been able to get desired results from other events.
The issue i'm running into is the inability to change the price of the product in the quote when looping through the quote using ->getAllItems()
.
I know that the price can be overridden via the following for the product add events.
$_item = $obs->getQuoteItem();
$_item = ( $_item->getParentItem() ? $_item->getParentItem() : $_item );
$new_price = 1000;
$_item->setCustomPrice($new_price);
$_item->setOriginalCustomPrice($new_price);
$_item->getProduct()->setIsSuperMode(true);
However, I can not find a way to do it when running through the quote items within the cart. Is this possible, or would the pricing have to be done in a different observer, and if so, which?
Full code is below...
public function adminAddProduct(Varien_Event_Observer $obs) {
$quote = $this->_getSession()->getQuote();
foreach($quote->getAllItems() as $_item){
$options = $_item->getProduct()->getTypeInstance(true)->getOrderOptions($_item->getProduct());
$_product = $_item->getProduct();
$sku = $options['simple_sku'];
if ($sku) {
$tierPrices = $_product->getTierPrice();
$price = $_item->getPrice();
$qty = $options['info_buyRequest']['qty'];
$product = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku);
$runspeed = $product->getResource()->getAttribute('runspeed')->getFrontend()->getValue($product);
$prodOpts = ;
foreach($options['options'] as $option) {
$prodOpts[$option['label']] = $option['value'];
}
$index = 0;
foreach($tierPrices as $tierPrice) {
if($tierPrice['price_qty'] <= $qty && $tierPrices[$index + 1]['price_qty'] > $qty) {
$price = $tierPrice['price'];
}
//$prodTiers[$tierPrice['price_qty']] = $tierPrice['price'];
}
if($prodOpts['Quantity'] == 'Fixed Quantity' && array_key_exists('Quantity Select', $prodOpts)){
$qty = str_replace(',', '', explode(" ", $prodOpts['Quantity Select'])[0]);
} else {
$qty = 1;
}
$_item->setQty($qty);
$_item->setCustomPrice($customPrice)->setOriginalCustomPrice($customPrice);
$new_price = 999;
$_item->setPrice($new_price);
$_item->setCustomPrice($new_price);
$_item->setOriginalCustomPrice($new_price);
$_item->save();
Mage::log($price);
Mage::log($prodOpts);
}
}
}
Any help is appreciated.
admin orders price event-observer quote
bumped to the homepage by Community♦ 15 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
I have a bit of custom pricing that needs to be carried out for each product in an order which alters the end price of the item.
I am using an observer with the sales_quote_item_qty_set_after
event to ensure that the price is run each time the product is re-configured, as I haven't been able to get desired results from other events.
The issue i'm running into is the inability to change the price of the product in the quote when looping through the quote using ->getAllItems()
.
I know that the price can be overridden via the following for the product add events.
$_item = $obs->getQuoteItem();
$_item = ( $_item->getParentItem() ? $_item->getParentItem() : $_item );
$new_price = 1000;
$_item->setCustomPrice($new_price);
$_item->setOriginalCustomPrice($new_price);
$_item->getProduct()->setIsSuperMode(true);
However, I can not find a way to do it when running through the quote items within the cart. Is this possible, or would the pricing have to be done in a different observer, and if so, which?
Full code is below...
public function adminAddProduct(Varien_Event_Observer $obs) {
$quote = $this->_getSession()->getQuote();
foreach($quote->getAllItems() as $_item){
$options = $_item->getProduct()->getTypeInstance(true)->getOrderOptions($_item->getProduct());
$_product = $_item->getProduct();
$sku = $options['simple_sku'];
if ($sku) {
$tierPrices = $_product->getTierPrice();
$price = $_item->getPrice();
$qty = $options['info_buyRequest']['qty'];
$product = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku);
$runspeed = $product->getResource()->getAttribute('runspeed')->getFrontend()->getValue($product);
$prodOpts = ;
foreach($options['options'] as $option) {
$prodOpts[$option['label']] = $option['value'];
}
$index = 0;
foreach($tierPrices as $tierPrice) {
if($tierPrice['price_qty'] <= $qty && $tierPrices[$index + 1]['price_qty'] > $qty) {
$price = $tierPrice['price'];
}
//$prodTiers[$tierPrice['price_qty']] = $tierPrice['price'];
}
if($prodOpts['Quantity'] == 'Fixed Quantity' && array_key_exists('Quantity Select', $prodOpts)){
$qty = str_replace(',', '', explode(" ", $prodOpts['Quantity Select'])[0]);
} else {
$qty = 1;
}
$_item->setQty($qty);
$_item->setCustomPrice($customPrice)->setOriginalCustomPrice($customPrice);
$new_price = 999;
$_item->setPrice($new_price);
$_item->setCustomPrice($new_price);
$_item->setOriginalCustomPrice($new_price);
$_item->save();
Mage::log($price);
Mage::log($prodOpts);
}
}
}
Any help is appreciated.
admin orders price event-observer quote
I have a bit of custom pricing that needs to be carried out for each product in an order which alters the end price of the item.
I am using an observer with the sales_quote_item_qty_set_after
event to ensure that the price is run each time the product is re-configured, as I haven't been able to get desired results from other events.
The issue i'm running into is the inability to change the price of the product in the quote when looping through the quote using ->getAllItems()
.
I know that the price can be overridden via the following for the product add events.
$_item = $obs->getQuoteItem();
$_item = ( $_item->getParentItem() ? $_item->getParentItem() : $_item );
$new_price = 1000;
$_item->setCustomPrice($new_price);
$_item->setOriginalCustomPrice($new_price);
$_item->getProduct()->setIsSuperMode(true);
However, I can not find a way to do it when running through the quote items within the cart. Is this possible, or would the pricing have to be done in a different observer, and if so, which?
Full code is below...
public function adminAddProduct(Varien_Event_Observer $obs) {
$quote = $this->_getSession()->getQuote();
foreach($quote->getAllItems() as $_item){
$options = $_item->getProduct()->getTypeInstance(true)->getOrderOptions($_item->getProduct());
$_product = $_item->getProduct();
$sku = $options['simple_sku'];
if ($sku) {
$tierPrices = $_product->getTierPrice();
$price = $_item->getPrice();
$qty = $options['info_buyRequest']['qty'];
$product = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku);
$runspeed = $product->getResource()->getAttribute('runspeed')->getFrontend()->getValue($product);
$prodOpts = ;
foreach($options['options'] as $option) {
$prodOpts[$option['label']] = $option['value'];
}
$index = 0;
foreach($tierPrices as $tierPrice) {
if($tierPrice['price_qty'] <= $qty && $tierPrices[$index + 1]['price_qty'] > $qty) {
$price = $tierPrice['price'];
}
//$prodTiers[$tierPrice['price_qty']] = $tierPrice['price'];
}
if($prodOpts['Quantity'] == 'Fixed Quantity' && array_key_exists('Quantity Select', $prodOpts)){
$qty = str_replace(',', '', explode(" ", $prodOpts['Quantity Select'])[0]);
} else {
$qty = 1;
}
$_item->setQty($qty);
$_item->setCustomPrice($customPrice)->setOriginalCustomPrice($customPrice);
$new_price = 999;
$_item->setPrice($new_price);
$_item->setCustomPrice($new_price);
$_item->setOriginalCustomPrice($new_price);
$_item->save();
Mage::log($price);
Mage::log($prodOpts);
}
}
}
Any help is appreciated.
admin orders price event-observer quote
admin orders price event-observer quote
edited May 18 '16 at 15:01
Sidriel
asked May 12 '16 at 19:31
SidrielSidriel
1011
1011
bumped to the homepage by Community♦ 15 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community♦ 15 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I have found the answer to be that $quote->save()
needed to be appended after the $_item->save()
method.
add a comment |
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%2f115189%2fadmin-order-update-price-programmatically-in-quote-loop%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I have found the answer to be that $quote->save()
needed to be appended after the $_item->save()
method.
add a comment |
I have found the answer to be that $quote->save()
needed to be appended after the $_item->save()
method.
add a comment |
I have found the answer to be that $quote->save()
needed to be appended after the $_item->save()
method.
I have found the answer to be that $quote->save()
needed to be appended after the $_item->save()
method.
answered May 19 '16 at 20:11
SidrielSidriel
1011
1011
add a comment |
add a comment |
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%2f115189%2fadmin-order-update-price-programmatically-in-quote-loop%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