<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Agence de Développement Informatique du Nord &#187; increment_id</title>
	<atom:link href="http://blog.adin.pro/tag/increment_id/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.adin.pro</link>
	<description>Just another WordPress site</description>
	<lastBuildDate>Thu, 26 Dec 2019 08:54:31 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=4.1.1</generator>
	<item>
		<title>Magento SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry &#8216;000000254&#8217; for key &#8216;UNQ_SALES_FLAT_ORDER_INCREMENT_ID&#8217;</title>
		<link>http://blog.adin.pro/2014-12-02/magento-sqlstate23000-integrity-constraint-violation-1062-duplicate-entry-000000254-for-key-unq_sales_flat_order_increment_id/</link>
		<comments>http://blog.adin.pro/2014-12-02/magento-sqlstate23000-integrity-constraint-violation-1062-duplicate-entry-000000254-for-key-unq_sales_flat_order_increment_id/#comments</comments>
		<pubDate>Tue, 02 Dec 2014 10:11:12 +0000</pubDate>
		<dc:creator><![CDATA[blogadmin]]></dc:creator>
				<category><![CDATA[Magento]]></category>
		<category><![CDATA[increment_id]]></category>
		<category><![CDATA[magento]]></category>

		<guid isPermaLink="false">http://blog.adin.pro/?p=427</guid>
		<description><![CDATA[<p>In some cases, customers encounter this error on your website : You don&#8217;t understand and drive you crazy ? Don&#8217;t worry, I will help you. First, this error probably happend when customer quit violenty the payment page, without canceling, so &#8230; <a href="http://blog.adin.pro/2014-12-02/magento-sqlstate23000-integrity-constraint-violation-1062-duplicate-entry-000000254-for-key-unq_sales_flat_order_increment_id/">Continue reading <span class="meta-nav">&#8594;</span></a></p>
<p>The post <a rel="nofollow" href="http://blog.adin.pro/2014-12-02/magento-sqlstate23000-integrity-constraint-violation-1062-duplicate-entry-000000254-for-key-unq_sales_flat_order_increment_id/">Magento SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry &#8216;000000254&#8217; for key &#8216;UNQ_SALES_FLAT_ORDER_INCREMENT_ID&#8217;</a> appeared first on <a rel="nofollow" href="http://blog.adin.pro">Agence de Développement Informatique du Nord</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>In some cases, customers encounter this error on your website :</p>
<pre class="brush: php; title: ; notranslate">
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '000000254' for key 'UNQ_SALES_FLAT_ORDER_INCREMENT_ID'
</pre>
<p>You don&#8217;t understand and drive you crazy ?<br />
Don&#8217;t worry, I will help you.</p>
<p>First, this error probably happend when customer quit violenty the payment page, without canceling, so your order is not &#8220;finished&#8221; and your quote is not &#8220;released&#8221; (is_active = 1 in db). Customer come back, recover his quote and try to proceed it again.<br />
The reserved_increment_id is not updated and when magento try to create the order, mySQL give him the previous error.</p>
<p>How a so obvious bug like this can exist ? and why does it crash only on my website, it seems to work fine on other website ?<br />
Let me guest, your client (clients always have weird needs &#8230;) ask you to start order number at 0 instead of 100000000, or ask you to put a letter into it.</p>
<p>So, why does this reserved_increment_id is not updated ?<br />
Let check on the function which update the reserved_increment_id field :</p>
<pre class="brush: php; title: ; notranslate">
// /app/code/core/Mage/Sales/Model/Resource/Quote.php
    /**
     * Check is order increment id use in sales/order table
     *
     * @param int $orderIncrementId
     * @return boolean
     */
    public function isOrderIncrementIdUsed($orderIncrementId)
    {
        $adapter   = $this-&gt;_getReadAdapter();
        $bind      = array(':increment_id' =&gt; (int)$orderIncrementId);
        $select    = $adapter-&gt;select();
        $select-&gt;from($this-&gt;getTable('sales/order'), 'entity_id')
            -&gt;where('increment_id = :increment_id');
        $entity_id = $adapter-&gt;fetchOne($select, $bind);
        if ($entity_id &gt; 0) {
            return true;
        }

        return false;
    }
</pre>
<p>You see it, the little (int) in this line $bind      = array(&#8216;:increment_id&#8217; => (int)$orderIncrementId); ?<br />
This transform your increment_id in your request from &#8220;000000254&#8221; to &#8220;254&#8221;, mySQL doesn&#8217;t find an order with the increment_id &#8220;254&#8221; and your quote is not updated.</p>
<p>To fix it, override this function :<br />
In your Sales/etc/config.xml file, add</p>
<pre class="brush: xml; title: ; notranslate">
&lt;global&gt;
  &lt;model&gt;
    &lt;sales_resource&gt;
                &lt;rewrite&gt;
                    &lt;quote&gt;Adin_Sales_Model_Resource_Sales_Quote&lt;/quote&gt;
                &lt;/rewrite&gt;
            &lt;/sales_resource&gt;
   &lt;/model&gt;
&lt;/global&gt;
</pre>
<p>And create the new class file /app/code/local/Adin/Sales/Model/Resource/Sales/Quote.php</p>
<pre class="brush: php; title: ; notranslate">
class Adin_Sales_Model_Resource_Sales_Quote extends Mage_Sales_Model_Resource_Quote {

    /**
     * Check is order increment id use in sales/order table
     *
     * @param int $orderIncrementId
     * @return boolean
     */
    public function isOrderIncrementIdUsed($orderIncrementId)
    {
        $adapter   = $this-&gt;_getReadAdapter();
        $bind      = array(':increment_id' =&gt; $orderIncrementId);
        $select    = $adapter-&gt;select();
        $select-&gt;from($this-&gt;getTable('sales/order'), 'entity_id')
            -&gt;where('increment_id = :increment_id');
        $entity_id = $adapter-&gt;fetchOne($select, $bind);
        if ($entity_id &gt; 0) {
            return true;
        }

        return false;
    }

}
</pre>
<p>That should do the tricks.</p>
<p>The post <a rel="nofollow" href="http://blog.adin.pro/2014-12-02/magento-sqlstate23000-integrity-constraint-violation-1062-duplicate-entry-000000254-for-key-unq_sales_flat_order_increment_id/">Magento SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry &#8216;000000254&#8217; for key &#8216;UNQ_SALES_FLAT_ORDER_INCREMENT_ID&#8217;</a> appeared first on <a rel="nofollow" href="http://blog.adin.pro">Agence de Développement Informatique du Nord</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.adin.pro/2014-12-02/magento-sqlstate23000-integrity-constraint-violation-1062-duplicate-entry-000000254-for-key-unq_sales_flat_order_increment_id/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
