Making External API Request in Product Modelshow product reviews and form in category viewhow to add product to cart in magento api V2Create categories through installerMulti update products quantities using one soap api requestDynamically calculated prices save after add to cartMagento 2 API get product priceauto save in stock if qty greater than 0Please specify the product's option(s). Error - Magento 1.9.2Magento 2: Add a product to the cart programmaticallyI have created one field using product form field for my price i want save my field value at product creation time from backend magento2

Optimising Table wrapping over a Select

I have a ruthless DM and I'm considering leaving the party. What are my options to minimize the negative impact to the rest of the group?

Referring to different instances of the same character in time travel

Are randomly-generated passwords starting with "a" less secure?

What is temperature on a quantum level

Cubic programming and beyond?

How do Windows version numbers work?

Using Images for Points in ListPlot

Is purchasing foreign currency before going abroad a losing proposition?

diff shows a file that does not exist

Using ”as” after dialogue tags

Geometric interpretation of complex inner products

Grammy Winners Grading

Is Arc Length always irrational between two rational points?

How might the United Kingdom become a republic?

If a specific mass of air is polluted, will the pollution stick with it?

Trying to find a flaw in my proof that there are more rearrangements of an infinite series than real numbers

Are there any intersection of Theory A and Theory B?

<schwitz>, <zwinker> etc. Does German always use 2nd Person Singular Imperative verbs for emoticons? If so, why?

What's the minimum number of sensors for a hobby GPS waypoint-following UAV?

Can I call 112 to check a police officer's identity in the Czech Republic?

What could be some effects of (physical) Mana consumption that prevent long term abuse?

Wrapper in return method for test class

Can I play a first turn Simic Growth Chamber to have 3 mana available in the second turn?



Making External API Request in Product Model


show product reviews and form in category viewhow to add product to cart in magento api V2Create categories through installerMulti update products quantities using one soap api requestDynamically calculated prices save after add to cartMagento 2 API get product priceauto save in stock if qty greater than 0Please specify the product's option(s). Error - Magento 1.9.2Magento 2: Add a product to the cart programmaticallyI have created one field using product form field for my price i want save my field value at product creation time from backend magento2






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















In my Magento project I have to receive pricing and inventory information on our products. Right now I have created my own method within the product model to make a SOAP request in the getPrice() function.



Here's what that looks like:



../app/code/core/Mage/Catalog/Model/Product.php



/**
* Get product price throught type instance
*
* @return unknown
*/
public function getPrice()

if ($this->getData('is_api'))
$this->getAPIData();

if ($this->_calculatePrice

/**
* Get time since last update
*
* @return int
*/
public function getTimeSinceLastUpdate()
return time() - strtotime($this->getUpdatedAt());


/**
* Get product data from API
*
* @return price
*/
public function getAPIData()
if ($this->getTimeSinceLastUpdate() > 5)
$parts = new SoapClient("XXXX");
$res = $parts->PartLookup([
'userName' => 'XXXX',
'password' => 'XXXX',
'partnumber' => 'XXXX',
'make' => ''
]);
$this->updateAPIProduct($res);
return $res;



/**
* Update API Product
*
* @return unknown
*/
public function updateAPIProduct($data)
$this->setPrice($data->PartLookupResult->PartInformation_v2[0]->Price);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$this->save();
Mage::app()->setCurrentStore(1);



However, this seems like the wrong way to do this. To me, it would make more sense to test whether or not it's an api product during product object creation. At that point I would update pricing AND inventory information.



I've also run into a bug where when I update the pricing while on the frontend the new price is grey and has a strike through it and the old price next to it.



The only problem is that I have no idea where to go to update that information at that point.










share|improve this question






















  • This is no answer, so I'll add it in a comment: this seems like a very flawed design to me. There will not be any operation (load backend, load frontend, order, anything) that is possible, when the SOAP-server is not reachable, or the access data has been changed, or there's another problem. You are completely relying on something that is not in Magento. You should update the product data with a cronjob or sth, save it in Magento, and then use the "normal" Magento way to show the data to the user.

    – simonthesorcerer
    Oct 19 '16 at 19:52











  • @simonthesorcerer thanks for the comment! I can easily build in something that will pass by the request if the server is unreachable. A cronjob is not realistic. The data needs to be requested in real time. Mainly pricing and inventory. Requesting data on 600k+ SKUs everyday will take lots of resources and time. The API is specifically designed to be used on an as needed basis.

    – Ryan Cady
    Oct 19 '16 at 20:12











  • can you not implement a push system by the external source to import data into magneto every time it has been changed?

    – simonthesorcerer
    Oct 19 '16 at 20:14











  • Their API is very limited. The only thing we get that changes is we get a bulk dump everyday that includes a SKU, and description. We would only see new products added or old ones removed. No pricing or inventory information. All of that info must be requested via the SOAP api. It's very very limited unfortunately.

    – Ryan Cady
    Oct 19 '16 at 20:17

















0















In my Magento project I have to receive pricing and inventory information on our products. Right now I have created my own method within the product model to make a SOAP request in the getPrice() function.



Here's what that looks like:



../app/code/core/Mage/Catalog/Model/Product.php



/**
* Get product price throught type instance
*
* @return unknown
*/
public function getPrice()

if ($this->getData('is_api'))
$this->getAPIData();

if ($this->_calculatePrice

/**
* Get time since last update
*
* @return int
*/
public function getTimeSinceLastUpdate()
return time() - strtotime($this->getUpdatedAt());


/**
* Get product data from API
*
* @return price
*/
public function getAPIData()
if ($this->getTimeSinceLastUpdate() > 5)
$parts = new SoapClient("XXXX");
$res = $parts->PartLookup([
'userName' => 'XXXX',
'password' => 'XXXX',
'partnumber' => 'XXXX',
'make' => ''
]);
$this->updateAPIProduct($res);
return $res;



/**
* Update API Product
*
* @return unknown
*/
public function updateAPIProduct($data)
$this->setPrice($data->PartLookupResult->PartInformation_v2[0]->Price);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$this->save();
Mage::app()->setCurrentStore(1);



However, this seems like the wrong way to do this. To me, it would make more sense to test whether or not it's an api product during product object creation. At that point I would update pricing AND inventory information.



I've also run into a bug where when I update the pricing while on the frontend the new price is grey and has a strike through it and the old price next to it.



The only problem is that I have no idea where to go to update that information at that point.










share|improve this question






















  • This is no answer, so I'll add it in a comment: this seems like a very flawed design to me. There will not be any operation (load backend, load frontend, order, anything) that is possible, when the SOAP-server is not reachable, or the access data has been changed, or there's another problem. You are completely relying on something that is not in Magento. You should update the product data with a cronjob or sth, save it in Magento, and then use the "normal" Magento way to show the data to the user.

    – simonthesorcerer
    Oct 19 '16 at 19:52











  • @simonthesorcerer thanks for the comment! I can easily build in something that will pass by the request if the server is unreachable. A cronjob is not realistic. The data needs to be requested in real time. Mainly pricing and inventory. Requesting data on 600k+ SKUs everyday will take lots of resources and time. The API is specifically designed to be used on an as needed basis.

    – Ryan Cady
    Oct 19 '16 at 20:12











  • can you not implement a push system by the external source to import data into magneto every time it has been changed?

    – simonthesorcerer
    Oct 19 '16 at 20:14











  • Their API is very limited. The only thing we get that changes is we get a bulk dump everyday that includes a SKU, and description. We would only see new products added or old ones removed. No pricing or inventory information. All of that info must be requested via the SOAP api. It's very very limited unfortunately.

    – Ryan Cady
    Oct 19 '16 at 20:17













0












0








0








In my Magento project I have to receive pricing and inventory information on our products. Right now I have created my own method within the product model to make a SOAP request in the getPrice() function.



Here's what that looks like:



../app/code/core/Mage/Catalog/Model/Product.php



/**
* Get product price throught type instance
*
* @return unknown
*/
public function getPrice()

if ($this->getData('is_api'))
$this->getAPIData();

if ($this->_calculatePrice

/**
* Get time since last update
*
* @return int
*/
public function getTimeSinceLastUpdate()
return time() - strtotime($this->getUpdatedAt());


/**
* Get product data from API
*
* @return price
*/
public function getAPIData()
if ($this->getTimeSinceLastUpdate() > 5)
$parts = new SoapClient("XXXX");
$res = $parts->PartLookup([
'userName' => 'XXXX',
'password' => 'XXXX',
'partnumber' => 'XXXX',
'make' => ''
]);
$this->updateAPIProduct($res);
return $res;



/**
* Update API Product
*
* @return unknown
*/
public function updateAPIProduct($data)
$this->setPrice($data->PartLookupResult->PartInformation_v2[0]->Price);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$this->save();
Mage::app()->setCurrentStore(1);



However, this seems like the wrong way to do this. To me, it would make more sense to test whether or not it's an api product during product object creation. At that point I would update pricing AND inventory information.



I've also run into a bug where when I update the pricing while on the frontend the new price is grey and has a strike through it and the old price next to it.



The only problem is that I have no idea where to go to update that information at that point.










share|improve this question














In my Magento project I have to receive pricing and inventory information on our products. Right now I have created my own method within the product model to make a SOAP request in the getPrice() function.



Here's what that looks like:



../app/code/core/Mage/Catalog/Model/Product.php



/**
* Get product price throught type instance
*
* @return unknown
*/
public function getPrice()

if ($this->getData('is_api'))
$this->getAPIData();

if ($this->_calculatePrice

/**
* Get time since last update
*
* @return int
*/
public function getTimeSinceLastUpdate()
return time() - strtotime($this->getUpdatedAt());


/**
* Get product data from API
*
* @return price
*/
public function getAPIData()
if ($this->getTimeSinceLastUpdate() > 5)
$parts = new SoapClient("XXXX");
$res = $parts->PartLookup([
'userName' => 'XXXX',
'password' => 'XXXX',
'partnumber' => 'XXXX',
'make' => ''
]);
$this->updateAPIProduct($res);
return $res;



/**
* Update API Product
*
* @return unknown
*/
public function updateAPIProduct($data)
$this->setPrice($data->PartLookupResult->PartInformation_v2[0]->Price);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$this->save();
Mage::app()->setCurrentStore(1);



However, this seems like the wrong way to do this. To me, it would make more sense to test whether or not it's an api product during product object creation. At that point I would update pricing AND inventory information.



I've also run into a bug where when I update the pricing while on the frontend the new price is grey and has a strike through it and the old price next to it.



The only problem is that I have no idea where to go to update that information at that point.







product php database api soap






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Oct 19 '16 at 16:20









Ryan CadyRyan Cady

2311 silver badge10 bronze badges




2311 silver badge10 bronze badges












  • This is no answer, so I'll add it in a comment: this seems like a very flawed design to me. There will not be any operation (load backend, load frontend, order, anything) that is possible, when the SOAP-server is not reachable, or the access data has been changed, or there's another problem. You are completely relying on something that is not in Magento. You should update the product data with a cronjob or sth, save it in Magento, and then use the "normal" Magento way to show the data to the user.

    – simonthesorcerer
    Oct 19 '16 at 19:52











  • @simonthesorcerer thanks for the comment! I can easily build in something that will pass by the request if the server is unreachable. A cronjob is not realistic. The data needs to be requested in real time. Mainly pricing and inventory. Requesting data on 600k+ SKUs everyday will take lots of resources and time. The API is specifically designed to be used on an as needed basis.

    – Ryan Cady
    Oct 19 '16 at 20:12











  • can you not implement a push system by the external source to import data into magneto every time it has been changed?

    – simonthesorcerer
    Oct 19 '16 at 20:14











  • Their API is very limited. The only thing we get that changes is we get a bulk dump everyday that includes a SKU, and description. We would only see new products added or old ones removed. No pricing or inventory information. All of that info must be requested via the SOAP api. It's very very limited unfortunately.

    – Ryan Cady
    Oct 19 '16 at 20:17

















  • This is no answer, so I'll add it in a comment: this seems like a very flawed design to me. There will not be any operation (load backend, load frontend, order, anything) that is possible, when the SOAP-server is not reachable, or the access data has been changed, or there's another problem. You are completely relying on something that is not in Magento. You should update the product data with a cronjob or sth, save it in Magento, and then use the "normal" Magento way to show the data to the user.

    – simonthesorcerer
    Oct 19 '16 at 19:52











  • @simonthesorcerer thanks for the comment! I can easily build in something that will pass by the request if the server is unreachable. A cronjob is not realistic. The data needs to be requested in real time. Mainly pricing and inventory. Requesting data on 600k+ SKUs everyday will take lots of resources and time. The API is specifically designed to be used on an as needed basis.

    – Ryan Cady
    Oct 19 '16 at 20:12











  • can you not implement a push system by the external source to import data into magneto every time it has been changed?

    – simonthesorcerer
    Oct 19 '16 at 20:14











  • Their API is very limited. The only thing we get that changes is we get a bulk dump everyday that includes a SKU, and description. We would only see new products added or old ones removed. No pricing or inventory information. All of that info must be requested via the SOAP api. It's very very limited unfortunately.

    – Ryan Cady
    Oct 19 '16 at 20:17
















This is no answer, so I'll add it in a comment: this seems like a very flawed design to me. There will not be any operation (load backend, load frontend, order, anything) that is possible, when the SOAP-server is not reachable, or the access data has been changed, or there's another problem. You are completely relying on something that is not in Magento. You should update the product data with a cronjob or sth, save it in Magento, and then use the "normal" Magento way to show the data to the user.

– simonthesorcerer
Oct 19 '16 at 19:52





This is no answer, so I'll add it in a comment: this seems like a very flawed design to me. There will not be any operation (load backend, load frontend, order, anything) that is possible, when the SOAP-server is not reachable, or the access data has been changed, or there's another problem. You are completely relying on something that is not in Magento. You should update the product data with a cronjob or sth, save it in Magento, and then use the "normal" Magento way to show the data to the user.

– simonthesorcerer
Oct 19 '16 at 19:52













@simonthesorcerer thanks for the comment! I can easily build in something that will pass by the request if the server is unreachable. A cronjob is not realistic. The data needs to be requested in real time. Mainly pricing and inventory. Requesting data on 600k+ SKUs everyday will take lots of resources and time. The API is specifically designed to be used on an as needed basis.

– Ryan Cady
Oct 19 '16 at 20:12





@simonthesorcerer thanks for the comment! I can easily build in something that will pass by the request if the server is unreachable. A cronjob is not realistic. The data needs to be requested in real time. Mainly pricing and inventory. Requesting data on 600k+ SKUs everyday will take lots of resources and time. The API is specifically designed to be used on an as needed basis.

– Ryan Cady
Oct 19 '16 at 20:12













can you not implement a push system by the external source to import data into magneto every time it has been changed?

– simonthesorcerer
Oct 19 '16 at 20:14





can you not implement a push system by the external source to import data into magneto every time it has been changed?

– simonthesorcerer
Oct 19 '16 at 20:14













Their API is very limited. The only thing we get that changes is we get a bulk dump everyday that includes a SKU, and description. We would only see new products added or old ones removed. No pricing or inventory information. All of that info must be requested via the SOAP api. It's very very limited unfortunately.

– Ryan Cady
Oct 19 '16 at 20:17





Their API is very limited. The only thing we get that changes is we get a bulk dump everyday that includes a SKU, and description. We would only see new products added or old ones removed. No pricing or inventory information. All of that info must be requested via the SOAP api. It's very very limited unfortunately.

– Ryan Cady
Oct 19 '16 at 20:17










1 Answer
1






active

oldest

votes


















0














Not a real answer, but too long for a comment:



I think to set the information when the object is created is the correct approach. With your solution now, one product could technically have a different price in different sections of the site, because the price could have been updated in the "source" in the meantime.



I would also try to do it in an observer and not in a class rewrite, as the latter can lead to problems with other extensions (only one rewrite of a certain class can be active at a time in Magento).



So, I would suggest to try something like this (not sure about the exact event name):



<!-- config.xml -->
<global>
<events>
<catalog_product_load_after>
<observers>
<my_module_observer_name>
<class>mymodel/observer</class>
<method>setRealValues</method>
</my_module_observer_name>
</observers>
</catalog_product_load_after>
</events>
</global>


...and the PHP file:



<?php // Observer.php

class Namespace_Mymodel_Model_Observer
public function setRealValues($observer)
$product = $observer->getEvent()->getProduct();
$attributes = ['price', 'name', 'something']
/*
* Either get all values you need with one request, or
* do a foreach() loop over $attributes
*/




You would also have to do this for cataloginventory_stockitem_load_after, because technically, this holds the stock data.



There is another problem with product lists: Magento uses a collection object for this. I think if the API only allows single request - have fun requesting the data for 20+ products in a time that is acceptable for the customer :) cURL can do some kind of asynchronous stuff that might help you here. The event to listen for would be catalog_product_resource_collection _load_after, I guess (not sure again).



Maybe that this does not get all the possibilities where the price is displayed, but I think it's a place to start.



Update after comment:



I think saving the products at night would be a good start; keep in mind that the most expensive action will be loading the collection (600k SKUs means you should probably do it in chunks) and saving the product with a save() method. The latter can be avoided if you first check if the data has actually changed, and if you use $product->getResource()->saveAttribute($product, 'price', 10.00) (not sure about exact method name) instead of saving the whole model.



You might also want to make separate functions for updating the product and the stock item, as the stock item does not operate with such big objects and can maybe run more often.






share|improve this answer

























  • Thank you so much for this! After all you guys have mentioned I've began to start thinking about this more in depth. I have been thinking that maybe it would be more wise to update pricing once a day through a product import. Then check inventory when added to cart if the inventory amount was under a specified amount like 10 units left or something similar. What do you think?

    – Ryan Cady
    Oct 20 '16 at 17:52











  • I have updated my answer

    – simonthesorcerer
    Oct 20 '16 at 21:04











  • Awesome! I think this is most likely the best way to do it. I found out that we may be able to have our dump file contain all the pricing and inventory information as well in a csv. If that's true, I'll just create a cronjob that will compare the two and detect changes then push that to the server and update as needed.

    – Ryan Cady
    Oct 21 '16 at 13:49













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
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f141677%2fmaking-external-api-request-in-product-model%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









0














Not a real answer, but too long for a comment:



I think to set the information when the object is created is the correct approach. With your solution now, one product could technically have a different price in different sections of the site, because the price could have been updated in the "source" in the meantime.



I would also try to do it in an observer and not in a class rewrite, as the latter can lead to problems with other extensions (only one rewrite of a certain class can be active at a time in Magento).



So, I would suggest to try something like this (not sure about the exact event name):



<!-- config.xml -->
<global>
<events>
<catalog_product_load_after>
<observers>
<my_module_observer_name>
<class>mymodel/observer</class>
<method>setRealValues</method>
</my_module_observer_name>
</observers>
</catalog_product_load_after>
</events>
</global>


...and the PHP file:



<?php // Observer.php

class Namespace_Mymodel_Model_Observer
public function setRealValues($observer)
$product = $observer->getEvent()->getProduct();
$attributes = ['price', 'name', 'something']
/*
* Either get all values you need with one request, or
* do a foreach() loop over $attributes
*/




You would also have to do this for cataloginventory_stockitem_load_after, because technically, this holds the stock data.



There is another problem with product lists: Magento uses a collection object for this. I think if the API only allows single request - have fun requesting the data for 20+ products in a time that is acceptable for the customer :) cURL can do some kind of asynchronous stuff that might help you here. The event to listen for would be catalog_product_resource_collection _load_after, I guess (not sure again).



Maybe that this does not get all the possibilities where the price is displayed, but I think it's a place to start.



Update after comment:



I think saving the products at night would be a good start; keep in mind that the most expensive action will be loading the collection (600k SKUs means you should probably do it in chunks) and saving the product with a save() method. The latter can be avoided if you first check if the data has actually changed, and if you use $product->getResource()->saveAttribute($product, 'price', 10.00) (not sure about exact method name) instead of saving the whole model.



You might also want to make separate functions for updating the product and the stock item, as the stock item does not operate with such big objects and can maybe run more often.






share|improve this answer

























  • Thank you so much for this! After all you guys have mentioned I've began to start thinking about this more in depth. I have been thinking that maybe it would be more wise to update pricing once a day through a product import. Then check inventory when added to cart if the inventory amount was under a specified amount like 10 units left or something similar. What do you think?

    – Ryan Cady
    Oct 20 '16 at 17:52











  • I have updated my answer

    – simonthesorcerer
    Oct 20 '16 at 21:04











  • Awesome! I think this is most likely the best way to do it. I found out that we may be able to have our dump file contain all the pricing and inventory information as well in a csv. If that's true, I'll just create a cronjob that will compare the two and detect changes then push that to the server and update as needed.

    – Ryan Cady
    Oct 21 '16 at 13:49















0














Not a real answer, but too long for a comment:



I think to set the information when the object is created is the correct approach. With your solution now, one product could technically have a different price in different sections of the site, because the price could have been updated in the "source" in the meantime.



I would also try to do it in an observer and not in a class rewrite, as the latter can lead to problems with other extensions (only one rewrite of a certain class can be active at a time in Magento).



So, I would suggest to try something like this (not sure about the exact event name):



<!-- config.xml -->
<global>
<events>
<catalog_product_load_after>
<observers>
<my_module_observer_name>
<class>mymodel/observer</class>
<method>setRealValues</method>
</my_module_observer_name>
</observers>
</catalog_product_load_after>
</events>
</global>


...and the PHP file:



<?php // Observer.php

class Namespace_Mymodel_Model_Observer
public function setRealValues($observer)
$product = $observer->getEvent()->getProduct();
$attributes = ['price', 'name', 'something']
/*
* Either get all values you need with one request, or
* do a foreach() loop over $attributes
*/




You would also have to do this for cataloginventory_stockitem_load_after, because technically, this holds the stock data.



There is another problem with product lists: Magento uses a collection object for this. I think if the API only allows single request - have fun requesting the data for 20+ products in a time that is acceptable for the customer :) cURL can do some kind of asynchronous stuff that might help you here. The event to listen for would be catalog_product_resource_collection _load_after, I guess (not sure again).



Maybe that this does not get all the possibilities where the price is displayed, but I think it's a place to start.



Update after comment:



I think saving the products at night would be a good start; keep in mind that the most expensive action will be loading the collection (600k SKUs means you should probably do it in chunks) and saving the product with a save() method. The latter can be avoided if you first check if the data has actually changed, and if you use $product->getResource()->saveAttribute($product, 'price', 10.00) (not sure about exact method name) instead of saving the whole model.



You might also want to make separate functions for updating the product and the stock item, as the stock item does not operate with such big objects and can maybe run more often.






share|improve this answer

























  • Thank you so much for this! After all you guys have mentioned I've began to start thinking about this more in depth. I have been thinking that maybe it would be more wise to update pricing once a day through a product import. Then check inventory when added to cart if the inventory amount was under a specified amount like 10 units left or something similar. What do you think?

    – Ryan Cady
    Oct 20 '16 at 17:52











  • I have updated my answer

    – simonthesorcerer
    Oct 20 '16 at 21:04











  • Awesome! I think this is most likely the best way to do it. I found out that we may be able to have our dump file contain all the pricing and inventory information as well in a csv. If that's true, I'll just create a cronjob that will compare the two and detect changes then push that to the server and update as needed.

    – Ryan Cady
    Oct 21 '16 at 13:49













0












0








0







Not a real answer, but too long for a comment:



I think to set the information when the object is created is the correct approach. With your solution now, one product could technically have a different price in different sections of the site, because the price could have been updated in the "source" in the meantime.



I would also try to do it in an observer and not in a class rewrite, as the latter can lead to problems with other extensions (only one rewrite of a certain class can be active at a time in Magento).



So, I would suggest to try something like this (not sure about the exact event name):



<!-- config.xml -->
<global>
<events>
<catalog_product_load_after>
<observers>
<my_module_observer_name>
<class>mymodel/observer</class>
<method>setRealValues</method>
</my_module_observer_name>
</observers>
</catalog_product_load_after>
</events>
</global>


...and the PHP file:



<?php // Observer.php

class Namespace_Mymodel_Model_Observer
public function setRealValues($observer)
$product = $observer->getEvent()->getProduct();
$attributes = ['price', 'name', 'something']
/*
* Either get all values you need with one request, or
* do a foreach() loop over $attributes
*/




You would also have to do this for cataloginventory_stockitem_load_after, because technically, this holds the stock data.



There is another problem with product lists: Magento uses a collection object for this. I think if the API only allows single request - have fun requesting the data for 20+ products in a time that is acceptable for the customer :) cURL can do some kind of asynchronous stuff that might help you here. The event to listen for would be catalog_product_resource_collection _load_after, I guess (not sure again).



