Author Topic: modification for checkout  (Read 383 times)

guardiano78

  • Jr. Member
  • **
  • Posts: 229
    • C.S.S. Informatica
modification for checkout
« on: May 02, 2018, 19:19:06 pm »
Hello,
I need to know what is the function that deletes the row in the #__virtuemart_carts table when the order is submitted.

I'll explain with an example:
in my website I have the product "Bottle of milk" with stock 15.

- I put 10 pieces in the cart and go into the cart.
- Someone else is buying 8 pieces of the same product and makes the checkout., Then the stock becomes 7 pieces.
- At this point I decide to finalize the purchase but it happens that I buy the remaining 7 pieces, while the other 3 I had in the cart are lost.

My goal is to recover those 3 pieces by writing a line in another table.

To do this I need to know in which file and which function does the deletion of the row in the table #__virtuemart_carts; so I can do another operation before the row is deleted.

Can anyone help me?
Thanks.

lausianne

  • Beginner
  • *
  • Posts: 44
Re: modification for checkout
« Reply #1 on: May 03, 2018, 15:03:02 pm »
Does this happen when doing the shopping on two different browsers? If it is in the same browser, I imagine that there could be a cookie issue.

guardiano78

  • Jr. Member
  • **
  • Posts: 229
    • C.S.S. Informatica
Re: modification for checkout
« Reply #2 on: May 03, 2018, 15:35:24 pm »
Hi lausianne,

i use a plugin that save the cart. This is not the problem.
How virtuemart works is fine for me!

I only need to write the remained number of products in another database table before deleting the row in #_virtuemart_carts.
I'm sorry if my explanation is not clear, i try do an example:

How work now:
- I put in the cart 10 pieces of product named AAAA
- the stock for AAAA is 22 pieces
- now another user purchase15 pieces of AAAA
- the new stock for AAAA is 7 pieces
- Now i click on "Complete the Order"
- In the order i bought 7 pieces of AAAA, but the other 3 pieces go losts, infact in #_virtuemart_carts the row is been deleted
- New stock of AAAA is 0

How i would like it work:
- I put in the cart 10 pieces of product named AAAA
- the stock for AAAA is 22 pieces
- now another user purchase15 pieces of AAAA
- the new stock for AAAA is 7 pieces
- Now i click on "Complete the Order"
- In the order i bought 7 pieces of AAAA, the other 3 pieces are written in a specific table in database named for example "product_out_of_stock" (product_id, num_of_products, user_id)and after in #_virtuemart_carts the row is been deleted
- New stock of AAAA is 0


For do this i need to locate the exact file and the exact sourcecode that empty my cart after checkout.
Thank you.

guardiano78

  • Jr. Member
  • **
  • Posts: 229
    • C.S.S. Informatica
Re: modification for checkout
« Reply #3 on: May 03, 2018, 16:33:02 pm »
Hello,
is it possible that is in components/com_virtuemart/helpers/cart.php ?

at line 1255 the function

Code: [Select]
static public function emptyCartValues(&$cart, $session = true){

//We delete the old stuff
$cart->products = array();
$cart->cartProductsData = array();
$cart->cartData = array();
$cart->cartPrices = array();
$cart->cartfields = array();
$cart->_inCheckOut = false;
$cart->_dataValidated = false;
$cart->_confirmDone = false;
$cart->couponCode = '';
$cart->order_language = '';
$cart->virtuemart_shipmentmethod_id = 0; //OSP 2012-03-14
$cart->virtuemart_paymentmethod_id = 0;
$cart->order_number=null;
$cart->_fromCart = false;
$cart->_inConfirm = false;
$cart->totalProduct=false;
$cart->productsQuantity=array();
$cart->virtuemart_order_id = null;

if($session){
$cart->deleteCart();
$cart->setCartIntoSession(false,true);
}
}


????


Thanks.

guardiano78

  • Jr. Member
  • **
  • Posts: 229
    • C.S.S. Informatica
Re: modification for checkout
« Reply #4 on: May 03, 2018, 19:39:13 pm »
Hello,

maybe I'm wrong ...
This operation must be done when the comparison between the quantity in the cart and the actual availability is made.

Does anyone know what is the function that deals with the comparison in the cart?
Thanks.

Studio 42

  • Contributing Developer
  • Sr. Member
  • *
  • Posts: 2974
  • Joomla & Virtuemart addon developper
    • Studio 42 - Virtuemart & Joomla extentions
  • VirtueMart Version: 2.6 & 3.0.x.y
Re: modification for checkout
« Reply #5 on: May 04, 2018, 09:49:56 am »
Your explain are very strange, i know VM code but dont understand.
Say for eg. current stock is 5
You do a cart of quantity 10, your stock is -5 after customer's checkout.
If you use stock handle, customer can only buy 5 and stock is 0.
If you use the last case, then you can perhaps suggest user to register as "stay me informed' for the product.
Another solution is to use a simple Joomla system plugin function onAfterRoute checking for stock and save the missing stock in a table, because you dont want prevent the standard add to cart.
Of course you have to get the Joomla user infos(if you get it), if you want send him a mail after or use the session to follow the user actions and collect user informations from order after .

