Create Coupon code Programmatically

coupon-code

By Default Magento Gives us the functionality to create a coupon for cart(Cart Rule) and for catalog (Catalog Rule). But What if we want to create a coupon dynamically as per our conditions without going to admin section.

coupon-default

Example: if we want to create a functionality to send a coupon code on some condition to a customer or if we want to send a gift card value as a coupon code to other customers.

There are also many Extensions available which are performing the same custom coupon code part. Here you can check that too.

So, let get Dig Into this.
But Before this we assume that one should a basic knowledge about Module and its base structure, as its a core of our Work. if not then please go to this Link.

Now as we are aware about base Module structure.

Lets Proceed Further

Here we have created a Cart rule Coupon which will be used at checkout page. we are using a helper function whi123ch could be used any where as per our requirement.

Vendor/Module/Helper
In Our Case It at Excellence/Hello/Helper

Above, we have define a function which will create a coupon code as per the given fields every time its gets trigger.
generateCouponCode( $length, $chars = null ) function return a random string with the given length that we pass as a parameter in it.

Note: Here in $coupon[‘discount_type’] we are setting the type of coupon i.e; either it can be Percentage type or fixed type which will be applied on subtotal of cart depending upon our requirement.

By Default we are setting it to active, all rest of the functionality of coupon code will be working as it made for Magento2.

Well, Its all Done As the Coupon Get Created and get saved into Magento 2 Database Table -> Salesrule Coupon with relevant information as we provided in above code.

One can also check in Admin Section Under
Marketing>Cart Price Rule.
Here will see our Dynamic Coupon code that we created via Above Code.

Well Its all Done for now.
Further one can use this according to there Needs.

Add Fields in Customizable option of products in admin

banner-design

There are situations when we want to add an extra field in customizable options of products in admin to get our work done or to perform few more functionalities on products or on product Page/ while placing an order for a particular product.
Even with the help of this, we can set validation on particular products according to our needs.

So, Let Get Dig into this

But before this, we assume that one should have a basic knowledge about the module and its base structure, as its a core of our work. if not then please go to this Link.

Now as we are aware of the base module structure.

Let’s Get Started:

Here we will be adding our custom fields via phtml file, which we can include by injecting it into di.xml file by user preference.

But why we are using preference here, it’s just because we have to add additional fields in Magento core custom options for which preference is the best way to easily allow custom changes in our custom file, other than that we can also use plugins for this but for our work its a relevant way.
And for further clarification, one can prefer this too.

Above file have to be created at Vendor/Module/etc/adminhtml/di.xml
in our case its Excellence/Hello/etc/adminhtml/di.xml.

Now we have to create the file that we defined in the above preference at Excellence\Hello\Ui\DataProvider\Product\Form\Modifier\CustomOptions and write our custom code to add new fields.

In the above code, we have simply extended the core customoption.php file and after that have added our extra field of code in function getSelectTypeGridConfig().

static::FIELD_QTY_NAME => $this->getQtyFieldConfig(55),
static::FIELD_PRODUCT_SKU => $this->getProductSkuFieldConfig(51)

In both functions (getQtyFieldConfig(), getProductSkuFieldConfig()) we have added our code in which we have described our fields validations and names.

As for now, we have created functionality that will create fields in admin but to save their data we need to add their field name in catalog_product_option_type_value database table. To do this we have to update the catalog_product_option_type_value table in our database.

This could be done in two ways either by Creating InstallSchema.php file or by UpgradeSchema.php file at Vendor/Module/Setup.

We are using the UpgradeSchema.php file as we have already Installed module so for this we can simply upgrade our module version and the fields get added into the Database Table.
For further clarification on InstallSchema and UpgradeSchema, one can check on This.

Vendor/Module/Setup
In our case its Excellence/Hello/Setup

For new Instance, we can use InstallSchema.php.

Vendor/Module/Setup
In our case Its Excellence/Hello/Setup

Above we have updated our database using InstallSchema.php file

After That Just Run These Commands:
php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy -f
php bin/magento cache:clean
php bin/magento cache:f
lush

Finally, we get this on admin End:

custom-option

Redirecting Custom URL to Home in Magento using URL Rewrites

Sometimes, you might need to redirect some custom URLs to home page of your Magento store. Let’s say, you used to own a website where there were a number of URLs (products, categories or static pages) and you have recently migrated to Magento, but you don’t have same pages or URLs anymore but you don’t want the users to land on a 404 page when they open a link that you previously shared on social media, etc. Instead, you can make them land on the home page of your store.

