Programatically update magento attribututes per store but code is updating all stores The Next...

Why do we use the plural of movies in this phrase "We went to the movies last night."?

What does convergence in distribution "in the Gromov–Hausdorff" sense mean?

Why do variable in an inner function return nan when there is the same variable name at the inner function declared after log

What benefits would be gained by using human laborers instead of drones in deep sea mining?

If the heap is initialized for security, then why is the stack uninitialized?

Interfacing a button to MCU (and PC) with 50m long cable

Inappropriate reference requests from Journal reviewers

I believe this to be a fraud - hired, then asked to cash check and send cash as Bitcoin

What happened in Rome, when the western empire "fell"?

What happens if you roll doubles 3 times then land on "Go to jail?"

What exact does MIB represent in SNMP? How is it different from OID?

Why does the UK parliament need a vote on the political declaration?

Should I tutor a student who I know has cheated on their homework?

Novel about a guy who is possessed by the divine essence and the world ends?

Is micro rebar a better way to reinforce concrete than rebar?

Is there a difference between "Fahrstuhl" and "Aufzug"

Why is the US ranked as #45 in Press Freedom ratings, despite its extremely permissive free speech laws?

Sending manuscript to multiple publishers

How to safely derail a train during transit?

Make solar eclipses exceedingly rare, but still have new moons

What is "(CFMCC)" on an ILS approach chart?

Unreliable Magic - Is it worth it?

Contours of a clandestine nature

Can you replace a racial trait cantrip when leveling up?



Programatically update magento attribututes per store but code is updating all stores



The Next CEO of Stack OverflowManage different stocks for different stores/store views in magento 1.9Trying to update Manufacturer value but not working MagentoMagento Different Transactional Emails Per Store ViewProgramatically update store name and website nameProgramatically update a single attribute in Store View ScopeMagento 2 - How to assign custom theme to specific store programaticallyBest setup for Multi-Warehouse Stock per store level? MAGENTO 1.9Magento 2 : create multi-store product programaticallyMagento 2.2.6: Programatically updating product custom optionsMagento 2 - Programatically updating the inventory store wise not working












1















I have a Magento multi store setup and have 2 csv files that will be uploaded on a daily bases to update price, qty, and other attributes.



I would like to create two of the following files that run on cron and each file will do the same thing for each store individually.



Currently the code below takes takes store1.csv and applies it to both stores also if i run this code with store3.csv it will apply all price and updates to both stores.



Would like for store1.csv to only update store 1 and store3.csv only update attributes on store 3.



    <?php
/*Move to our working directory
$home = getenv("HOME");
if (! $home) {
chdir('../'); // We hope we are somewhere where this works
} else {
chdir($home.'/project/html');
}*/
$csv = "store3.csv";
if (sizeof($argv) > 1) {
$csv = $argv[1];
}
//Turn On Error Reporting
error_reporting(E_ALL | E_STRICT);
//BOOTSTRAP MAGENTO
$mageFilename = '../app/Mage.php';
require_once $mageFilename;
//require_once 'relatedProducts.php';
Mage::app()->setCurrentStore(Mage::getModel('core/store')->setStoreId(3));
Mage::setIsDeveloperMode(true);
umask(0);

//OPEN CSV
if (($handle = fopen($csv, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 2000, "t")) !== FALSE) {
$num = count($data);
if ($num < 1) continue; // skip blank lines
$sku = trim($data[0]);
if ($num < 2) {
echo "Skipping: ".$sku." not enough fieldsn";
continue;
}
$qty = trim($data[1]);
$price = trim($data[2]);
// grab the product based on sku.
$product = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku)->setStoreId(3);
if(!$product) {
print "Error: Invalid SKU, ".$sku."n";
continue;
}
if ($product->getPrice() != $price) {
$product->setPrice($price);
$product->save();
}
// Grab the inventory(stock) model in order to update quantities.
$stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product->getId())->setStoreId(3);
if ($stockItem->getData('qty') != $qty) {
$stockItem->setData('qty', $qty);
if ($qty > 0) {
$stockItem->setData('is_in_stock', 1);
}
$stockItem->save();
}
}
fclose($handle);
}
?>









share|improve this question

























  • David, did you solved your problem? if yes can you post the code, I have the same problem now, thanks

    – Alberto
    Jul 19 '16 at 14:42
















1















I have a Magento multi store setup and have 2 csv files that will be uploaded on a daily bases to update price, qty, and other attributes.



I would like to create two of the following files that run on cron and each file will do the same thing for each store individually.



Currently the code below takes takes store1.csv and applies it to both stores also if i run this code with store3.csv it will apply all price and updates to both stores.



Would like for store1.csv to only update store 1 and store3.csv only update attributes on store 3.



    <?php
