Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Using MultiAdd to create a "reorder" facility for Craft Commerce
...
<form method="POST">
<p>You can re-order by clicking the button below.</p>
<input type="hidden" name="action" value="logic/orders/reOrder">
<input type="hidden" name="redirect" value="shop/cart">
<input type="hidden" name="oldOrderNumber" value="{{ order.number }}">
{{ getCsrfInput() }}
{% for item in order.lineItems %}{# build the full cart items #}
{% set itemNumber = loop.index %}
<input type="hidden" name="items[{{ loop.index }}][qty]" value="{{ item.qty }}">
<input type="hidden" name="items[{{ loop.index }}][note]" value="{{ item.note }}">
<input type="hidden" name="items[{{ loop.index }}][purchasableId]" value="{{ item.purchasableId }}">
{% for key, option in item.options %}
<input type="hidden" name="items[{{ itemNumber }}][options][{{ key }}]" value="{{ option }}">
{% endfor %}{# key, option in item.options #}
{% endfor %}{# item in order.lineItems #}
<input type="submit" value="Re Order" class="button" />
</form>
...
<?php
// logic/controllers/Logic_OrdersController.php
namespace Craft;
class Logic_OrdersController extends BaseController
{
public function actionReorder()
{
LogicPlugin::log("Action reorder triggered", LogLevel::Info);
// basic security precautions
$this->requirePostRequest();
// setup some required variables
$oldOrderNumber = craft()->request->getPost('oldOrderNumber');
$oldOrder = craft()->commerce_orders->getOrderByNumber( $oldOrderNumber );
// get the 'old' order from the POSTed field
if(!$oldOrder) {
LogicPlugin::log("Couldn't get the order record for the order being edited", LogLevel::Error);
$user->addError('editOrder','Couldnt get the order being edited');
}
// empty the current cart (deletes the cookie, means anything in there is abandoned)
craft()->commerce_cart->forgetCart();
// make a new cart (or get the current cart if we don't delete it first)
$cart = craft()->commerce_cart->getCart();
// set the shipping etc on the cart to the same as the old order
$cart->shippingAddressId = $oldOrder->shippingAddressId;
$cart->shippingAddress = $oldOrder->shippingAddress;
if (!craft()->commerce_cart->setShippingMethod($cart, $oldOrder->shippingMethodHandle, $error)) {
$cart->addError('shippingMethod', $error);
LogicPlugin::log("error adding shipping method to cart: " . $error, LogLevel::Error);
}
// ok now pass it all over to the multiAdd controller...
craft()->runController('multiAdd/multiAdd');
}
}
<?php
// logic/LogicPlugin.php
/**
* logic plugin for Craft CMS
*
* logic
*
* @author VCA
* @copyright Copyright (c) 2016 VCA
* @link http://viewcreative.co.uk
* @package Example
* @since 1
*/
namespace Craft;
class LogicPlugin extends BasePlugin
{
/**
* @return mixed
*/
public function init()
{
}
/**
* @return mixed
*/
public function getName()
{
return Craft::t('logic');
}
/**
* @return mixed
*/
public function getDescription()
{
return Craft::t('Implement a ReOrder facility calling the MultiAdd plugin');
}
/**
* @return string
*/
public function getDocumentationUrl()
{
return '???';
}
/**
* @return string
*/
public function getReleaseFeedUrl()
{
return '???';
}
/**
* @return string
*/
public function getVersion()
{
return '0.1';
}
/**
* @return string
*/
public function getSchemaVersion()
{
return '0.1';
}
/**
* @return string
*/
public function getDeveloper()
{
return 'VCA';
}
/**
* @return string
*/
public function getDeveloperUrl()
{
return 'http://viewcreative.co.uk';
}
/**
* @return bool
*/
public function hasCpSection()
{
return false;
}
/**
*/
public function onBeforeInstall()
{
}
/**
*/
public function onAfterInstall()
{
}
/**
*/
public function onBeforeUninstall()
{
}
/**
*/
public function onAfterUninstall()
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.