Back to Top

How To Add Category In Opencart Using CasperJS

Updated 8 June 2017

In Opencart while adding product there is a field of the category. If the category is not selected then the product will not show up under any categories in the front end of the store. We can add the category in our Opencart store using CasperJS. In this blog, we will learn how to admin login in our Opencart store and after that, we will add the category.

Firstly, we will create our script for admin login process and then, we will add the category in our Opencart store.

For admin login and adding category in our Opencart store we have used fill(), casper.cli.get(), getTitle(), then(), getCurrentUrl() and click() methods.

Following script is used to automate the admin login functionality:

var casper = require('casper').create();
 
var url = casper.cli.get(0);
 
casper.start(url, function() {
	this.echo('Login page Title:',"INFO"),
    this.echo(this.getTitle());
});
 
casper.then(function(){
	this.fill('form', { 
	    username: 'admin', 
	    password: 'admin'
	}, true);
	this.echo('Login Completed',"INFO");
});

casper.wait(1000, function() {
	this.echo('Category page Title:',"INFO"),
    this.echo(this.getTitle());
});

In the above script, casper.cli.get() is used to pass parameter while running the script and fill() is used to fill the data on the form.

Start your headless eCommerce
now.
Find out More

After successful login, now we will add data about the category. Following script is used to add the details of the category:

casper.then(function(){
	this.fill('form#form-category', {
	    'category_description[1][name]' : 'Demo',
        'category_description[1][description]' : 'Lorem Ipsum is simply dummy text of the printing and typesetting industry.',
	    'category_description[1][meta_title]' : 'Demo',
        'category_description[1][meta_description]' : 'Demo',
        'category_description[1][meta_keyword]' : 'Demo',
        'keyword' : 'demo-category',
        'sort_order' : '2',
        'status': '1'
	}, false);
   
});
 
casper.then(function(){
  this.click(".container-fluid button.btn.btn-primary");
});

casper.wait(1000, function() {
    this.echo(this.getCurrentUrl());
});

casper.then(function(){
    	if (casper.exists('#content > div.container-fluid > div.alert.alert-success')){
		this.echo(this.fetchText('#content > div.container-fluid > div.alert.alert-success'),"INFO");
	}
});

In the above script, all details of the category are filled and then clicking on save button will save the category. When the category is saved we are fetching current URL and successful message of category added.

Here is the full script of login and add category process.

/**
* Webkul Software.
*
* @category Webkul
* @package Webkul_CasperJS
* @author Webkul
* @copyright Copyright (c) 2010-2017 Webkul Software Private Limited (https://webkul.com)
* @license https://store.webkul.com/license.html
*/
var casper = require('casper').create();
 
var url = casper.cli.get(0);
 
casper.start(url, function() {
	this.echo('Login page Title:',"INFO"),
    this.echo(this.getTitle());
});
 
casper.then(function(){
	this.fill('form', { 
	    username: 'admin', 
	    password: 'admin'
	}, true);
	this.echo('Login Completed',"INFO");
});

casper.wait(1000, function() {
	this.echo('Category page Title:',"INFO"),
    this.echo(this.getTitle());
});

casper.then(function(){
	this.fill('form#form-category', {
	    'category_description[1][name]' : 'Demo',
        'category_description[1][description]' : 'Lorem Ipsum is simply dummy text of the printing and typesetting industry.',
	    'category_description[1][meta_title]' : 'Demo',
        'category_description[1][meta_description]' : 'Demo',
        'category_description[1][meta_keyword]' : 'Demo',
        'keyword' : 'demo-category',
        'sort_order' : '2',
        'status': '1'
	}, false);
   
});
 
casper.then(function(){
  this.click(".container-fluid button.btn.btn-primary");
});

casper.wait(1000, function() {
    this.echo(this.getCurrentUrl());
});

casper.then(function(){
    	if (casper.exists('#content > div.container-fluid > div.alert.alert-success')){
		this.echo(this.fetchText('#content > div.container-fluid > div.alert.alert-success'),"INFO");
	}
});
 
casper.run();

After preparing the script, you have to execute the command in terminal. The command for execution is:

casperjs Category.js

Here, Category.js is the Java Script file in which we have written script for login and add a category in Opencart store process.

casperjs

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home