Maybe that this does not get all the possibilities where the price is displayed, but I think it's a place to start.



Update after comment:



I think saving the products at night would be a good start; keep in mind that the most expensive action will be loading the collection (600k SKUs means you should probably do it in chunks) and saving the product with a save() method. The latter can be avoided if you first check if the data has actually changed, and if you use $product->getResource()->saveAttribute($product, 'price', 10.00) (not sure about exact method name) instead of saving the whole model.



You might also want to make separate functions for updating the product and the stock item, as the stock item does not operate with such big objects and can maybe run more often.






share|improve this answer















Not a real answer, but too long for a comment:



I think to set the information when the object is created is the correct approach. With your solution now, one product could technically have a different price in different sections of the site, because the price could have been updated in the "source" in the meantime.



I would also try to do it in an observer and not in a class rewrite, as the latter can lead to problems with other extensions (only one rewrite of a certain class can be active at a time in Magento).



So, I would suggest to try something like this (not sure about the exact event name):



<!-- config.xml -->
<global>
<events>
<catalog_product_load_after>
<observers>
<my_module_observer_name>
<class>mymodel/observer</class>
<method>setRealValues</method>
</my_module_observer_name>
</observers>
</catalog_product_load_after>
</events>
</global>


...and the PHP file:



<?php // Observer.php

class Namespace_Mymodel_Model_Observer
public function setRealValues($observer)
$product = $observer->getEvent()->getProduct();
$attributes = ['price', 'name', 'something']
/*
* Either get all values you need with one request, or
* do a foreach() loop over $attributes
*/




You would also have to do this for cataloginventory_stockitem_load_after, because technically, this holds the stock data.



There is another problem with product lists: Magento uses a collection object for this. I think if the API only allows single request - have fun requesting the data for 20+ products in a time that is acceptable for the customer :) cURL can do some kind of asynchronous stuff that might help you here. The event to listen for would be catalog_product_resource_collection _load_after, I guess (not sure again).



Maybe that this does not get all the possibilities where the price is displayed, but I think it's a place to start.



Update after comment:



I think saving the products at night would be a good start; keep in mind that the most expensive action will be loading the collection (600k SKUs means you should probably do it in chunks) and saving the product with a save() method. The latter can be avoided if you first check if the data has actually changed, and if you use $product->getResource()->saveAttribute($product, 'price', 10.00) (not sure about exact method name) instead of saving the whole model.



You might also want to make separate functions for updating the product and the stock item, as the stock item does not operate with such big objects and can maybe run more often.







share|improve this answer














share|improve this answer



share|improve this answer








edited Oct 20 '16 at 21:04

























answered Oct 20 '16 at 6:35









simonthesorcerersimonthesorcerer

3,6272 gold badges11 silver badges28 bronze badges




3,6272 gold badges11 silver badges28 bronze badges












  • Thank you so much for this! After all you guys have mentioned I've began to start thinking about this more in depth. I have been thinking that maybe it would be more wise to update pricing once a day through a product import. Then check inventory when added to cart if the inventory amount was under a specified amount like 10 units left or something similar. What do you think?

    – Ryan Cady
    Oct 20 '16 at 17:52











  • I have updated my answer

    – simonthesorcerer
    Oct 20 '16 at 21:04











  • Awesome! I think this is most likely the best way to do it. I found out that we may be able to have our dump file contain all the pricing and inventory information as well in a csv. If that's true, I'll just create a cronjob that will compare the two and detect changes then push that to the server and update as needed.

    – Ryan Cady
    Oct 21 '16 at 13:49

















  • Thank you so much for this! After all you guys have mentioned I've began to start thinking about this more in depth. I have been thinking that maybe it would be more wise to update pricing once a day through a product import. Then check inventory when added to cart if the inventory amount was under a specified amount like 10 units left or something similar. What do you think?

    – Ryan Cady
    Oct 20 '16 at 17:52











  • I have updated my answer

    – simonthesorcerer
    Oct 20 '16 at 21:04











  • Awesome! I think this is most likely the best way to do it. I found out that we may be able to have our dump file contain all the pricing and inventory information as well in a csv. If that's true, I'll just create a cronjob that will compare the two and detect changes then push that to the server and update as needed.

    – Ryan Cady
    Oct 21 '16 at 13:49
















