PrestaShop / docs

PrestaShop technical documentation
https://devdocs.prestashop-project.org/
Other
117 stars 466 forks source link

Product cancel hook doesn't fire when refunding only shipping costs #1405

Open andrewrowanwallee opened 2 years ago

andrewrowanwallee commented 2 years ago

We have a use case whereby a merchant wishes to be able to refund only the shipping costs, not any part of the item. We need to be able to register this refund in our payment module but the hookActionProductCancel doesn't fire if no product is being cancelled. Is there a way to hook into shipping costs refunds? Thanks

andrewrowanwallee commented 1 year ago

@prestaforum any ideas?

Kasvaja commented 2 months ago
  1. Create a Custom Hook Since PrestaShop is modular and allows for extensive customization, you can create a custom hook that specifically deals with shipping cost refunds. Here's how you can define a new hook:

php Copy code // Register the hook in your module's install function public function install() { return parent::install() && $this->registerHook('actionRefundShippingCosts'); }

// Implement the hook execution logic in your module public function hookActionRefundShippingCosts($params) { // Add your logic to handle the refund of shipping costs here }

  1. Trigger the Hook Manually You need to find a place in your PrestaShop files (most likely in a controller or a class that deals with order handling) where the refund process for shipping costs is decided. In that place, you can manually trigger the custom hook you've created:

php Copy code // Assuming $order and $shippingRefundAmount are defined Hook::exec('actionRefundShippingCosts', [ 'order' => $order, 'amount' => $shippingRefundAmount ]);

  1. Adjust the Order Total Manually Since this action doesn't natively exist in PrestaShop, you may also need to manually adjust the order totals and possibly interact with the payment module to process the refund:

php Copy code $order->total_paid -= $shippingRefundAmount; $order->total_paid_tax_incl -= $shippingRefundAmount; $order->total_paid_tax_excl -= $shippingRefundAmount; // Adjust according to whether your shipping costs are taxed or not $order->update();

  1. Log the Refund for Reporting Make sure to log these operations appropriately. You can use PrestaShop's OrderSlip class to create a record of the refund:

php Copy code $order_slip = new OrderSlip(); $order_slip->id_customer = (int)$order->id_customer; $order_slip->id_order = (int)$order->id_order; $order_slip->conversion_rate = 1; // Modify based on your needs $order_slip->total_shipping_cost = $shippingRefundAmount; $order_slip->add();

  1. Test Thoroughly Since this is a customization outside the standard PrestaShop workflows, thoroughly test this feature across different scenarios to ensure stability and reliability.

  2. Consider the Payment Gateway Consult with your payment gateway provider or documentation to handle the actual refund process correctly. This might require API calls or other integrations to ensure the refunded amount is processed back through the customer's original payment method.