Author Topic: [SOLVED] Registered user can cancel "Pending" order Tutorial  (Read 7175 times)

kjogiste

  • Beginner
  • *
  • Posts: 11
    • Deffex Media - Your idea, our realization!
[SOLVED] Registered user can cancel "Pending" order Tutorial
« on: April 22, 2010, 16:50:41 pm »
Hello everyone,

With my last project i needed one core hack for my VirtueMart. I had to make one function to registered user: more clearly: my client wanted to have function that every registered user from his site can "Cancel" his/hers orders. So i thought it would be helpful and useful for someone who have samekind of issue:

Tutorial:

Files that you have to use are located:

administrator/components/com_virtuemart/classes/ps_order.php (file 1)

components/com_sh404sef/sef_ext/com_virtuemart.php (file 2)

components/com_sh404sef/language/plugins/com_virtuemart.php (file 3)

components/com_virtemart/languages/common/english.php (file 4)


* People who dont use com_sh404sef, can skip editing file 2 and file 3

For convenience, I’ll be referring them to as file 1, file 2, file 3 and file 4 – as their order above.

Now, the first step is to make an order cancellation button appear on the account panel of the user. For this, open file 1 and go to the function ‘list_order’. This should be around line 548. Look for code:

while ($db->next_record()) {

         $order_status = ps_order_status::getOrderStatusName($db->f("order_status"));

         $listObj->newRow();

         $tmp_cell = "<a href=\"". $sess->url( $mm_action_url."index.php?page=account.order_details&order_id=".$db->f("order_id") )."\">\n";
         $tmp_cell .= "<img src=\"".IMAGEURL."ps_image/goto.png\" height=\"32\" width=\"32\" align=\"middle\" border=\"0\" alt=\"".$VM_LANG->_('PHPSHOP_ORDER_LINK')."\" />&nbsp;".$VM_LANG->_('PHPSHOP_VIEW')."</a><br />";
         $listObj->addCell( $tmp_cell );

         $tmp_cell = "<strong>".$VM_LANG->_('PHPSHOP_ORDER_PRINT_PO_DATE').":</strong> " . strftime("%d. %B %Y", $db->f("cdate"));
         $tmp_cell .= "<br /><strong>".$VM_LANG->_('PHPSHOP_ORDER_PRINT_TOTAL').":</strong> " . $CURRENCY_DISPLAY->getFullValue($db->f("order_total"), '', $db->f('order_currency'));
         $listObj->addCell( $tmp_cell );

         $tmp_cell = "<strong>".$VM_LANG->_('PHPSHOP_ORDER_PRINT_PO_STATUS').":</strong> ".$order_status;
         $tmp_cell .= "<br /><strong>".$VM_LANG->_('PHPSHOP_ORDER_PRINT_PO_NUMBER').":</strong> " . sprintf($db->f("order_number"));
         $listObj->addCell( $tmp_cell );
}


Towards end of the while code, just below the last “$listObj->addcell($tmp_cell)” add the following code:

while ($db->next_record()) {

         $order_status = ps_order_status::getOrderStatusName($db->f("order_status"));

         $listObj->newRow();

         $tmp_cell = "<a href=\"". $sess->url( $mm_action_url."index.php?page=account.order_details&order_id=".$db->f("order_id") )."\">\n";
         $tmp_cell .= "<img src=\"".IMAGEURL."ps_image/goto.png\" height=\"32\" width=\"32\" align=\"middle\" border=\"0\" alt=\"".$VM_LANG->_('PHPSHOP_ORDER_LINK')."\" />&nbsp;".$VM_LANG->_('PHPSHOP_VIEW')."</a><br />";
         $listObj->addCell( $tmp_cell );

         $tmp_cell = "<strong>".$VM_LANG->_('PHPSHOP_ORDER_PRINT_PO_DATE').":</strong> " . strftime("%d. %B %Y", $db->f("cdate"));
         $tmp_cell .= "<br /><strong>".$VM_LANG->_('PHPSHOP_ORDER_PRINT_TOTAL').":</strong> " . $CURRENCY_DISPLAY->getFullValue($db->f("order_total"), '', $db->f('order_currency'));
         $listObj->addCell( $tmp_cell );

         $tmp_cell = "<strong>".$VM_LANG->_('PHPSHOP_ORDER_PRINT_PO_STATUS').":</strong> ".$order_status;
         $tmp_cell .= "<br /><strong>".$VM_LANG->_('PHPSHOP_ORDER_PRINT_PO_NUMBER').":</strong> " . sprintf($db->f("order_number"));
         $listObj->addCell( $tmp_cell );
// cancellation page to be created
                        if ($db->f('order_status') == 'P' || $db->f('order_status') == 'C') {
                          $tmp_cell = "<a href=\"". $sess->url( $mm_action_url."index.php?page=account.order_cancel&order_id=".$db->f('order_id') )."\">\n";
                          $tmp_cell .= "<img src=\"".IMAGEURL."ps_image/cancel.png\" height=\"32\" width=\"32\" align=\"middle\" border=\"0\" alt=\"".$VM_LANG->_('PHPSHOP_ORDER_LINK')."\" />&nbsp;".$VM_LANG->_('PHPSHOP_CANCEL')."</a><br />";
                          $listObj->addCell( $tmp_cell );
                        }

}


This allows for the button to appear when the order is in “Pending” or “Confirmed” mode. This is to allow the user to cancel his order before the item has been shipped. If you’d like any other kind of behaviour, edit it as you deem fit. You’ll also need a “cancel.png” file at the following location:

components/com_virtuemart/shop_image/ps_image/

This should preferably be a square image with 32×32 as the dimensions. Even if the image is bigger, it’ll get resized to the 32×32 dimensions, so make sure your image looks good on those scales.

If you’ve correctly applied the above code, you should be seeing a nice cancel button in the account maintenance tab in the list of order, to the rightmost for “Pendin” / “Confirmed” orders. If you click the link, you’ll get a 404 page as there is no cancellation page yet.

For that, find the attached files. Please the files as follows:

account.order_cancel.php -> administrator/components/com_virtuemart/html

account.order_cancel.php ->  components/com_virtuemart/themes/default/templates/pages


After this is done, open file 3 and in the english section add at the last:

$sh_LANG['en']['_PHPSHOP_ORDER_CANCEL'] = 'Order cancellation';

Now, open file 4 and add:

'PHPSHOP_ACC_ORDER_CANCEL' => 'Cancellation of ',
'PHPSHOP_ACC_ORDER_CANCEL_TITLE' => 'Order Cancellation',
'PHPSHOP_ACC_ORDER_CANCEL_MESSAGE' => 'Are you sure you want to cancel this order ?',
'PHPSHOP_ACC_ORDER_CANCEL_MESSAGE_ERROR' => 'Unable to cancel this order! Please try later or contact customer care.',
'PHPSHOP_ACC_ORDER_CANCEL_MESSAGE_SUCCESS' => 'Success cancelling order! You will receive your refund shortly.',
'PHPSHOP_ACC_ORDER_CANCEL_MESSAGE_DENY' => 'Order is already cancelled.',
'PHPSHOP_ACC_ORDER_CANCEL_MESSAGE_DECLINE' => 'Unauthorized access!',


You can add this at anyplace you feel appropriate below:

$langvars = array (

before the block ends.

Now, just to make the whole thing SEF compatible, open file 2 and find:

case 'account.order_details':
$order_id = isset($order_id) ? @$order_id : null;
if ($sefConfig->shVmInsertShopName) $title[] = $shShopName;
if ($shVmCChk)
$title[] = 'vmchk';
$title[] =  $sh_LANG[$shLangIso]['_PHPSHOP_VIEW'].$sefConfig->replacement
.$sh_LANG[$shLangIso]['_PHPSHOP_ORDER_ITEM']
.($order_id ? $sefConfig->replacement.'id'.strval($order_id):'');
if (isset($order_id))  // V 1.2.4.r
shRemoveFromGETVarsList('order_id');
break;


and add just below it the following:

case 'account.order_cancel':
$order_id = isset($order_id) ? @$order_id : null;
if ($sefConfig->shVmInsertShopName) $title[] = $shShopName;
if ($shVmCChk)
$title[] = 'vmchk';
$title[] =  $sh_LANG[$shLangIso]['_PHPSHOP_VIEW'].$sefConfig->replacement
.$sh_LANG[$shLangIso]['_PHPSHOP_ORDER_CANCEL']
.($order_id ? $sefConfig->replacement.'id'.strval($order_id):'');
if (isset($order_id))  // V 1.2.4.r
shRemoveFromGETVarsList('order_id');
shRemoveFromGETVarsList('order_cancel');
break;  


That’s it! You’re now have an order cancellation button (assuming you edited correctly and I haven’t forgotten anything) in the Account Maintenance tab! This should allow the user to cancel the orders till the confirmed stage.

If you would like to be notified when an order has been cancelled, add the notify code for cancellation case in the notify_customer function in file 1.

Hope this helps a lot of people doing e-commerce with the open source friendly virtuemart!

I got this tutorial from: http://www.nandebayo.org/blog/?p=40 and send my regards for the author.

Best regards,
Kristjan Jogiste
www.deffex.com

[attachment cleanup by admin]
Deffex Media - Your idea, our realization! (www.deffex.com)

richsung

  • Jr. Member
  • **
  • Posts: 85
Re: Cancel "Pending" order Tutorial
« Reply #1 on: April 22, 2010, 16:54:47 pm »
Hey Kristjan - that's awesome! I've been looking to implement this function down this line...

Cheers,
--Rich

pedro555

  • Beginner
  • *
  • Posts: 1
Re: [SOLVED] Registered user can cancel "Pending" order Tutorial
« Reply #2 on: July 03, 2010, 15:19:11 pm »
Hello, I'm new here and new in VirtueMart.
My shop store is working, but I have a lot of double order from the same client and I need this feature so much.
But I’m trying to put this feature in my shop store. The cancel button is showing but with the wrong link. The link: http://www.mywebsite.com.br/jogos.html?order_id=3
*jogos is a section in my website.
I guess is something wrong with the ps_order file.
Can you help me?
Thanks

grietbroek

  • Beginner
  • *
  • Posts: 4
Re: [SOLVED] Registered user can cancel "Pending" order Tutorial
« Reply #3 on: August 12, 2010, 09:08:45 am »
Hi thanks for posting this, I really don't understand they haven't implemented this as standard future in VM.

1 Small question/tip: When going to the cancelpage, the ordernumber is a very strange string of numbers and characters. I don't know if this is on purpose?

Anyway, if you want to change it to the 'real' ordernumber change this:

<tr>
            <td><?php echo $VM_LANG->_('PHPSHOP_ORDER_PRINT_PO_NUMBER')?>:</td>
            <td><?php printf($db->f('order_number')); ?></td>
        </tr>


To this:

<tr>
            <td><?php echo $VM_LANG->_('PHPSHOP_ORDER_PRINT_PO_NUMBER')?>:</td>
            <td><?php printf("%08d", $db->f("order_id")); ?></td>
        </tr>


But still I am very happy with this easy solution, will spare me some time creating it:)

