Skip to content

Instantly share code, notes, and snippets.

@bummzack
Created January 30, 2017 16:44
Show Gist options
  • Save bummzack/6f5e1733ce966e2088aec2bbe5ceea6c to your computer and use it in GitHub Desktop.
Save bummzack/6f5e1733ce966e2088aec2bbe5ceea6c to your computer and use it in GitHub Desktop.
OrderActionsForm:
allow_cancelling: false
allow_paying: false
extensions:
- OrderActionsFormExtension
<?php
/**
*
*/
class OrderActionsFormExtension extends Extension
{
private static $allowed_actions = array(
'copyOrderToCart'
);
public function updateForm()
{
if($actions = $this->owner->Actions()){
$actions->push(
FormAction::create('copyOrderToCart',
_t('OrderActionsFormExtension.CopyOrderToCart', 'Copy order to cart')
)
);
}
}
public function copyOrderToCart($data, $form)
{
$form->clearMessage();
if($order = $this->owner->order){
$cart = ShoppingCart::singleton();
foreach($order->Items() as $item){
// try to add the product if it's still available
if($product = $item->Buyable()){
$cart->add($product, $item->Quantity);
}
}
return $this->owner->controller->redirect(CartPage::find_link());
}
$form->sessionMessage(
_t('OrderActionsFormExtension.CouldNotRestoreCart', 'Unable to add your order to the shopping-cart.'),
'bad'
);
return $this->owner->controller->redirectBack();
}
}
@bummzack
Copy link
Author

To make things more robust, you could also store the SKU of a product with each OrderItem. That way you could also restore the Items in cart by SKU (for example if Items were deleted and re-imported and therefore have a different ID, but the same SKU)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment