News:

You may pay someone to create your store, or you visit our seminar and become a professional yourself with the silver certification

Main Menu

how to create $order like structure from $cart variable

Started by nugirt, October 12, 2016, 17:04:35 PM

Previous topic - Next topic

nugirt

Hi, I am new in VM and I would really appreciate your help. I am I trying create $order like structure from $cart data.
I need this structure like you get in plgVmOnConfirmOrder($cart, $order):
$order = [
   'details' => [..],
   'items' => [
       prod1, prod2,...
    ],
   'calc_rules' => [..]
];
I am creating custom payment plugin and i need this structure before an order is created. I don't want to create vm order in part of code where I need this.
I was looking how VM does this in administrator/componets/com_virtuemart/models/orders.php in createOrderFromCart function but without any luck.
My products are not calculated good and I can't create good calc_rules list, like the ones in the $order variable.

I'm working on Joomla 3 and VM 3. Thanks.

K&K media production

maybe with this trigger


/**
* This event is fired after the payment method has been selected. It can be used to store
* additional payment info in the cart.
*
* @author Max Milbers
* @author Valérie isaksen
*
* @param VirtueMartCart $cart: the actual cart
* @return null if the payment was not selected, true if the data is valid, error message if the data is not vlaid
*
*/
public function plgVmOnSelectCheckPayment (VirtueMartCart $cart, &$msg) {

return $this->OnSelectCheck ($cart);
}


or this


/*
* plgVmonSelectedCalculatePricePayment
* Calculate the price (value, tax_id) of the selected method
* It is called by the calculator
* This function does NOT to be reimplemented. If not reimplemented, then the default values from this function are taken.
* @author Valerie Isaksen
* @cart: VirtueMartCart the current cart
* @cart_prices: array the new cart prices
* @return null if the method was not selected, false if the shiiping rate is not valid any more, true otherwise
*
*
*/

public function plgVmonSelectedCalculatePricePayment (VirtueMartCart $cart, array &$cart_prices, &$cart_prices_name) {

return $this->onSelectedCalculatePrice ($cart, $cart_prices, $cart_prices_name);
}


take a look through the existing payment plugins

nugirt

Thank you for fast answer. :)

I looked into this, but I think that none of them will do what I need. I looked more into orders.php file and solved the problem.
Solution at the end is to copy logic and modify it.

here is my code for someone who has similar problem.. This way I have similar output like the one from $orderModel->getMyOrderDetails($this->virtuemart_order_id,$this->order_number,$this->order_pass);

If you have some suggestions for improving this that would be great.  ;)



/**
* Format cart data to be similar to order->orderDetails values
* */
private function _formatCartData($cart)
{
$formatedData = array();

$formatedData['detailes']['BT'] = $this->_formatBTAddress($cart);

$products = $this->_formatLineItems($cart);
$formatedData['items'] = $products['items'];
$formatedData['calc_rules'] = $this->_formatCalcRules($cart, $products['calcRulesProductIds']);

return $formatedData;
}

private function _formatBTAddress($cart)
{
$address = new stdClass();

foreach($cart['BT'] as $key=>$val)
{
$address->$key = $val;
}

return $address;
}