/*Move to our working directory
$home = getenv("HOME");
if (! $home) {
chdir('../'); // We hope we are somewhere where this works
} else {
chdir($home.'/project/html');
}*/
$csv = "store3.csv";
if (sizeof($argv) > 1) {
$csv = $argv[1];
}
//Turn On Error Reporting
error_reporting(E_ALL | E_STRICT);
//BOOTSTRAP MAGENTO
$mageFilename = '../app/Mage.php';
require_once $mageFilename;
//require_once 'relatedProducts.php';
Mage::app()->setCurrentStore(Mage::getModel('core/store')->setStoreId(3));
Mage::setIsDeveloperMode(true);
umask(0);

//OPEN CSV
if (($handle = fopen($csv, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 2000, "t")) !== FALSE) {
$num = count($data);
if ($num < 1) continue; // skip blank lines
$sku = trim($data[0]);
if ($num < 2) {
echo "Skipping: ".$sku." not enough fieldsn";
continue;
}
$qty = trim($data[1]);
$price = trim($data[2]);
// grab the product based on sku.
$product = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku)->setStoreId(3);
if(!$product) {
print "Error: Invalid SKU, ".$sku."n";
continue;
}
if ($product->getPrice() != $price) {
$product->setPrice($price);
$product->save();
}
// Grab the inventory(stock) model in order to update quantities.
$stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product->getId())->setStoreId(3);
if ($stockItem->getData('qty') != $qty) {
$stockItem->setData('qty', $qty);
if ($qty > 0) {
$stockItem->setData('is_in_stock', 1);
}
$stockItem->save();
}
}
fclose($handle);
}
?>









share|improve this question

























  • David, did you solved your problem? if yes can you post the code, I have the same problem now, thanks

    – Alberto
    Jul 19 '16 at 14:42














1












1








1








I have a Magento multi store setup and have 2 csv files that will be uploaded on a daily bases to update price, qty, and other attributes.



I would like to create two of the following files that run on cron and each file will do the same thing for each store individually.



Currently the code below takes takes store1.csv and applies it to both stores also if i run this code with store3.csv it will apply all price and updates to both stores.



Would like for store1.csv to only update store 1 and store3.csv only update attributes on store 3.



    <?php
/*Move to our working directory
$home = getenv("HOME");
if (! $home) {
chdir('../'); // We hope we are somewhere where this works
} else {
chdir($home.'/project/html');
}*/
$csv = "store3.csv";
if (sizeof($argv) > 1) {
$csv = $argv[1];
}
//Turn On Error Reporting
error_reporting(E_ALL | E_STRICT);
//BOOTSTRAP MAGENTO
$mageFilename = '../app/Mage.php';
require_once $mageFilename;
//require_once 'relatedProducts.php';
Mage::app()->setCurrentStore(Mage::getModel('core/store')->setStoreId(3));
Mage::setIsDeveloperMode(true);
umask(0);

//OPEN CSV
if (($handle = fopen($csv, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 2000, "t")) !== FALSE) {
$num = count($data);
if ($num < 1) continue; // skip blank lines
$sku = trim($data[0]);
if ($num < 2) {
echo "Skipping: ".$sku." not enough fieldsn";
continue;
}
$qty = trim($data[1]);
$price = trim($data[2]);
// grab the product based on sku.
$product = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku)->setStoreId(3);
if(!$product) {
print "Error: Invalid SKU, ".$sku."n";
continue;
}
if ($product->getPrice() != $price) {
$product->setPrice($price);
$product->save();
}
// Grab the inventory(stock) model in order to update quantities.
$stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product->getId())->setStoreId(3);
if ($stockItem->getData('qty') != $qty) {
$stockItem->setData('qty', $qty);
if ($qty > 0) {
$stockItem->setData('is_in_stock', 1);
}
$stockItem->save();
}
}
fclose($handle);
}
?>









share|improve this question
















I have a Magento multi store setup and have 2 csv files that will be uploaded on a daily bases to update price, qty, and other attributes.



I would like to create two of the following files that run on cron and each file will do the same thing for each store individually.



Currently the code below takes takes store1.csv and applies it to both stores also if i run this code with store3.csv it will apply all price and updates to both stores.



Would like for store1.csv to only update store 1 and store3.csv only update attributes on store 3.



    <?php
/*Move to our working directory
$home = getenv("HOME");
if (! $home) {
chdir('../'); // We hope we are somewhere where this works
} else {
chdir($home.'/project/html');
}*/
$csv = "store3.csv";
if (sizeof($argv) > 1) {
$csv = $argv[1];
}
//Turn On Error Reporting
error_reporting(E_ALL | E_STRICT);
//BOOTSTRAP MAGENTO
$mageFilename = '../app/Mage.php';
require_once $mageFilename;
//require_once 'relatedProducts.php';
Mage::app()->setCurrentStore(Mage::getModel('core/store')->setStoreId(3));
Mage::setIsDeveloperMode(true);
umask(0);

