4

I have 10000 products in my magento store and I had to make these products available for particular countries, so I like to proceed with the task by adding available country as a new attribute for each product, how can I proceed with this?

  • You're approaching the problem wrong. Once you've added the attribute (which is a lot of work), you probably want to then actually use that attribute to determine whether the product is available (which is a lot of work). Look instead at how you can group products and customers using existing features. Consider running different product stores instead. – Paul Gregory Apr 25 '14 at 14:47
  • thanks for your reply , we are planning to select available countries by data entry. so i am wondering how to add a new attribute with all countries list simillar to country of manufacture – user2830891 Apr 25 '14 at 14:56
4

Granted I agree with Paul's comment, I'll answer the question as you need, i.e. creating an attribute with a list a countries.

Unfortunately, creating the attribute in the admin panel is no easy solution. You would have to manually input each and every other country manually. Which would be redundant as the list of countries is already available in Magento.

To create a country attribute in Magento, you need to create the attribute in an install (or upgrade) script and set a source model for it.

Mangeto offers two models which loads the countries:

  1. Mage_Adminhtml_Model_System_Config_Source_Country
  2. Mage_Catalog_Model_Product_Attribute_Source_Countryofmanufacture

The first, can only be used a source for configuration entries and cannot be used as a product attribute.

The second, although its name could have been a more generic one, can be used as a source for the countries' list. What it does, is load the list of countries and matches them with the available countries selected in the admin panel.

For a more complete (and ocd-less) solution, you can create your own source model and use a different name for it and make it extend the Countryofmanufacture model or just re-implement the getAllOptions() method in case you need to remove the first empty element.

In your install/upgrade script, you need the following code. I'll explain it inline in the code as PHP comments

/*
 * Due to the large amount of variables & options here,
 * I'll start by setting them first,
 * and you change them as needed
 */

/*
 * I'm using the default attribute set here, if you need other
 * or more attribute sets, just replace `Default` or add it as an
 * extra array item.
 */
$attributeSets  = array(
    'Default',
);
$attributeLabel = 'Available in Countries';
$attributeCode  = 'available_in_countries';
$attributeGroup = 'General';
$attributeOrder  = 100;

$attributeOptions = array(
    // This is the the label text which will show in the admin panel
    'label'                      => $attributeLabel,
    // You can change the input to 'select' instead of 'multiselect'
    // if you don't want to allow multi selection
    'input'                      => 'multiselect',
    // A multiple select is saved as varchar in the database,
    // besides the country ids are actually text.
    'type'                       => 'varchar',
    // Set the 'backend' to null if changed 'multiselect' to 'select'
    'backend'                    => 'eav/entity_attribute_backend_array',
    'frontend'                   => null,
    // This line is where the magic happens. We are setting a source
    // model as the values of attribute. Each time its data needs
    // to be loaded, it will go to that model and load the return
    // values of its 'getAllOptions()' method
    'source'                     => 'catalog/product_attribute_source_countryofmanufacture',
    'global'                     => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    'required'                   => false,
    // You can set this to true, but leaving it as false removes some
    // admin capabilities like deleting the attribute from admin panel.
    'user_defined'               => false,
    // Change the following option to true
    // if the attribute should be required.
    'required'                   => false,
    'class'                      => null,
    'unique'                     => false,
    'searchable'                 => false,
    'filterable'                 => false,
    'comparable'                 => false,
    'visible_on_front'           => false,
    'is_configurable'            => false,
    'visible'                    => true,
);

/*
 * The following method will finally create the attribute
 */
$this->addAttribute(
    Mage_Catalog_Model_Product::ENTITY,
    $attributeCode,
    $attributeOptions, $attributeCode
);

/*
 * This block of code will add the attribute to the attribute sets
 * which are set in the $attributeSets variable
 */
foreach ($attributeSets as $attributeSet) {
    $this->addAttributeToSet(
        Mage_Catalog_Model_Product::ENTITY, $attributeSet,
        $attributeGroup, $attributeCode, $attributeOrder
    );
}

Disclaimer: This answer takes into account that you already know how to create setup scripts, and that your setup script uses the Mage_Eav_Model_Entity_Setup class or a custom class which extends that one.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.