Pro100Tom Pro100Tom - 17 days ago 7
Javascript Question

Which file should I use when assigning products to a category in Magento programatically?

I am new to Magento. I have created a category in the back end of Magento. There are quite few threads about how to assign a product to a category, however I don't understand which file should I use to use that code:



If I understand correctly, the page is generated automatically by Magento, when you create a category and assign products to it. I know there is a
view.phtml
file which is a template of how the webpage should look like. Thank you in advance, guys. Will be waiting for your respond.

Answer

Code examples like that is for developers (which I'm kind of guessing you're primarily not) to put into their own modules, maybe as an observer running on the product save event.

For you I would instead recommend testing out snippets like that in small freestanding files in your magento-folder. At least until you know how to make a proper module this is the easiest way to run some custom code, that perhaps only needs to run once.

Create file triggerCustomAction.php in your Magento root directory, making it accessible from www.yourdomain.com/triggerCustomAction.php to run it once.

Use this as a starting point, with code you need to run at the bottom of the file:

<?php

require 'app/Mage.php';
if (!Mage::isInstalled()) {
    echo "Application is not installed yet, please complete install wizard first.";
    exit;
}

Mage::setIsDeveloperMode(true);
ini_set('display_errors', 1);

// this can be changed from to any store ID number like this:
// Mage::app()->setCurrentStore(xx);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

//enter your code to run below here: