1

I am having a feed which is run every night with crons. My instance is magento 2.4.6-p2.

I am using this function to get store id:

public function execute()
    {
        foreach ($this->storeManager->getStores() as $store) {
            // Only generate feeds when feed notification is active
            if ($this->coreConfig->isDailyFeedActive($store->getId())) {
                $this->logger->debug('MyModule: Nightly Feeds being run for Store View ' . $store->getId());
                $this->feedRunner->allFeeds($store->getId());
            }
        }
    }

But it return error when cron run:

Argument #1 ($storeId) must be of type int, string given

I can't figure out what I do wrong and I can't get the store id. Instead of getting a number I get a string as error says

Any help please??

2 Answers 2

1

It seems that the $store->getId() method returns a string value. You should convert this value to an integer using (int)$store->getId() to match the method's requirement.

You can optimize your code with the following implementation:

public function execute()
{
    $stores = $this->storeManager->getStores();
    foreach ($stores as $store) {
        $storeId = (int)$store->getId();
        // Only generate feeds when feed notification is active
        if ($this->coreConfig->isDailyFeedActive($storeId)) {
            $this->logger->debug('MyModule: Nightly Feeds being run for Store View ' . $storeId);
            $this->feedRunner->allFeeds($storeId);
        }
    }
}
2
  • Now it throws error: Warning: Undefined variable $store in this line: foreach ($store as $store) {
    – G. G.
    Commented Nov 1, 2023 at 17:19
  • 1
    @G.G. there is a typo, please replace foreach ($store as $store) { with foreach ($stores as $store) {
    – Tu Van
    Commented Nov 1, 2023 at 23:50
1
Argument #1 ($storeId) must be of type int, string given

The above error indicates that you are trying to use a variable as an argument for a function or method that expects an integer (int) as the first argument, but you are passing a string instead.You can use type hints to specify the expected data type for function or method arguments. If a function expects an argument of type int, you should pass an integer value, not a string.

The $store->getId() method returns a value as a string, which is not acceptable for your isDailyFeedActive() and allFeeds() methods. To resolve this, you should convert the string value into an integer using the int() PHP function.

$storeId = (int)$store->getId();
$this->coreConfig->isDailyFeedActive($storeId);

OR

$this->coreConfig->isDailyFeedActive((int)$store->getId());

You have to use PHP 8 in Magento 2.4.6.

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.