Magento 2 :: How to setup Cron Job

Setup Cron In Local System.

1. Run the following command

crontab -e

2. Add the following command using terminal.

* * * * * /usr/bin/php /var/www/html/your_magento2_root_folder/bin/magento cron:run | grep -v "Run jobs by schedule" >> /var/www/html/your_magento2_root_folder/var/log/magento.cron.log
* * * * * /usr/bin/php /var/www/html/your_magento2_root_folder/update/cron.php >> /var/www/html/your_magento2_root_folder/var/log/update.cron.log
* * * * * /usr/bin/php /var/www/html/your_magento2_root_folder/bin/magento setup:cron:run >> /var/www/html/your_magento2_root_folder/var/log/setup.cron.log

Add Cron Jobs in Magento Custom Module.

1.Create crontab.xml

<?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="default">
		<job instance="Namespace\Module\Cron\Taskname" method="execute" name="your_cron_job_title">
			<schedule>*/5 * * * *</schedule>
		</job>
	</group>
</config>

2.Create Taskname.php

File Path: File: app/code/Namespace/Module/Cron/Taskname.php

<?php

namespace Namespace\Module\Cron;

class Taskname {

	public function execute() {

		$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/cron.log');
		$logger = new \Zend\Log\Logger();
		$logger->addWriter($writer);
		$logger->info(__METHOD__);

		return $this;

	}
}

Advertisements

Magento 2: Remove upsell products or move upsell products

1. If you want to remove the upsell products from product details page.
Open Your theme catalog_product_view.xml and add the following code.
File location:
app/design/frontend/Vendor/Theme/Magento_Catalog/layout/catalog_product_view.xml

<referenceBlock name="product.info.upsell" remove="true"/>

2. Add upsell product block under Tab section.

<referenceBlock name="product.info.details">
  	<block class="Magento\Catalog\Block\Product\ProductList\Upsell" template="Magento_Catalog::product/list/items.phtml" name="upsell.tab" as="upselltab" group="detailed_info" >
        <arguments>
         	<argument translate="true" name="title" xsi:type="string">Upsell</argument>
         	<argument name="type" xsi:type="string">upsell</argument>
        </arguments>
        <block class="Magento\Catalog\Block\Product\ProductList\Item\Container" name="upsell.product.addto" as="addto">
            <block class="Magento\Catalog\Block\Product\ProductList\Item\AddTo\Compare" name="upsell.product.addto.compare" as="compare" template="Magento_Catalog::product/list/addto/compare.phtml"/>
        </block>
  	</block>
</referenceBlock>

Magento2 :: Get System Config Data Using Data Helper

<?php

namespace YourNamespace\Module\Helper;

class Data extends \Magento\Framework\App\Helper\AbstractHelper {
    /**
    * @var \Magento\Framework\App\Config\ScopeConfigInterface
    */
    protected $scopeConfig;

    /**
     * Recipient Top Static Block config path
     */
    const XML_PATH_CMS_BLOCK_TOP = 'section_id/groupid/field_id';

	public function __construct(
		\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
	) {
		$this->scopeConfig = $scopeConfig;
	}

    /**
    * function returning config value
    */

	public function getConfigData() {
	
		$storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
		return $this->scopeConfig->getValue(self::XML_PATH_EMAIL_RECIPIENT, $storeScope);
    }
}

Magento 2 :: Custom Breadcrumbs using layout update

<referenceContainer name="page.wrapper">
    <referenceBlock name="breadcrumbs" remove="true"/>
    <referenceContainer name="page.top">
        <block class="Magento\Theme\Block\Html\Breadcrumbs" name="breadcrumbs.cms">
            <action method="addCrumb">
                <argument name="crumbName" xsi:type="string">Home</argument>
                <argument name="crumbInfo" xsi:type="array">
                    <item name="title" xsi:type="string">Home</item>
                    <item name="label" xsi:type="string">Home</item>
                    <item name="link" xsi:type="string">/</item>
                </argument>
            </action> 
            <action method="addCrumb">
                <argument name="crumbName" xsi:type="string">Devices</argument>
                <argument name="crumbInfo" xsi:type="array">
                    <item name="title" xsi:type="string">Devices</item>
                    <item name="label" xsi:type="string">Devices</item>
                </argument>
            </action>
            <action method="addCrumb">
                <argument name="crumbName" xsi:type="string">Stig Disposable Pod</argument>
                <argument name="crumbInfo" xsi:type="array">
                    <item name="title" xsi:type="string">Stig Disposable Pod</item> 
                    <item name="label" xsi:type="string">Stig Disposable Pod</item> 
                </argument>
            </action>
        </block>
    </referenceContainer>
</referenceContainer>

Magento 2 :: call static block in phtml page

<?php echo $this->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('block_identifier')->toHtml(); ?>

Magento 2 :: Call phtml page in cms page

The template file would be :
app/design/frontend/Namespace/theme/Magento_Theme/templates/your_folder_name/file_name.phtml

{{block class="Magento\Framework\View\Element\Template" template="Magento_Theme::your_folder_name/file_name.phtml"}}

Magento 2 :: Add custom class in referenceContainer using xml

Added device-hardware class

<referenceContainer name="main.content" htmlTag="main" htmlId="maincontent" htmlClass="page-main device-hardware"/>