0

I'm trying to create a Wholesale: Yes/No drop down attribute on customer accounts in the admin. After creating my module and install script, I get the following error:

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'mycompany_wholesale_setup' for key 'PRIMARY'

Here is what I tried:

app/etc/modules/Mycompany_Wholesale.xml

<?xml version="1.0"?>
    <config>
        <modules>
            <Mycompany_Wholesale>
                <active>true</active>
                <codePool>local</codePool>
                <version>0.1.0</version>
            </Mycompany_Wholesale>
        </modules>
    </config>

app/code/local/Mycompany/Wholesale/etc/config.xml

<?xml version="1.0"?>
    <config>
        <modules>
            <Mycompany_Wholesale>
                <version>0.1.0</version>
            </Mycompany_Wholesale>
        </modules>
        <global>
            <resources>
                <mycompany_wholesale_setup>
                    <setup>
                        <module>Mycompany_Wholesale</module>
                        <class>Mage_Customer_Model_Entity_Setup</class>
                    </setup>
                    <connection>
                        <use>core_setup</use>
                    </connection>
                </mycompany_wholesale_setup>

                <Mycompany_Wholesale_write>
                    <connection>
                        <use>core_write</use>
                    </connection>
                </Mycompany_Wholesale_write>

                <Mycompany_Wholesale_read>
                    <connection>
                        <use>core_read</use>
                    </connection>
                </Mycompany_Wholesale_read>
            </resources>
        </global>   
    </config>

app/code/local/Mycompany/Wholesale/sql/mycompany_wholesale_setup/mysql4-install-0.1.0.php

<?php
$installer = $this;
$installer->startSetup();

$setup = new Mage_Eav_Model_Entity_Setup('core_setup');

$entityTypeId     = $setup->getEntityTypeId('customer');
$attributeSetId   = $setup->getDefaultAttributeSetId($entityTypeId);
$attributeGroupId = $setup->getDefaultAttributeGroupId($entityTypeId, $attributeSetId);

$setup->addAttribute("customer", "wholesale",  array(
    "type"     => "int",
    "backend"  => "",
    "label"    => "Wholesale",
    "input"    => "select",
    'source'   => 'eav/entity_attribute_source_boolean',
    "visible"  => true,
    "required" => false,
    "default"  => "",
    "frontend" => "",
    "unique"   => false,
    "note"     => "Wholesale"
));

        $attribute   = Mage::getSingleton("eav/config")->getAttribute("customer", "wholesale");


$setup->addAttributeToGroup(
    $entityTypeId,
    $attributeSetId,
    $attributeGroupId,
    'wholesale',
    '999'  //sort_order
);

$used_in_forms=array();

$used_in_forms[]="adminhtml_customer";
//$used_in_forms[]="checkout_register";
//$used_in_forms[]="customer_account_create";
//$used_in_forms[]="customer_account_edit";
//$used_in_forms[]="adminhtml_checkout";
        $attribute->setData("used_in_forms", $used_in_forms)
                ->setData("is_used_for_customer_segment", true)
                ->setData("is_system", 0)
                ->setData("is_user_defined", 1)
                ->setData("is_visible", 1)
                ->setData("sort_order", 100)
                ;
        $attribute->save();    

$installer->endSetup();

Any idea what I did wrong or how I can fix the Duplicate entry error? I should also note that I don't have direct access to the DB

1 Answer 1

1

Try this Code

in your config.xml file

<?xml version="1.0"?>
<config>
  <modules>
    <Mycompany_Wholesale>
      <version>0.1.0</version>
    </Mycompany_Wholesale>
  </modules>
  <global>
    <helpers>
      <wholesale>
        <class>Mycompany_Wholesale_Helper</class>
      </wholesale>
    </helpers>
    <models>
      <wholesale>
        <class>Mycompany_Wholesale_Model</class>
        <resourceModel>wholesale_mysql4</resourceModel>
      </wholesale>
    </models>
    <resources>
      <customerattribute_setup>
        <setup>
          <module>Mycompany_Wholesale</module>
          <class>Mage_Customer_Model_Entity_Setup</class>
        </setup>
        <connection>
          <use>core_setup</use>
        </connection>
      </customerattribute_setup>
      <customerattribute_write>
        <connection>
          <use>core_write</use>
        </connection>
      </customerattribute_write>
      <customerattribute_read>
        <connection>
          <use>core_read</use>
        </connection>
      </customerattribute_read>
    </resources>
  </global>
</config> 

and in your install file customerattribute_setup/mysql4-install-0.1.0.php

<?php
$installer = $this;
$installer->startSetup();


$installer->addAttribute("customer", "wholesale",  array(
    "type"     => "int",
    "backend"  => "",
    "label"    => "Wholesale",
    "input"    => "select",
    "source"   => "eav/entity_attribute_source_boolean",
    "visible"  => true,
    "required" => false,
    "default" => "",
    "frontend" => "",
    "unique"     => false,
    "note"       => ""

    ));

$attribute   = Mage::getSingleton("eav/config")->getAttribute("customer", "wholesale");


$used_in_forms=array();

$used_in_forms[]="adminhtml_customer";
        $attribute->setData("used_in_forms", $used_in_forms)
        ->setData("is_used_for_customer_segment", true)
        ->setData("is_system", 0)
        ->setData("is_user_defined", 1)
        ->setData("is_visible", 1)
        ->setData("sort_order", 100)
        ;
        $attribute->save();



$installer->endSetup();

Please note, I used customerattribute_setup instead of mycompany_wholesale_setup, So you only need to adjust this as you wish

1
  • Thank you, that worked and I kept it as you wrote using customerattribute_setup
    – AJK
    Commented Mar 28, 2018 at 20:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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