Created
January 30, 2017 16:44
-
-
Save bummzack/6f5e1733ce966e2088aec2bbe5ceea6c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
OrderActionsForm: | |
allow_cancelling: false | |
allow_paying: false | |
extensions: | |
- OrderActionsFormExtension |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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)