Jump to content

How to convert a cart into an order in a standalone script


musicmaster

Recommended Posts

I found on the forum one item on how to convert a cart into an order. It runs as follows:

$cart_id = Tools::getvalue('cart_id');
$cart = new Cart((int)$cart_id);
$order_status = (int)Configuration::get('PS_OS_PAYMENT');
$order_total = $cart->getOrderTotal(true, Cart::BOTH);
$this->module->validateOrder($cart->id, $order_status, $order_total, "Bankwire module", null, array(), null, false, $cart->secure_key);

 

But this obviously misses the context to run it standalone. Requiring config.inc.php and init.php is not enough. The "$this" gives an error because I am not in object context. But when replace the last line with "PaymentModule:validateOrder(" I still get an error about "$this": now one inside validateOrder(). 

So how can this be run standalone?

Link to comment
Share on other sites

6 hours ago, ps8moduly.cz said:

Hi.

./classes/PaymentModule.php

PaymentModule::validateOrder(.....

I know that one. But that still gives me the message that I cannot use "$this" outside an object context. In this case it concerns a "$this" somewhere in the validateOrder() function.

Link to comment
Share on other sites

It's hard to advise when you can't see the whole example !!!

The part you put here is useless.

class myCustomCartClass 
{
    protected $module = false;
	protected $namef_class;
    private $context;

    public function __construct(Module $module = null)
    {
        $this->_namef_class = get_class($this);
        $this->module = $module;
        $this->context = Context::getContext();
    }

    public static function cart() {
        $this->addToCart();
    }
 
    public function addToCart() {
        $cart_id = Tools::getvalue('cart_id');
        $cart = new Cart((int)$cart_id);
        $order_status = (int)Configuration::get('PS_OS_PAYMENT');
        $order_total = $cart->getOrderTotal(true, Cart::BOTH);
        $this->module->validateOrder($cart->id, $order_status, $order_total, "Bankwire module", null, array(), null, false, $cart->secure_key);    
    }
}
 
/* IN YOUR MODULE
require_once(dirname(__FILE__).'/classes/myCustomCartClass.php');
   
$foo = new myCustomCartClass($this);
$foo->cart();
*/

 

Edited by ps8moduly.cz (see edit history)
Link to comment
Share on other sites

I tried to get this old code working as standalone by putting some includes before it. So - omitting validity checks - I get something like:

<?php 
require_once('/config/config.inc.php');
include('/init.php');

$cart_id=$_GET['id_cart']; 

$cart = new Cart((int)$cart_id);
$order_status = (int)Configuration::get('PS_OS_PAYMENT');
$order_total = $cart->getOrderTotal(true, Cart::BOTH);
$this->module->validateOrder($cart->id, $order_status, $order_total, "Bankwire module", null, array(), null, false, $cart->secure_key);

That doesn't work. So my question is: how could I get that to work?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...