//OPEN CSV
if (($handle = fopen($csv, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 2000, "t")) !== FALSE) {
$num = count($data);
if ($num < 1) continue; // skip blank lines
$sku = trim($data[0]);
if ($num < 2) {
echo "Skipping: ".$sku." not enough fieldsn";
continue;
}
$qty = trim($data[1]);
$price = trim($data[2]);
// grab the product based on sku.
$product = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku)->setStoreId(3);
if(!$product) {
print "Error: Invalid SKU, ".$sku."n";
continue;
}
if ($product->getPrice() != $price) {
$product->setPrice($price);
$product->save();
}
// Grab the inventory(stock) model in order to update quantities.
$stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product->getId())->setStoreId(3);
if ($stockItem->getData('qty') != $qty) {
$stockItem->setData('qty', $qty);
if ($qty > 0) {
$stockItem->setData('is_in_stock', 1);
}
$stockItem->save();
}
}
fclose($handle);
}
?>






attributes ce-1.9.0.1 inventory programmatically






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 13 mins ago









Teja Bhagavan Kollepara

3,01241949




3,01241949










asked Sep 14 '15 at 19:00









DavidDavid

134




134













  • David, did you solved your problem? if yes can you post the code, I have the same problem now, thanks

    – Alberto
    Jul 19 '16 at 14:42



















  • David, did you solved your problem? if yes can you post the code, I have the same problem now, thanks

    – Alberto
    Jul 19 '16 at 14:42

















David, did you solved your problem? if yes can you post the code, I have the same problem now, thanks

– Alberto
Jul 19 '16 at 14:42





David, did you solved your problem? if yes can you post the code, I have the same problem now, thanks

– Alberto
Jul 19 '16 at 14:42










1 Answer
1






active

oldest

votes


















2














The store ID should be set before loading the product.



$product = Mage::getModel('catalog/product')->setStoreId(3)->loadByAttribute('sku', $sku);


Afterwards it doesn't have any influence on the loaded data.



Also make sure price is set to website level under System > Configuration > Catalog > Price.



As far as stock quantity goes, that can only be set on global level. Per SKU there is only one stock level






share|improve this answer
























    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%2f83079%2fprogramatically-update-magento-attribututes-per-store-but-code-is-updating-all-s%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









    2














    The store ID should be set before loading the product.



    $product = Mage::getModel('catalog/product')->setStoreId(3)->loadByAttribute('sku', $sku);


    Afterwards it doesn't have any influence on the loaded data.



    Also make sure price is set to website level under System > Configuration > Catalog > Price.



    As far as stock quantity goes, that can only be set on global level. Per SKU there is only one stock level






    share|improve this answer




























      2














      The store ID should be set before loading the product.



      $product = Mage::getModel('catalog/product')->setStoreId(3)->loadByAttribute('sku', $sku);


      Afterwards it doesn't have any influence on the loaded data.



      Also make sure price is set to website level under System > Configuration > Catalog > Price.



      As far as stock quantity goes, that can only be set on global level. Per SKU there is only one stock level






      share|improve this answer


























        2












        2








        2







        The store ID should be set before loading the product.



        $product = Mage::getModel('catalog/product')->setStoreId(3)->loadByAttribute('sku', $sku);


        Afterwards it doesn't have any influence on the loaded data.



        Also make sure price is set to website level under System > Configuration > Catalog > Price.



        As far as stock quantity goes, that can only be set on global level. Per SKU there is only one stock level






        share|improve this answer













        The store ID should be set before loading the product.



        $product = Mage::getModel('catalog/product')->setStoreId(3)->loadByAttribute('sku', $sku);


        Afterwards it doesn't have any influence on the loaded data.



        Also make sure price is set to website level under System > Configuration > Catalog > Price.



        As far as stock quantity goes, that can only be set on global level. Per SKU there is only one stock level







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Sep 14 '15 at 19:04









        Sander MangelSander Mangel

        35k571138




        35k571138






























            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%2f83079%2fprogramatically-update-magento-attribututes-per-store-but-code-is-updating-all-s%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

            “%fieldName is a required field.”, in Magento2 REST API Call for GET Method Type The Next...

            How to change City field to a dropdown in Checkout step Magento 2Magento 2 : How to change UI field(s)...

            變成蝙蝠會怎樣? 參考資料 外部連結 导航菜单Thomas Nagel, "What is it like to be a...