0

I want to override file in vendor\magento\module-sales-sequence\Model\Sequence.php

Magentsales\Modulesequence\registration.php

<?php
 \Magento\Framework\Component\ComponentRegistrar::register(
 \Magento\Framework\Component\ComponentRegistrar::MODULE,
 'Magentsales_Modulesequence',
 __DIR__
 );

Magentsales\Modulesequence\etc\di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Framework\DB\Sequence\SequenceInterface">
    <arguments>
        <argument name="pattern" xsi:type="string">%s%'.04d%s</argument>
    </arguments>
</type>

<preference for="Magento\SalesSequence\Model\Sequence" type="Magentsales\Modulesequence\Model\SalesSequence\Sequence" />
</config>

Magentsales\Modulesequence\etc\module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
 <module name="Magentsales_Modulesequence" setup_version="1.0.1"></module>
</config>

Magentsales\Modulesequence\Model\SalesSequence\Sequence.php

 <?php
 namespace Magentsales\Modulesequence\Model\SalesSequence; 
 class Sequence extends \Magento\SalesSequence\Model\Sequence
 {  
 public function getCurrentValue()
{
  if (!isset($this->lastIncrementId)) {
        return null;
    }
    $prefix = "HLS".date("-y");

    return sprintf($this->pattern, $prefix, $this->calculateCurrentValue(), $this->meta->getActiveProfile()->getSuffix());
   }
 }

i edited in file Sequnce.php file in "getCurrentValue()" for rename order and invoice number but it's not getting. Please let me know any thing wrong in module?

  • Try adding sequence in "Magentsales\Modulesequence\etc\module.xml" file for core module like this ``` <sequence> <module name="Magento_SalesSequence"/> </sequence> ``` within <module> tag – Prachi Saxena Jul 29 at 12:38
  • There is a private function in that file calculateCurrentValue() which wouldn't be called from your function so you need to add that function also in your overided file.. – aravind Jul 29 at 12:47
1

Modify Magentsales\Modulesequence\Model\SalesSequence\Sequence.php

<?php
namespace Magentsales\Modulesequence\Model\SalesSequence;

use Magento\SalesSequence\Model\Meta;
use Magento\Framework\App\ResourceConnection as AppResource;

class Sequence extends \Magento\SalesSequence\Model\Sequence
{
    /**
     * @var string
     */
    private $lastIncrementId;

    /**
     * @var Meta
     */
    private $meta;

    /**
     * @var false|\Magento\Framework\DB\Adapter\AdapterInterface
     */
    private $connection;

    /**
     * @var string
     */
    private $pattern;

    /**
     * @param Meta $meta
     * @param AppResource $resource
     * @param string $pattern
     */
    public function __construct(
        Meta $meta,
        AppResource $resource,
        $pattern = self::DEFAULT_PATTERN
    ) {
        $this->meta = $meta;
        $this->connection = $resource->getConnection('sales');
        $this->pattern = $pattern;
        parent::__construct($meta, $resource, $pattern);
    }

    /**
     * Retrieve current value
     *
     * @return string
     */
    public function getCurrentValue()
    {
        if (!isset($this->lastIncrementId)) {
            return null;
        }

        $prefix = "HLS".date("-y");
        return sprintf(
            $this->pattern,
            $prefix,
            $this->calculateCurrentValue(),
            $this->meta->getActiveProfile()->getSuffix()
        );
    }

    /**
     * Retrieve next value
     *
     * @return string
     */
    public function getNextValue()
    {
        $this->connection->insert($this->meta->getSequenceTable(), []);
        $this->lastIncrementId = $this->connection->lastInsertId($this->meta->getSequenceTable());
        return $this->getCurrentValue();
    }

    /**
     * Calculate current value depends on start value
     *
     * @return string
     */
    private function calculateCurrentValue()
    {
        return ($this->lastIncrementId - $this->meta->getActiveProfile()->getStartValue())
            * $this->meta->getActiveProfile()->getStep() + $this->meta->getActiveProfile()->getStartValue();
    }
}
  • Hi @Sohel , it's working thank you – Priya Jul 30 at 4:06
2

please try this code:-

CompanyName/ModuleName/etc/di.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\SalesSequence\Model\Sequence" type="CompanyName\ModuleName\Model\Sequence" />
</config>

CompanyName/ModuleName/Model/Sequence.php

<?php

namespace CompanyName\ModuleName\Model;

use Magento\Framework\App\ResourceConnection as AppResource;
use Magento\Framework\DB\Sequence\SequenceInterface;
use Magento\SalesSequence\Model\Meta;


class Sequence extends \Magento\SalesSequence\Model\Sequence
{

    /**
     * @var string
     */
    private $lastIncrementId;

    /**
     * @var Meta
     */
    private $meta;

    /**
     * @var false|\Magento\Framework\DB\Adapter\AdapterInterface
     */
    private $connection;

    /**
     * @param Meta $meta
     * @param AppResource $resource
     * @param string $pattern
     */
    public function __construct(
        Meta $meta,
        AppResource $resource,
        $pattern = \Magento\SalesSequence\Model\Sequence::DEFAULT_PATTERN
    ) {
        $this->meta = $meta;
        $this->connection = $resource->getConnection('sales');
        $this->pattern = $pattern;
    }

 public function getCurrentValue()
{
  if (!isset($this->lastIncrementId)) {
        return null;
    }
    $prefix = "HLS".date("-y");

    return sprintf($this->pattern, $prefix, $this->calculateCurrentValue(), $this->meta->getActiveProfile()->getSuffix());

 }

}

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.