magento - access cart in external PHP file - magento

Trying to access the magento cart from an external php file, I have loaded Mage and am able to access products & categories but for some reason I'm unable to access the cart information.
Shop is located in www.domain.com/shop/
PHP File is located in www.domain.com/file.php
Magento cookie setting is set to '/'
I have looked at and tried many example of how to get the information and none of them have worked, the code I have at the moment is:
<?php
require_once '/home/admin/public_html/shop/appMage.php';
Mage::app();
Mage::getSingleton('checkout/cart', array('name' => 'frontend'));
$cartItemsCount = Mage::getSingleton('checkout/cart')->getItemsCount();
$cartTotal = Mage::helper('checkout')->formatPrice(Mage::getSingleton('checkout/cart')->getQuote()->getGrandTotal());
echo 'You have '. $cartItemsCount . ' item(s) in your cart. <a class="cartgo" href="'.Mage::helper('checkout/cart')->getCartUrl().'">Checkout</a>';
if($cartTotal > 0){ echo '<span>[£'.$cartTotal.']</span>'; }
echo '</a>';
?>
It works perfectly fine within the magento site but not from this external file for some reason. It returns a 0 even though there is a product in the cart.
Any pointers?

Try
// Mage init
require_once '../../app/Mage.php';
umask(0);
Mage::init('default');
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();
$cartItemsCount = $cart->getItemsCount();
see magento 1.8 add product to cart using php

Related

add product to wishlist in magento API

I have checked in magento soap API but I can not find API for add product to wish list so I am developing own API but I don't know what is the problem with my code so please help me to find solution.
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
require_once '../app/Mage.php';
Mage::app();
$customer_id = $_GET['customer_id'];
$product_id = $_GET['product_id'];
$customer = Mage::getModel('customer/customer');
$wishlist = Mage::getModel('wishlist/wishlist');
$product = Mage::getModel('catalog/product');
$customer->load($customer_id);
$wishlist->loadByCustomer($customer_id);
$res = $wishlist->addNewItem($product->load($product_id));
if($res)
{
$status =1;
$message = "your product has been added in wishlist";
}
else
{
$status =0;
$message = "wrong data send";
}
$result = array("status" =>$status,"message"=>$message);
header('Content-type: application/json; charset=utf-8');
echo json_encode($result);
?>
There is a similar question you might want to check.
In your code you are not extending Magento API in any way but creating a standalone hackish script that inserts items to wishlist. I suggest that you take a look on how to develop a magento extension or if you need to create a backend (CLI) script, at least use the same structure as in shell/abstract.php and extend the shell base class - you will save yourself loads of headaches.
Moreover, your code is not secure, maintainable and removes all the benefits of using Magento as an ecommerce platform/framework (security, authorization & authentication etc)
Inchoo have quite some blog posts that could give you an idea where to begin
http://inchoo.net/magento/magento-api-v2/
http://inchoo.net/magento/extending-the-magento-api/
/* error_reporting(E_ALL);
ini_set("display_errors", 1);*/
You Have Comment This line And Write this
require_once './app/Mage.php';

Magento 1.9 - resave all products

I need to re-save all products in magento. I found solution (for 1.7 version):
<?php
set_time_limit(0);
// require magento core
require_once 'app/Mage.php';
// execute on admin store
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$products = Mage::getModel('catalog/product')->getCollection();
foreach($products as $product) {
echo $product->getName() . '<br/>';
// save the product
$product->save();
}
echo 'DONE';
?>
But this not working for magento 1.9.
I need to re-save all configurable products, if it possible.
It works, but you cannot see product name, try use sku field

Why are products in the category with Mage::app('default') but not with Mage::app('admin')?

I wrote an extension and in this extension I need the count of active products of a category.
I used this snipped to determine active products by category:
$return = Mage::getModel('catalog/category')
->load($category->getId())
->getProductCollection()
->addAttributeToSelect('entity_id')
->addAttributeToFilter('status', Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
->addAttributeToFilter('visibility', Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
->count();
If I call the function to get the count of active products in my test.php
header('Content-Type: text/html; charset=utf-8');
require_once "app/Mage.php";
Mage::reset();
Mage::app('default');
var_dump(myDummyFunction($categoryObject));
I get 3 as the result. If I use the same function within the same magento in the extension (which is used in the admin area) the result is 0.
Has somebody an idea why this happens and how to fix this?
Btw: If I change the Mage::app('default'); in the test.php to Mage::app('admin'); it also displays 0...
Use this
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
Just Before this
Mage::app('default');

Bring cart count value from Magento cart to ExpressionEngine

I have a website structured as:
www.myEEsite.com/myMAGENTOstore
Where myEEsite is the root of the site and myMAGENTOstore is the Magento site. I'd like them to both look the same site, but I was wondering if there was a way to bring over the cart count value from Magento and display it in the header template of my ExpressionEngine site.
Take a look # Magento cart / session data outside magento
umask(0);
require_once 'app/Mage.php';
Mage::app();
Mage::getSingleton('core/session', array('name'=>'frontend'));
$ItemsCount = Mage::getSingleton('checkout/cart')->getItemsCount();
var_dump(array(
"ItemsCount" =>
$ItemsCount
));
Thanks for the help. Although that didn't work, I was able to successfully achieve with the following code...just changed the path of Mage.php according to my setup:
require_once 'app/Mage.php';
umask(0);
$app = Mage::app('default');
$app->getTranslator()->init('frontend');
if( ($_COOKIE["frontend"]=="") || ($_COOKIE["frontend"]=="undefined") || ($_COOKIE["frontend"]==null) )
{
$session = Mage::getSingleton('customer/session');
session_name('frontend');}
else
{session_name('frontend');}
Mage::getSingleton('core/session', array('name' => 'frontend'));
$className = Mage::getConfig()->getBlockClassName('checkout/cart/sidebar');
$block = new $className();
$block->setTemplate('checkout/cart/sidebar.phtml');
$cart = Mage::helper('checkout/cart')->getCart()->getItemsCount();
$carturl = Mage::helper('checkout/url')->getCartUrl();
echo $block->renderView();
echo '<br />cart items count: ' . $cart."<br />";
echo "cart Summary Qty: ".Mage::helper('checkout/cart')->getCart()->getSummaryQty()."<br />";
$cart_count = Mage::helper('checkout/cart')->getCart()->getSummaryQty();

Magento 1.5.1 getting product images using php

This code gets contents(images) in a magento store. It is able to fetch images for magento 1.4x - 1.5
I tried it in 1.5.1 and it seems it cannot fetch the images. Is it located in "media/catalog/product" ?
Any help in getting the location of magento 1.5.1 images? Thanks.
<?php
include_once 'app/Mage.php';
umask(0);
Mage::app();
$products = Mage::getModel('catalog/product')->getCollection();
$products->addAttributeToSelect('*');
$products->load();
$baseUrl = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
$myImage = $baseUrl ."media/catalog/product". $product['image'];
?>
<?php
include_once 'app/Mage.php';
umask(0);
Mage::app();
$products = Mage::getModel('catalog/product')->getCollection();
$products->addAttributeToSelect('thumbnail');
$products->load();
$product = $products->getData('thumbnail');
$baseUrl = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
$myImage = $baseUrl ."media/catalog/product". $product[2]['thumbnail'];
print $myImage;
?>
Obviously you'll want to loop over the collection or specify a product ID in your collection for a specific product, I just used an index of [2] for show.
Also no need to load the entire product attributes collection with * but rather the specific attribute your needing to conserve memory space.

Resources