private function _formatLineItems($cart)
{
$line_items = array();
$product_calc_rules = array();

$calculation_kinds = array('DBTax','Tax','VatTax','DATax','Marge');

foreach ($cart->products  as $priceKey=>$product) {

$_orderItems = new stdClass();

$_orderItems->product_attribute = vmJsApi::safe_json_encode($product->customProductData);

// - this is ugly hack for adjustments for helper functions
$_orderItems->virtuemart_order_item_id = $product->virtuemart_product_id;

$_orderItems->virtuemart_vendor_id = $product->virtuemart_vendor_id;
$_orderItems->virtuemart_product_id = $product->virtuemart_product_id;
$_orderItems->order_item_sku = $product->product_sku;
$_orderItems->order_item_name = $product->product_name;
$_orderItems->product_quantity = $product->quantity;
$_orderItems->product_item_price = $product->allPrices[$product->selectedPrice]['basePriceVariant'];
$_orderItems->product_basePriceWithTax = $product->allPrices[$product->selectedPrice]['basePriceWithTax'];

//$_orderItems->product_tax = $_cart->pricesUnformatted[$priceKey]['subtotal_tax_amount'];
$_orderItems->product_tax = $product->allPrices[$product->selectedPrice]['taxAmount'];
$_orderItems->product_final_price = $product->allPrices[$product->selectedPrice]['salesPrice'];
$_orderItems->product_subtotal_discount = $product->allPrices[$product->selectedPrice]['subtotal_discount'];
$_orderItems->product_subtotal_with_tax =  $product->allPrices[$product->selectedPrice]['subtotal_with_tax'];
$_orderItems->product_priceWithoutTax = $product->allPrices[$product->selectedPrice]['priceWithoutTax'];
$_orderItems->product_discountedPriceWithoutTax = $product->allPrices[$product->selectedPrice]['discountedPriceWithoutTax'];
$_orderItems->order_status = 'P';

$allPrices = $product->allPrices[$product->selectedPrice];

foreach($allPrices as $key=>$calcRule)
{
if(in_array($key, $calculation_kinds) && is_array($calcRule) && count($calcRule) > 0)
{
$product_calc_rules[$key]['itemsIds'][] = $product->virtuemart_product_id;
}
}

$line_items[] = $_orderItems;
}

return array('items' => $line_items, 'calcRulesProductIds' => $product_calc_rules);
}

private function _formatCalcRules($_cart, $calcRulesProductIds)
{
$calc_rules = array();

$productKeys = array_keys($_cart->products);

$calculation_kinds = array('DBTax','Tax','VatTax','DATax','Marge');

foreach($productKeys as $key){
foreach($calculation_kinds as $calculation_kind) {

if(!isset($_cart->cartPrices[$key][$calculation_kind])) continue;
$productRules = $_cart->cartPrices[$key][$calculation_kind];

foreach($productRules as $rule){
if(empty($rule[4])){
continue;
}

$orderCalcRules = new stdClass();

//$orderCalcRules = $this->getTable('order_calc_rules');
$orderCalcRules->virtuemart_order_calc_rule_id= null;
$orderCalcRules->virtuemart_calc_id= $rule[7];
$orderCalcRules->virtuemart_order_item_id = $_cart->products[$key]->virtuemart_order_item_id;
$orderCalcRules->calc_rule_name = $rule[0];
$orderCalcRules->calc_amount =  0;
$orderCalcRules->calc_result =  0;
if ($calculation_kind == 'VatTax') {
$orderCalcRules->calc_amount =  $_cart->cartPrices[$key]['taxAmount'];
$orderCalcRules->calc_result =  $_cart->cartData['VatTax'][$rule[7]]['result'];
}
$orderCalcRules->calc_value = $rule[1];
$orderCalcRules->calc_mathop = $rule[2];
$orderCalcRules->calc_kind = $calculation_kind;
$orderCalcRules->calc_currency = $rule[4];
$orderCalcRules->calc_params = $rule[5];
$orderCalcRules->virtuemart_vendor_id = $rule[6];


// - this is ugly hack for adjustments for helper functions
if($calculation_kind == 'VatTax' || $calculation_kind == 'Tax')
{
if( array_key_exists($calculation_kind, $calcRulesProductIds) &&
count($calcRulesProductIds[$calculation_kind]['itemsIds']) > 0 )
{
$orderCalcRules->virtuemart_order_item_id = array_pop($calcRulesProductIds[$calculation_kind]['itemsIds']);
}
}

$calc_rules[] = $orderCalcRules;
}
}
}


$Bill_calculation_kinds=array('DBTaxRulesBill', 'taxRulesBill', 'DATaxRulesBill');

foreach($Bill_calculation_kinds as $calculation_kind) {

foreach($_cart->cartData[$calculation_kind] as $rule){

$orderCalcRules = new stdClass();

//$orderCalcRules = $this->getTable('order_calc_rules');
$orderCalcRules->virtuemart_order_calc_rule_id = null;
$orderCalcRules->virtuemart_calc_id= $rule['virtuemart_calc_id'];
$orderCalcRules->calc_rule_name= $rule['calc_name'];
$orderCalcRules->calc_amount =  $_cart->cartPrices[$rule['virtuemart_calc_id'].'Diff'];
if ($calculation_kind == 'taxRulesBill' and !empty($_cart->cartData['VatTax'][$rule['virtuemart_calc_id']]['result'])) {
$orderCalcRules->calc_result =  $_cart->cartData['VatTax'][$rule['virtuemart_calc_id']]['result'];
}
$orderCalcRules->calc_kind=$calculation_kind;
$orderCalcRules->calc_currency=$rule['calc_currency'];
$orderCalcRules->calc_value=$rule['calc_value'];
$orderCalcRules->calc_mathop=$rule['calc_value_mathop'];
$orderCalcRules->calc_params=$rule['calc_params'];
$orderCalcRules->virtuemart_vendor_id = $rule['virtuemart_vendor_id'];

$calc_rules[] = $orderCalcRules;
}
}

if(!empty($_cart->virtuemart_paymentmethod_id)){

if(empty($_cart->cartPrices['payment_calc_id'])){

} else {

$orderCalcRules = new stdClass();

//$orderCalcRules = $this->getTable('order_calc_rules');
$calcModel = VmModel::getModel('calc');
$calcModel->setId($_cart->cartPrices['payment_calc_id']);
$calc = $calcModel->getCalc();
if(empty($calc->calc_currency) or empty($calc->calc_value)) {

} else {
$orderCalcRules->virtuemart_order_calc_rule_id = null;
$orderCalcRules->virtuemart_calc_id = $calc->virtuemart_calc_id;
$orderCalcRules->calc_kind = 'payment';
$orderCalcRules->calc_rule_name = $calc->calc_name;
$orderCalcRules->calc_amount = $_cart->cartPrices['paymentTax'];
$orderCalcRules->calc_value = $calc->calc_value;
$orderCalcRules->calc_mathop = $calc->calc_value_mathop;
$orderCalcRules->calc_currency = $calc->calc_currency;
$orderCalcRules->calc_params = $calc->calc_params;
$orderCalcRules->virtuemart_vendor_id = $calc->virtuemart_vendor_id;

$calc_rules[] = $orderCalcRules;
}
}
}

if(!empty($_cart->virtuemart_shipmentmethod_id)){

if(empty($_cart->cartPrices['shipment_calc_id'])){

} else {

$orderCalcRules = new stdClass();

//$orderCalcRules = $this->getTable('order_calc_rules');
$calcModel = VmModel::getModel('calc');
$calcModel->setId($_cart->cartPrices['shipment_calc_id']);
$calc = $calcModel->getCalc();
if(empty($calc->calc_currency) or empty($calc->calc_value)) {

} else {
$orderCalcRules->virtuemart_order_calc_rule_id = null;
$orderCalcRules->virtuemart_calc_id = $calc->virtuemart_calc_id;
$orderCalcRules->calc_kind = 'shipment';
$orderCalcRules->calc_rule_name = $calc->calc_name;
$orderCalcRules->calc_amount = $_cart->cartPrices['shipmentTax'];
$orderCalcRules->calc_value = $calc->calc_value;
$orderCalcRules->calc_mathop = $calc->calc_value_mathop;
$orderCalcRules->calc_currency = $calc->calc_currency;
$orderCalcRules->calc_params = $calc->calc_params;
$orderCalcRules->virtuemart_vendor_id = $calc->virtuemart_vendor_id;

$calc_rules[] = $orderCalcRules;
}
}
}

return $calc_rules;
}