Thank you so much for this! After all you guys have mentioned I've began to start thinking about this more in depth. I have been thinking that maybe it would be more wise to update pricing once a day through a product import. Then check inventory when added to cart if the inventory amount was under a specified amount like 10 units left or something similar. What do you think?

– Ryan Cady
Oct 20 '16 at 17:52





Thank you so much for this! After all you guys have mentioned I've began to start thinking about this more in depth. I have been thinking that maybe it would be more wise to update pricing once a day through a product import. Then check inventory when added to cart if the inventory amount was under a specified amount like 10 units left or something similar. What do you think?

– Ryan Cady
Oct 20 '16 at 17:52













I have updated my answer

– simonthesorcerer
Oct 20 '16 at 21:04





I have updated my answer

– simonthesorcerer
Oct 20 '16 at 21:04













Awesome! I think this is most likely the best way to do it. I found out that we may be able to have our dump file contain all the pricing and inventory information as well in a csv. If that's true, I'll just create a cronjob that will compare the two and detect changes then push that to the server and update as needed.

– Ryan Cady
Oct 21 '16 at 13:49





Awesome! I think this is most likely the best way to do it. I found out that we may be able to have our dump file contain all the pricing and inventory information as well in a csv. If that's true, I'll just create a cronjob that will compare the two and detect changes then push that to the server and update as needed.

