Delete product from order from PHP

  • Posts: 88
  • Thank you received: 19
1 year 11 months ago #341056

-- HikaShop version -- : 4.5.1
-- Joomla version -- : 4.x

Hi

I'm creating a script to handle batch refunding on orders of a specific product. If an order contains more than this specific product, I want to remove it from the order and add a history note with the refund.

I know how to save or modify an order and add history to it, but does Hikashop has a built-in function to remove a product from an order or can you recommend a way to do this?

Merci

Please Log in or Create an account to join the conversation.

  • Posts: 88
  • Thank you received: 19
1 year 11 months ago #341057

I figured it out.
For anyone else interested in this, this is a simplified version of the code to delete a product from an order:

$orderClass = hikashop_get('class.order');
if(!@include_once(rtrim(JPATH_ADMINISTRATOR,DS).DS.'components'.DS.'com_hikashop'.DS.'helpers'.DS.'helper.php')){ return false; }

if ( ($key = array_search($product->product_id, $order->products) ) !== false) {
    $product_id = $order->products[$key]->product_id;
    $order_product_id = $order->products[$key]->order_product_id;
}

$modifyOrder = new stdClass();
$modifyOrder->order_id = $order->order_id;

$modifyOrder->product = new stdClass();
$modifyOrder->product->product_id = $product_id;
$modifyOrder->product->order_product_id = $order_product_id;
$modifyOrder->product->order_product_quantity = 0;

$orderClass->save($modifyOrder);

It removes the product from $modifyOrder->products - however It's not saving as expected.

Last edit: 1 year 11 months ago by dyvel.

Please Log in or Create an account to join the conversation.

  • Posts: 81506
  • Thank you received: 13064
  • MODERATOR
1 year 11 months ago #341059

Hi,

I would rather do like this:

if(!@include_once(rtrim(JPATH_ADMINISTRATOR,DS).DS.'components'.DS.'com_hikashop'.DS.'helpers'.DS.'helper.php')){ return false; }
if ( ($key = array_search($product->product_id, $order->products) ) !== false) {
    $product_id = $order->products[$key]->product_id;
    $order_product_id = $order->products[$key]->order_product_id;
}
hikaInput::get()->set('cid', $order->order_id);
hikaInput::get()->set('order_product_id', $order_product_id);
$orderClass = hikashop_get('class.order');
$status = $orderClass->saveForm('product_delete');
That's basically the code in the task "product_remove" of the "order" controller, which is behind the delete button of each product on the listins of products of an order on the backend interface.

Please Log in or Create an account to join the conversation.

Time to create page: 0.059 seconds
Powered by Kunena Forum