Skip to content

Instantly share code, notes, and snippets.

@remcotolsma
Last active March 25, 2016 12:29
Show Gist options
  • Save remcotolsma/45e953bb71cef21fc863 to your computer and use it in GitHub Desktop.
Save remcotolsma/45e953bb71cef21fc863 to your computer and use it in GitHub Desktop.
Magento custom sessions JSONP API.
<?php
/**
* Magento custom sessions API
*
* @see http://fishpig.co.uk/magento/tutorials/run-magento-code-externally/
*/
require_once 'app/Mage.php';
umask( 0 );
Mage::app();
// @see http://magento.stackexchange.com/questions/8814/how-to-get-all-items-in-cart-currently
$cart_items = Mage::getSingleton( 'checkout/cart' )->getItems();
// @see http://stackoverflow.com/questions/416553/current-user-in-magento
$customer = Mage::getSingleton( 'customer/session' );
// JSON
$data = new stdClass();
$data->cart_count = count( $cart_items );
$data->email = $customer->getEmail();
// Response
// @see http://stackoverflow.com/questions/6809053/simple-jquery-php-and-jsonp-example
// @see http://stackoverflow.com/questions/1678214/javascript-how-do-i-create-jsonp
if ( filter_has_var( INPUT_GET, 'callback' ) ) {
header( 'Content-Type: application/json' );
$callback = filter_input( INPUT_GET, 'callback', FILTER_SANITIZE_STRING );
echo $callback, '(', json_encode( $data ), ');';
} else {
echo json_encode( $data );
}
<?php
class Mct_Wordpress_IndexController extends Mage_Core_Controller_Front_Action {
public function indexAction(){
// JSON
$data = new stdClass();
// CART
/** @var Mage_Checkout_Model_Cart $cart */
$cart = Mage::getSingleton('checkout/cart');
$data->cart = new stdClass();
$data->cart->qty = $cart->getSummaryQty();
$data->cart->url = Mage::helper('checkout/cart')->getCartUrl();
$data->cart->price = Mage::helper('checkout')->formatPrice($this->_getCartSubtotal($cart));
// CUSTOMER
/** @var Mage_Customer_Model_Session $session */
$session = Mage::getSingleton('customer/session');
$customer = $session->getCustomer();
$data->customer = new stdClass();
if($session->isLoggedIn()) {
$data->customer->loggedin = $session->isLoggedIn();
$data->customer->id = $customer->getId();
$data->customer->email = $customer->getEmail();
$data->customer->name = $customer->getName();
} else {
$data->customer->loggedin = $session->isLoggedIn();
}
// Response
$this->getResponse()->setHeader('Content-type', 'application/json');
if (filter_has_var(INPUT_GET, 'callback')) {
$callback = filter_input(INPUT_GET, 'callback', FILTER_SANITIZE_STRING);
echo $callback, '(', json_encode($data), ');';
} else {
echo json_encode($data);
}
}
protected function _getCartSubtotal($cart) {
$skipTax = true;
$subtotal = 0;
$totals = $cart->getQuote()->getTotals();
/** @var Mage_Tax_Model_Config $config */
$config = Mage::getSingleton('tax/config');
if (isset($totals['subtotal'])) {
if ($config->displayCartSubtotalBoth()) {
if ($skipTax) {
$subtotal = $totals['subtotal']->getValueExclTax();
} else {
$subtotal = $totals['subtotal']->getValueInclTax();
}
} elseif($config->displayCartSubtotalInclTax()) {
$subtotal = $totals['subtotal']->getValueInclTax();
} else {
$subtotal = $totals['subtotal']->getValue();
if (!$skipTax && isset($totals['tax'])) {
$subtotal+= $totals['tax']->getValue();
}
}
}
return $subtotal;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment