:::: MENU ::::

Magento Tutorial | Magento Blog | Learn Magento 2

Cookies Consent Popup

First you need to create your cron file in `Cron` directory as below


app/code/Vendor/Module/Cron/Mycron.php

    <?php

       namespace Vendor\Module\Cron;

       class Mycron

        {

          protected $logger;

          public function __construct(

        \Psr\Log\LoggerInterface $loggerInterface

          ) {

        $this->logger = $loggerInterface;

          }

          public function execute() {

            //Your Logic/Code here

            //$this->logger->debug('Vendor\Module\Cron\Mycron');

          }

        }


then create cron_groups.xml in `app/code/Vendor/Module/etc/cron_groups.xml.

 it's option step.


    <?xml version="1.0"?>

    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/cron_groups.xsd">

        <group id="vendor_module_cron_group">

            <schedule_generate_every>1</schedule_generate_every>

            <schedule_ahead_for>4</schedule_ahead_for>

            <schedule_lifetime>2</schedule_lifetime>

            <history_cleanup_every>10</history_cleanup_every>

            <history_success_lifetime>60</history_success_lifetime>

            <history_failure_lifetime>600</history_failure_lifetime>

            <use_separate_process>1</use_separate_process>

        </group>

    </config>


This will add entry in admin 

Now for scheduling cron script create crontab.xml on below path

`app/code/Vendor/Module/etc/crontab.xml`. 

Schedule time according to your need. I configured for every 5 minute.


    <?xml version="1.0"?>

    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd">

        <group id="vendor_module_cron_group">

            <job name="vendor_module_cronjob_mycron" instance="Vendor\Module\Cron\Mycron" method="execute">

                <schedule>*/5 * * * *</schedule>

            </job>

        </group>

    </config>


This will execute your cron at every 5th min. your magento cron must be configured on your server or you can run manually by running `php bin/magento cron:run` (run twice for schedule and execute)


Note: you can skip cron_groups.xml step and define default group too as below 

<group id="default">


"catalog_product_save_before" This event gets called for every product save action, including new products.

In order to use it you could do the following in your module:

**app\code\Vendor\Module\etc\webapi_rest\events.xml**


    <?xml version="1.0"?>

    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">

        <event name="catalog_product_save_before">

            <observer name="catalog_product_save_before_check_condition" instance="Vendor\Module\Observer\Productsaveafter" />

        </event>

    </config>


Then in `Productsaveafter.php` you can implement logic to connect to your own software and perform actions like adding or updating the product. 


Example:


**app\code\Vendor\Module\Observer\Productsaveafter.php**


    <?php

      namespace Vendor\Module\Observer;

     class Productsaveafter implements ObserverInterface{    

         public function execute(\Magento\Framework\Event\Observer $observer) {

           $product = $observer->getProduct();

            // Your logic to do stuff with $product       

            }

    }


Happy Coading ... Cheers !!!!!


Magento 2 Database Missing Table Error "inventory_stock_1"

Exception #0 (Zend_Db_Statement_Exception): SQLSTATE[42S02]: Base table or view not found: 1146 Table 'inventory_stock_1' doesn't exist, query was: INSERT INTO `search_tmp_5c4f24124efa61_76233970` SELECT `main_select`.`entity_id`, SUM(score) AS `relevance` FROM (SELECT DISTINCT  `search_index`.`entity_id`, (((0) + (0)) * 1) AS `score` FROM `catalog_product_index_eav` AS `search_index`
 INNER JOIN `catalog_product_entity` AS `product` ON product.entity_id = search_index.entity_id
 INNER JOIN `inventory_stock_1` AS `stock_index` ON stock_index.sku = product.sku
 INNER JOIN `catalog_category_product_index_store1` AS `category_ids_index` ON search_index.entity_id = category_ids_index.product_id AND category_ids_index.store_id = '1' WHERE (search_index.store_id = '1') AND (`search_index`.`attribute_id` = 102 AND `search_index`.`value` in ('2', '4') AND `search_index`.`store_id` = '1') AND (category_ids_index.category_id = 394)) AS `main_select` GROUP BY `entity_id` ORDER BY `relevance` DESC, `entity_id` DESC
 LIMIT 10000
Exception #1 (PDOException): SQLSTATE[42S02]: Base table or view not found: 1146 Table 'inventory_stock_1' doesn't exist

Solution:

Please follow these steps:

1) Open your store database.

2) Remove your current "inventory_stock_1" view and run this one:

CREATE
SQL SECURITY INVOKER
VIEW `inventory_stock_1`
  AS
    SELECT
    DISTINCT    
      legacy_stock_status.product_id,
      legacy_stock_status.website_id,
      legacy_stock_status.stock_id,
      legacy_stock_status.qty quantity,
      legacy_stock_status.stock_status is_salable,
      product.sku
    FROM `cataloginventory_stock_status` `legacy_stock_status`
      INNER JOIN `catalog_product_entity` product
        ON legacy_stock_status.product_id = product.entity_id;

 Reindex your store data then check your store.