0

I am using VM 3.0.2 with JM 3.3.
I know that order cancellation is, unfortunately, not available in VM3 and I am trying to add it.
To do this, I added a link to the "order list" on the front end for registered users. I have also changed it so that if the order is still in "Confirmed By Customer", it adds a button called cancel.

I added an ajax call when "cancel" is clicked to call a file.php with the following:

if(!empty($_REQUEST['id'])) { 
            $id= $_REQUEST['id'];
            if ($id) {

                    if (!class_exists('VirtueMartModelOrders'))
                            require( JPATH_VM_ADMINISTRATOR . DS . 'models' . DS . 'orders.php' );
                    $modelOrder = new VirtueMartModelOrders();
                    $order['order_status'] = 'X';
                    $order['virtuemart_order_id'] = $id;
                    $order['customer_notified'] = 1;
                    $modelOrder->updateStatusForOneOrder($id, $order, true);
            }
    }

My ajax call is:

<script>
jQuery(document).ready(function($) {
  $(".can").click(function(event) {
    var id = event.target.id;
    $.ajax({
      type: 'get',
      url: '/orders/f.php',
      data: {
        id: id
      },
      success: function(data) {
        alert('tritri');
      },
      error: function(jqXHR, status, err) {
        alert(err);
      }
    });
  });
});
</script>

Problem is that the result is always alerting the word tritri and order is not getting cancelled... I have placed f.php in views/orders/tmpl...

What am I doing wrong? Is there a simpler way to achieve this?

2

A jquery ajax request is still technically "successful" as long as the ajax request doesn't run into an actual error in the process of execution-- as long as it's successful in hitting the endpoint and returning with something, it's successful-- it doesn't know about your application state.

So what you need to do is gain some insight into what's actually happening on the other end. You can use the new Joomla JResponseJson class to help return an object that will have an application-specific (rather than ajax-specific) success / failure message.

Psuedo-code:

if(!empty($_REQUEST['id'])) { 
        $id= $_REQUEST['id'];
        if ($id) {
                if (!class_exists('VirtueMartModelOrders')) require( JPATH_VM_ADMINISTRATOR . DS . 'models' . DS . 'orders.php' );
                $modelOrder = new VirtueMartModelOrders();
                $order['order_status'] = 'X';
                $order['virtuemart_order_id'] = $id;
                $order['customer_notified'] = 1;
                try 
                {
                    $result = $modelOrder->updateStatusForOneOrder($id, $order, true);
                    echo new JResponseJson($result);
                }
                catch(Exception $e)
                {
                    // assuming the model throws a real exception-- otherwise check the $result value for success / failure
                    echo new JResponseJson($e);
                }
        }
}

Then, in your ajax function's success block, check what result is there, and if it's an error message alert / display it, or continue processing if it's a success message:

  success: function(data) {
    if(data.success){
      // real success
    } else {
      // handle error
    }
  },
  error: function(jqXHR, status, err) {
    alert(err);
  }

Hope this helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.