Magento has in-built feature of redirecting and rewriting URLs which I guess you are already familiar with. If not, you may refer to this Magento Documentation.

If you need to redirect an URL to home, you just need to set Target Path to ../ and set Redirect Type to Permanent (301). Refer the image below:

How to manage Stock Classes in Magento

In this blog, we will discuss how Stocks work in magento. There are certain setting which we can do from Magento Core files and from System > Configuration.

Firstly we will see what we can do from Magento Admin Panel

Configuring the Inventory Stock Options

  1. From the Admin panel, select System > Configuration.
  2. In the Configuration panel on the left, under Catalog, select Inventory.
  3. Click to expand the Stock Options section. Then, do the following:
    • If you want to show the OutOfStock products on the Frontend then set Yes Includes “out of stock” products in the list else set No to “out of stock” products from the lists.
    • If you want to display of the stock availability message on the product detail page, set Display products availability in stock in the frontend to Yes else set No
  4. Click to expand the Product Stock Options section. Then, do the following:
    • If you want the inventory options are available for this single product to manage your inventory then goto Manage Stock and set Yes else set No to Manage Stock
    • If you want that customer can order a product even if its quantity is below 0 then goto Backorders and from there we can set Allow Quantity Below 0 or we can set Allow Quantity Below 0 and Notify Customer or if we dont want the customer to order a praticular product if its quantity is below 0 then we can set No BackOrders
  5. Click the Save Config button to save the setting.

Secondly,we can also work with Magento Stock Inventory Core files.

For that, Magento has a specific module for handling the Stock, named as ‘ StockInventory ‘. Basically how it works, whenever we add a product to cart it goes to checkQuoteItemQty() function of Mage_CatalogInventory_Model_Observer Observer. This function checks whether the requested product is in Stock or not. If the quantity of that particular product is available then it goes to checkQuoteItemQty() function of Mage_CatalogInventory_Model_Stock_Item and again it reverifies for product availability and if it is available then it added that product to cart else it throws an error message showing ‘ This product is currently out of stock ‘.
So, from there you can also add the product directly to the cart without checking the product availability by modifying both the files. Most of the Stock related Operations are managed from these two files.

Next we have a Resource Model class named Mage_CatalogInventory_Model_Resource_Indexer_Stock_Default for simple, virtual and downloadable products, Mage_CatalogInventory_Model_Resource_Indexer_Stock_Configurable for configurable products and Mage_CatalogInventory_Model_Resource_Indexer_Stock_Grouped for Grouped Products which manages the Stock of Magento Products. These classes used for reindexing of the Stock Inventory.
Basically what these Resource Model files does is that it changes the Stock_Status whenever admin Reindex the Product Prices or Stock Status according to the Quantity Availability.
Whenever we place an order in Magento, the quantity of product reduces, and then again product that is ordered is reindexed. This is done by _updateIndex() function of the Mage_CatalogInventory_Model_Resource_Indexer_Stock_Default class which checks the quantity availability and changes the Stock_Status accordingly.

Admin Password Reset From Database

In this blog, we will see how we can edit/reset our lost magento admin password in few simple steps.

Step 1:

First of all open your phpmyadmin database of your website. it should look like this

phpmyadmin_db

Step 2:

admin_table

Then select the admin_user table from it . This table contains your admin account details as follows

Step 3:

Select the admin user record for which you want to change the login password and click to edit the record. This leads u to the edit window of the phpmyadmin record

edit_window

Step 4:

Now, as the password stored  in the magento are MD5 encrypted.So, first we need to encryt our password using MD5 encryption. For this we require two things

1) New Password -Any Password that you want to use.

2)Salt – any 2 letters word to make your password more secure.

I will choose “pass” as my New Password and “xy” as my Salt. Then use any of the MD5 generator site to generate the MD5 of the string “xypass”. In my case MD5 is “1028ff89cc8dae9372f21a3dd889b8c8”.

Now, In the password column, put in the md5:salt i.e in my case “1028ff89cc8dae9372f21a3dd889b8c8:xy” and then click “Go” to  save the changes.

This will change your admin password. Now you will be able to login with new password.

Always remember that you will use password alone as login password not password with the salt. In my case i will use “pass” as password not “xypass”.

Or simply go to the SQL menu of the admin_user table of your phpmyadmin database and execute the following query,

UPDATE admin_user SET password=CONCAT(MD5('YOUR_SALT+YOUR_PASSWORD'), ':YOUR_SALT') WHERE username='YOUR_USERNAME';