Monday, July 10, 2017

Adding a Simple product to Cart in Magento externally

In this article, we'll see how we can insert a simple product to Magento Cart from an external PHP script.

We are testing this on Magento 1.9.2.2 and we have a simple product with ID 375 which we'll push to Cart. 

Let's see the code below. First, we are showing the existing Cart, then we'll insert a simple product ID 375, and finally we'll print the new Cart.

<?php
// Load Magento 
require_once("app/Mage.php");
umask(0);
Mage::app();

/// We Build a String for printing
$str = "";
$str .= "1. Showing existing Cart<br>===============<br>";

/// This line connects to the FrontEnd session
Mage::getSingleton('core/session', array('name' => 'frontend'));  

// Get customer session
$session = Mage::getSingleton('customer/session'); 

// Get cart instance
$cart = Mage::getSingleton('checkout/cart'); 
$cart->init();

// Get CART Count
$cartItemsCount = $cart->getItemsCount();
$str .= "Count >> $cartItemsCount <br>";

// GET CART Items
$quote = $cart->getQuote();
$cartItems = $quote->getAllVisibleItems();

// Loop Through ITEMs
foreach ($cartItems as $item) 
{
    $productId = $item->getProductId();
    $product = Mage::getModel('catalog/product')->load($productId);
$str .= "Id: $productId, Price: " . $item->getPrice() 
                . ", Qty:" . $item->getQty() . "<br>";
    // Do something more
}

// Build String
$str .= "<br><br>2. Adding a New Product<br>===============<br>";

// SET Product ID
$productId = 375;

// Build Product Params
$productInstance = Mage::getModel('catalog/product')->load($productId);
$param = array(
'product' => $productInstance->getId(),
'qty' => 1
);

// Finally ADD the product to CART
$cart->addProduct($productInstance, $param);            
// Save The Cart
$cart->save(); 

// Update Session
$session->setCartWasUpdated(true);

// Building the String
$str .= "<br><br>3. Showing the new Cart<br>===============<br>";

// Get cart instance
$cart = Mage::getSingleton('checkout/cart'); 
$cart->init();

// Get CART Count
$cartItemsCount = $cart->getItemsCount();
$str .= "Count >> $cartItemsCount <br>";

// GET CART Items
$quote = $cart->getQuote();
$cartItems = $quote->getAllVisibleItems();

// Loop Through ITEMs
foreach ($cartItems as $item) 
{
    $productId = $item->getProductId();
    $product = Mage::getModel('catalog/product')->load($productId);
    $str .= "Id: $productId, Price: " . $item->getPrice() 
            . ", Qty:" . $item->getQty() . "<br>";
    // Do something more
}

// Print the String
echo $str;
?>

The above code is quite self explanatory. We tried to connect to frontend customer's session and get his Cart details. If customer is not logged in, then it does not create any problem.

See some screenshot here : 

Step 1. Existing Cart


Step 2. Our Script Runs



Step 3. Final Cart




In this example, we added a simple product. To add a configurable product to Cart, we need to do some extra work which I'll show in next article.

Hope this helps.

Saturday, June 24, 2017

Running Laravel on Windows

Here I'll discuss about how we can install and run Laravel on Windows environment and start development in Laravel. 

First step is to download the Laravel package from https://github.com/laravel/laravel and Unzip it in a folder, say C:\xampp2\htdocs\laravel. Notice that my Xampp is installed in c:\xampp2.

Now we have two ways we can start the Laravel project running.

First Method :: From Command Line

A. First, create a DB through phpmyadmin, the name I gave is 'homestead'

B. Secondly, rename the file .env.example to .env file which holds all the basic configuration for running Laravel. It contains the DB configuration, application URL. We need to make changes accordingly. 
  
See that APP_KEY= is still left blank. We'll generate a key for our application shortly.

C. In this situation, if we try yo run Laravel server by issuing command "php artisan serve", it will show some "file not found" error as some dependencies are still unavailable.  



  
  We need to install them by issuing "composer install". 
  

  
D. After the command "composer install" ran, we can see that a new folder called "vendor" is created where various libraries are installed. 

E. Now, see what happens if we try to run the server by issuing "php artisan serve

    The server is run. Below message is shown in the command line .. 
   
    Laravel Development Server started: <http://127.0.0.1:8000>
   
   and if we hit the URL http://127.0.0.1:8000 in browser, see what happens
   
  


   
So, we need to follow some more steps as shown below.

F. Press ctrl + c to stop the server. Run command "php artisan key:generate" to create new Security Key.

   If the command line console shows any message like "Dotenv values containing spaces must be surrounded by quotes.", it means any settings provided in .env file need to be wrapped by a quote (") or (').
   
   

   
   See, it is reported that an Application key has been generated. The environment configuration file .env is updated also with the new key. In my case, it is as shown below :
   
 APP_KEY=base64:6Ed0zZN/3u+Q58OyZn6JnTvXjO7/vxM0Jdvder18QU8=
   
G. Now issue "php artisan serve" to start your server. Visit the webpage at http://127.0.0.1:8000.

Second Method :: The other way

A. The steps till Security Key Generation shown above are still required here. But we don't need to issue the command 'php artisan serve' here ...

Start your Xampp. In my case, I have a configuration where port 80 is listened by IIS, 8082 is listened by Xampp Apache. 

I have Xampp 3.2.2 installed on my Windows 8. In my case, Xampp is installed in c:\xampp2 folder.

Here, I'll be assigned the Laravel to a different port 8060.  

So, I just create a Virtual Host in file c:\xampp2\apache\conf\extra\httpd-vhosts.conf file as shown below.

<VirtualHost *:8060>
    DocumentRoot "C:\xampp2\htdocs\laravel\public"
    ServerName laravel.dev
</VirtualHost>

So, my Xampp will listen/serve to 8060 port as well.

If I change this 8060 to default 8082, my Xampp Home page (http://127.0.0.1:8082) will be replaced by Laravel, which I don't want.

B. Next, start the Xampp server. See below, the port numbers apache is serving now.




C. Hit "http://127.0.0.1:8060/" in the browser URL to see Laravel running.

D. You can also modify c:\windows\system32\drivers\etc\hosts file to have entry like this :
   
  127.0.0.1:8060  laravel.dev
   
   So, if you enter 'laravel.dev' in address bar of the browser, it will point to 127.0.0.1:8060.

The 'public' directory inside the "Laravel" installation contains the index.php file, which is the entry point for all requests to our  application. This directory also holds all JavaScript, images and CSS.

I hope this helps.