guardiano78

  • Jr. Member
  • **
  • Posts: 229
    • C.S.S. Informatica
Re: modification for checkout
« Reply #6 on: May 04, 2018, 10:42:47 am »
Hello Studio 42,
thank you for response.

In fact I already use a custom plugin that takes care of putting the products out of stock in backorder.
So I just need to insert in the table used by the backorder, the products that could not be sold due to lack of stock.
For the moment I'm proceeding like this:

In cart page:
Code: [Select]
<?php 
// get Joomla session object
$session JFactory::getSession();

// get current session cart data
$session->set('carrello'$session->get('vmcart',0,'vm')); //i put in session a variable with the cart data before checkout
?>


In cart.php
Code: [Select]
public function emptyCart(){

// get Joomla session object
$session = JFactory::getSession();

// get current session cart data
$carrello = json_decode($session->get('carrello')); //I get cart data before checkout

self::emptyCartValues($this, $carrello);

}

/**
* emptyCart: Used for payment handling.
*
* @author Valerie Cartan Isaksen
*
*/
static public function emptyCartValues(&$cart, $carrello, $session = true){

JFactory::getApplication()->enqueueMessage('forse ho trovato il punto giusto');
//print_r($cart);
file_put_contents("ciao.txt", print_r($cart->cartProductsData)); //print cart data after checkout -  Array ( [0] => Array ( [virtuemart_product_id] => 73469 [quantity] => 2 [customProductData] => Array ( ) ) )
file_put_contents("ciao.txt", print_r($carrello->cartProductsData)); //print cart data before checkout - Array ( [0] => stdClass Object ( [virtuemart_product_id] => 73469 [quantity] => 4 [customProductData] => Array ( ) ) ) 

                //now i have to compare product_id and quantity between cart data before chechout and cart data after checkout



//We delete the old stuff
$cart->products = array();
$cart->cartProductsData = array();
$cart->cartData = array();
$cart->cartPrices = array();
$cart->cartfields = array();
$cart->_inCheckOut = false;
$cart->_dataValidated = false;
$cart->_confirmDone = false;
$cart->couponCode = '';
$cart->order_language = '';
$cart->virtuemart_shipmentmethod_id = 0; //OSP 2012-03-14
$cart->virtuemart_paymentmethod_id = 0;
$cart->order_number=null;
$cart->_fromCart = false;
$cart->_inConfirm = false;
$cart->totalProduct=false;
$cart->productsQuantity=array();
$cart->virtuemart_order_id = null;

if($session){
$cart->deleteCart();
$cart->setCartIntoSession(false,true);
}
}

This solution may work fine in your opinion?

Thank you.

Studio 42

  • Contributing Developer
  • Sr. Member
  • *
  • Posts: 2974
  • Joomla & Virtuemart addon developper
    • Studio 42 - Virtuemart & Joomla extentions
  • VirtueMart Version: 2.6 &amp; 3.0.x.y
Re: modification for checkout
« Reply #7 on: May 05, 2018, 23:29:06 pm »
Hum ,for me your solution is bad because.
- you use a text file
- the file "ciao.txt" have no path
If you want to add trigger, you can create a shipment plugin and function plgVmConfirmedOrder, this should always be called before cart is empty and dont need to modify core file.
for the add to cart the method explained before should work.
If you create a system plugin, you can add the two trigger, you have only to load some Virtuemart class or config perhaps, if it not work
So you need to include
class PlgSystemMYPLUGIN extends JPlugin
{
 function onAfterRoute(){
  //Check if option = com_viruemart and task = add or addJS, collect the quantities and set product with lowstock in a table. Do same perhaps on quantity update, product remove ...
 }
 function plgVmConfirmedOrder(){
  //here you can check the cart content again if needed ?
 }
}

guardiano78

  • Jr. Member
  • **
  • Posts: 229
    • C.S.S. Informatica
Re: modification for checkout [SOLVED]
« Reply #8 on: May 07, 2018, 10:37:37 am »
Hi Studio 42,
"ciao.txt" only served to display the contents of the arrays.

Unfortunately I do not have the knowledge to create a plugin yet.
But I got the desired result by inserting some code in cart.php as I wrote in the previous post.

Writing the code in the function "emptyCartValues", i verified that it works with all the payment methods i use, including paypal.

Thanks for the help.

Studio 42

  • Contributing Developer
  • Sr. Member
  • *
  • Posts: 2974
  • Joomla & Virtuemart addon developper
    • Studio 42 - Virtuemart & Joomla extentions
  • VirtueMart Version: 2.6 &amp; 3.0.x.y
Re: modification for checkout
« Reply #9 on: May 07, 2018, 14:49:29 pm »
I only give you the general concept to do it without hacks