Category Archives: PHP

How To Create Magento 2 Store Programmatically

I would like to know if there is any way can create a Magento 2’s store programmatically.

You can call ‘StoreFactory’ class to create Magento 2’s website and use the store resource model to save the store object. Here is the example to create a Magento 2’s store programmatically

$storeFactory = \Magento\Framework\App\ObjectManager::getInstance()
            ->get('Magento\Store\Model\StoreFactory');
$storeResourceModel = \Magento\Framework\App\ObjectManager::getInstance()
            ->get('use Magento\Store\Model\ResourceModel\Store');
$websiteFactory = \Magento\Framework\App\ObjectManager::getInstance()
            ->get('Magento\Store\Model\WebsiteFactory');

$eventManager = \Magento\Framework\App\ObjectManager::getInstance()
            ->get('Magento\Framework\Event\ManagerInterface');


$website = $websiteFactory->create();
$website->load('your_website');
$groupid = 1;

$store= $storeFactory->create();
$store->setCode('new_store');
$store->setName('New Store');
$store->setWebsite($website);
$store->setGroupId($groupid);
$store->setData('is_active','1');
$storeResourceModel->save($store);

$eventManager->dispatch('store_add', ['store' => $store]);

How To Create Magento 2 Website Programmatically

How can I create Magento 2’s website programmatically, like from my custom Magento 2 extension

You can call ‘websiteFactory’ class to create Magento 2’s website and use the website resource model to save the website object. Here is the example to create a Magento 2’s website programmatically

$websiteFactory = \Magento\Framework\App\ObjectManager::getInstance()
            ->get('Magento\Store\Model\WebsiteFactory');
$websiteResourceModel = \Magento\Framework\App\ObjectManager::getInstance()
            ->get('use Magento\Store\Model\ResourceModel\Website');
$website = $websiteFactory->create();

$website->setCode('custom_code');
$website->setName('Custom Name');
$website->setDefaultGroupId(1);
$websiteResourceModel->save($website);

How To Add Magento 2 Products To Category Programmatically

I have created Magento 2 product programmatically, but how can I add or assign new products to exist Magento 2 categories

You will need to get Magento 2 product SKU and category ID first and call assignProductToCategories method from categoryLinkManagement class to assigning the products to exist Magento 2 categories

Here is the example

$sku = "test";
$cids = array(1,2,3);
$link = \Magento\Framework\App\ObjectManager::getInstance()
            ->get('Magento\Catalog\Api\CategoryLinkManagementInterface');
$link->assignProductToCategories($sku,$cids);

How To Change Or Insert WordPress Yoast SEO Fields Programmatically

I have a WordPress site and installed Yoast SEO plugin. I would like to know how can I update or insert Yoast SEO fields when I create a post or page programmatically

Yoast SEO is storing meta title and meta description via post meta. You can use get_post_meta function to retrieve the Yoast meta title or meta description fields and use add_post_meta or update_post_meta to insert or update the Yoast meta title or meta description

Insert Yoast meta title / description

add_post_meta( 1, '_yoast_wpseo_metadesc', "Meta desc");
add_post_meta( 1, '_yoast_wpseo_title', "Meta title");

Update Yoast meta title / description

update_post_meta( 1, '_yoast_wpseo_metadesc', 'Meta desc' );
update_post_meta( 1, '_yoast_wpseo_title', 'Meta title' );

Get Yoast meta title / description

get_post_meta(1, '_yoast_wpseo_metadesc', true);
get_post_meta(1, '_yoast_wpseo_title', true);

How To List All Custom Post Type In WordPress Admin Dashboard

I would like to print a list of all WordPress post type within my WordPress admin dashboard. So I can check it quickly when I am writing the plugin

You can use the get_post_types function to get a list of WordPress post type objects. Meanwhile, you can use “wp_add_dashboard_widget” to add your custom widgets for your WordPress admin dashboard.

Output list of post type for new admin dashboard widget

function show_all_posttype() {
    $args = array(
        'public'   => true,
        '_builtin' => false
    );
  
    $output = 'names';   
    $post_types = get_post_types( $args, $output);
    echo '<ul>';
    foreach ( $post_types as $post_type ) {
        echo '<li>' . $post_type . '</li>';
    }
    echo '</ul>';
}

Add new admin dashboard widget with output function

function posttype_dashboard() {

    if( current_user_can('manage_options') ) {
        wp_add_dashboard_widget(
                     'posttype_dashboard', 
                     'WordPress Post Types', 
                     'show_all_posttype' 
            );  
    }
}
add_action( 'wp_dashboard_setup', 'posttype_dashboard' );

How To Setup Blog In Magento 2

I have a Magento 2 store, I would like to setup a blog for my Magento 2 site. However does not want to use CMS like WordPress

You can search Magento 2 blog extension from Magento 2 marketplace like following

https://marketplace.magento.com/catalogsearch/result/?q=Blog

To install Magento 2 extension

  1. Via FTP client
    • Connect to your server via FTP client
    • Unzip the extension and upload into Magento 2 root directory
    • Run the command to updates, compile and deploy the software
      php bin/magento setup:upgrade
      php bin/magento setup:di:compile
      php bin/magento setup:static-content:deploy
  2. Using Composer
    • If your server support “Composer”, you can run the following command to install the Magento 2 extension
      composer require vendorname/module-name
      php bin/magento setup:upgrade
      php bin/magento setup:di:compile
      php bin/magento setup:static-content:deploy
  3. Magento 2 Installation Service
    If you have an issue with Magento 2 extension installation you can always request Service via Contact Us

Magento 2 Error – Maximum Function Nesting Level Of ‘100’ Reached

After install Magento 2, I got error like Fatal error: Maximum function nesting level of ‘100’ reached, aborting!. How can I fix this Magento 2 error

If you have enabled the PHP extension – xdebug, you will get Fatal error: Maximum function nesting level of ‘100’ reached, aborting! .

To resolve this issue

  1. Disable the xdebug extension (2 ways)
    1. Disable the xdebug via php.ini
      xdebug.remote_autostart=0  
      xdebug.remote_enable=0
      xdebug.profiler_enable=0
    2. Using command to disable the PHP extension
      For php5 (Ubuntu)

      sudo php5dismod xdebug
      sudo service apache2 restart

      For php7 (Ubuntu)

      sudo phpdismod xdebug
      sudo service apache2 restart
  2. Increasing the value of max_nesting_level (2 ways)
    1. Via php.ini
      xdebug.max_nesting_level = 200
    2. In your PHP code
      ini_set('xdebug.max_nesting_level', 200);