BeTheDance

  • Beginner
  • *
  • Posts: 2
Re: [SOLVED] Registered user can cancel "Pending" order Tutorial
« Reply #4 on: November 17, 2010, 05:48:25 am »
Followed this correctly, I believe, but it's not cancelling the order. Does anyone have this working with 1.1.5?

Thanks.

FilipeRuivo

  • Jr. Member
  • **
  • Posts: 62
Re: [SOLVED] Registered user can cancel "Pending" order Tutorial
« Reply #5 on: November 27, 2010, 06:40:00 am »
Hello

Could you add a feature to automatically update the stock when the customer cancel a order?

Thanks

nyuszi

  • Beginner
  • *
  • Posts: 1
Re: [SOLVED] Registered user can cancel "Pending" order Tutorial
« Reply #6 on: June 12, 2011, 16:32:56 pm »
Hi!

This feature does not work when the user is ordering more!

Any idea?

CowBoyDave

  • Beginner
  • *
  • Posts: 5
Re: [SOLVED] Registered user can cancel "Pending" order Tutorial
« Reply #7 on: June 28, 2011, 18:45:54 pm »
Thanks! VERY helpful.

jerry05031111

  • Beginner
  • *
  • Posts: 2
Re: [SOLVED] Registered user can cancel "Pending" order Tutorial
« Reply #8 on: July 02, 2011, 03:07:09 am »
Could you add a feature to automatically update the stock when the customer cancel a order?

Thanks





buy outlook 2010
Professional Plus 2010
Microsoft Outlook 2010

Stonedfury

  • Jr. Member
  • **
  • Posts: 266
    • Stonedfury
  • Skype Name: Stonedfury
  • VirtueMart Version: 2.0.24
Re: [SOLVED] Registered user can cancel "Pending" order Tutorial
« Reply #9 on: September 21, 2011, 20:11:20 pm »
I would love to add this to our site however I am curious if the issue of it only showing on the current order. Such as if the customer places 2 orders the button only showed on one of the two till the first one is cancelled.

Can anyone please inform me if that is taken care of?
No such thing as a stupid question, only a stupid answer. I won't ask if I know the answer or found it using the search as you suggested.

shakensoul

  • Beginner
  • *
  • Posts: 24
Re: [SOLVED] Registered user can cancel "Pending" order Tutorial
« Reply #10 on: January 17, 2012, 08:21:54 am »
This seems to be a great mod and I have got it working till getting the cancel button on the order screen. After that a couple of questions...

I am using Joomla Version 1.5.23 and VM 1.1.9

1. There is no english.php file in the location specified

components/com_virtemart/languages/common/english.php

I have edited the english.php located in

administrator/component/languages/common/english.php

but not able to enter the code

Quote
$langvars = array (

at the end of the file as website throws an error about invalid string

2. I have artio joomsef installed. Now when I click on the cancel button, it redirects to the homepage page because I presume joomsef is not able to find the account cancel page.

If anyone can help with the code for joomsef. This is the code from my

components/com_sef/sef_ext/com_virtuemart.php

Quote
case 'account.order_details':
                    $title[] = $texts['_PHPSHOP_ACCOUNT'];
                    $title[] = $texts['_PHPSHOP_ORDER'];
                    if (! $sefConfig->appendNonSef)
                        $title[] = $order_id;
                    break;