5

I would like to get the cart Block outside of Magento. Here is my Code.

<?php 
    require_once ( $_SERVER['DOCUMENT_ROOT']."/app/Mage.php" );
    umask(0);
    Mage::app('base','website');

    echo Mage::app()->getLocale()->getLocaleCode();

    //Solution
    Mage::getSingleton('core/translate')->setLocale('de_DE')->init('frontend', true);        

    Mage::getSingleton('core/session', array('name'=>'frontend'));

    $block = Mage::getSingleton('core/layout')
            ->createBlock("checkout/cart_sidebar", "sidebar")
            ->setTemplate("checkout/cart/sidebar.phtml");
    echo $block->toHtml(); 

?>

I have just the Problem that the output ist just english and translation doesn't work.

Thanks for Help

hans
  • 61
  • 4
  • Well done for finding the solution yourself. To help others who may, in future, try to read this page please put the solution as a self answer then mark it as correct. That will put a big checkmark next to it so people can clearly see. – clockworkgeek Dec 14 '11 at 15:24

3 Answers3

0

Through this you can get all the cart details out side magneto. Now you can give any desired template to these elements.

umask(0);
Mage::app('default');

// This has to run to authenticate customer and checkout session calls.
Mage::getSingleton('core/session', array('name' => 'frontend'));

// Get any customer model you desire.
$oSession = Mage::getSingleton( 'customer/session' );
$oCustomer = $oSession->getCustomer();
$oCheckout = Mage::getSingleton( 'checkout/session' );
$oQuote = $oCheckout->getQuote();

var_dump( $oCustomer );
var_dump( $oSession );
var_dump( $oQuote );
var_dump( $oCheckout );

$oCart = $oQuote->getAllItems();
if( !empty( $oCart ) )
{
    foreach ( $oCart as $oItem ) 
    {
        $sName  = $oItem->getProduct()->getName();
        $fPrice = $oItem->getProduct()->getPrice();
        var_dump( $sName );
        var_dump( $fPrice );
    }
}

?>

Ashwin Shahi
  • 339
  • 2
  • 14
0

namespace moduleName\addtobasket\Controller\Product;

class Index extends \Magento\Framework\App\Action\Action {

/**
 * @var \Magento\Checkout\Model\Cart
 */
protected $cart;
/**
 * @var \Magento\Catalog\Model\Product
 */
protected $product;

public function __construct(
    \Magento\Framework\App\Action\Context $context,
    \Magento\Framework\View\Result\PageFactory $resultPageFactory,
    \Magento\Catalog\Model\Product $product,
    \Magento\Checkout\Model\Cart $cart
) {
    $this->resultPageFactory = $resultPageFactory;
    $this->cart = $cart;
    $this->product = $product;
    parent::__construct($context);
}
public function execute()
{
    try {
        $params = array();
        $params['qty'] = '1';//product quantity
        /* Get product id from a URL like /addtobasket/product?id=1,2,3 */
        $pIds = explode(',',$_GET['id']);
        foreach($pIds as $value) {
            $_product = $this->product->load($value);
            if ($_product) {
                $this->cart->addProduct($_product, $params);
                $this->cart->save();
            }
        }

        $this->messageManager->addSuccess(__('Add to cart successfully.'));
    } catch (\Magento\Framework\Exception\LocalizedException $e) {
        $this->messageManager->addException(
            $e,
            __('%1', $e->getMessage())
        );
    } catch (\Exception $e) {
        $this->messageManager->addException($e, __('error.'));
    }
    /*cart page*/
    $this->getResponse()->setRedirect('/checkout/cart/index');
}

}

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 31 '22 at 17:40
0

Any reason why you don't just specify a store code that has the german locale set in your call to Mage::app()?

Unrelated to your problem, but you may also be interested in a more solid approach of loading a block into another website.

Community
  • 1
  • 1
Daniel Sloof
  • 12,568
  • 14
  • 72
  • 106