– Ryan Cady
Oct 21 '16 at 13:49

















draft saved

draft discarded
















































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.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f141677%2fmaking-external-api-request-in-product-model%23new-answer', 'question_page');

);

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







Popular posts from this blog

Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

Circuit construction for execution of conditional statements using least significant bitHow are two different registers being used as “control”?How exactly is the stated composite state of the two registers being produced using the $R_zz$ controlled rotations?Efficiently performing controlled rotations in HHLWould this quantum algorithm implementation work?How to prepare a superposed states of odd integers from $1$ to $sqrtN$?Why is this implementation of the order finding algorithm not working?Circuit construction for Hamiltonian simulationHow can I invert the least significant bit of a certain term of a superposed state?Implementing an oracleImplementing a controlled sum operation

Magento 2 “No Payment Methods” in Admin New OrderHow to integrate Paypal Express Checkout with the Magento APIMagento 1.5 - Sales > Order > edit order and shipping methods disappearAuto Invoice Check/Money Order Payment methodAdd more simple payment methods?Shipping methods not showingWhat should I do to change payment methods if changing the configuration has no effects?1.9 - No Payment Methods showing upMy Payment Methods not Showing for downloadable/virtual product when checkout?Magento2 API to access internal payment methodHow to call an existing payment methods in the registration form?