Magento Forum

Get contents of CART Externally (check this out)
 
zep007
Sr. Member
 
Avatar
Total Posts:  131
Joined:  2008-05-15
Connecticut
 

I need to get the contents of the shopping cart on a php page that I will calculate installation services on.

when a customer adds a product to their cart, they will see the ‘add installation” icon, which will bring them to a page I have in my custom directory, where I will be taking the contents of the cart, moving through the array, and quoting installation for each item.

I think the best way to do this is to have the external page because installation is a very complicated item (minimum charges for trip, install 1 item @ same price as 3, install combinations of products with different prices, and the ability to give price breaks, etc.)

I need to pull the variables that are used in the cart page to do this.

I started with

<?
require_once '/opt/lsws/wwwroot/mage1/app/Mage.php';                                                        
Mage::app('default');
Mage::getSingleton('checkout/cart'); //get items currently in this cart
?>

but I cant pull the variables I see in sidebar.phtml of cart.phtml out.  How do I go about getting this data?

 
Magento Community Magento Community
Magento Community
Magento Community
 
nikefido
Guru
 
Avatar
Total Posts:  481
Joined:  2008-07-11
New Haven, CT
 

I can’t give you an exact answer, but I CAN show you another way of doing the same thing that might help you out a lot:

<?php
require_once 'app/Mage.php';
umask(0);
//not Mage::run();
Mage::app('default');

//code snipped
$className Mage::getConfig()
            ->
getBlockClassName('catalog/product_ajax');
$block = new $className();
$className Mage::getConfig()
            ->
getBlockClassName('core/template');
$toolbar = new $className();
$block->setChild('toolbar'$toolbar);

//choose whatever category ID you want
$block->setCategoryId(3);
$block->setTemplate('catalog/product/ajaxevents.phtml');

echo 
$block->renderView();
?>

This is basically running a module (block) from Magento outside of Magento -

‘catalog/product_ajax’ relates to the block PHP file I made - this particular one grabs all the products from a chosen category

‘catalog/product/ajaxtemplate.phtml’ is the .phtml template file that will do all the HTML output

echo $block->renderView() will obviously tell it to output the HTML output from the template file

In your case, you don’t want to pull products, but cart information, so your code will be different - but this will show you how to get a block to output outside of Magento :D

===================================
I’m not up to par on the cart methods, but you can’t do stuff like:

$items Mage::getSingleton('checkout/cart')->getItems();  //can't do that successfully??
//check out functions in this file:  app\code\core\Mage\Checkout\Block\Cart.php
//app\code\core\Mage\Checkout\Block\Cart\Abstract.php
^^  has some functionality to grab a session and check if logged inetc!

I should mention, getting any cart data outside of Magento could possibly be a conflict with any session data (or however Magento keeps track of cart items per logged in user) - see my comments in above code

 
Magento Community Magento Community
Magento Community
Magento Community
 
zep007
Sr. Member
 
Avatar
Total Posts:  131
Joined:  2008-05-15
Connecticut
 

nikefido,

Thank you fro your reply, I truly appreciate the help, I will be working on this soon.  The following code, from my understanding, gets the current contents of the cart for the specific visitor browsing the site :

Mage::getSingleton(’checkout/cart’)->getItems();

I have started o another mod for now, but will post back when I come back to this one.

Thanks!

 
Magento Community Magento Community
Magento Community
Magento Community
 
nikefido
Guru
 
Avatar
Total Posts:  481
Joined:  2008-07-11
New Haven, CT
 

Good to hear -

I was actually having a problem - I tried to use an external .php page using my code snippet above - (to be clear, that’s a code being run in the root folder, which hooks into Magento via
“require_once ‘app/Mage.php’;
umask(0);
//not Mage::run();
Mage::app(’default’)” )

Anyway - I tried to get the Sidebar block of the cart (app/code/core/mage/checkout/block/cart/sidebar.php and it’s related .phtml file) - But I always got the message that there were no items in the cart. Apparently it wasn’t reading a customer/checkout session :( (or something like that...)…

Anyway, I’m not sure if that’s a related problem (if you’re doing code “outside” of magento or not??) but I thought I’d mention it.

 
Magento Community Magento Community
Magento Community
Magento Community
 
zep007
Sr. Member
 
Avatar
Total Posts:  131
Joined:  2008-05-15
Connecticut
 

I was actually trying to slap my own php page in a folder to be accessed by http://www.mystore.com/newfolder/XXX.php but found an interesting workaround for the problem, I modified some code in the bundled product section to accommodate my needs.

Thanks for the heads up!

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top