Hooks
Customizing Behavior via Hooks
The default behavior of Simpler Checkout is to exactly match the behavior of your wooCommerce store. This affects coupons, discounts and/or available shipping methods.
If you want to modify the checkout behavior only for the customers using Simpler Checkout you can use the programmatic hooks offered by our plugin to dynamically update the behavior of the Simpler Checkout form.
Customizing available shipping methods
The simplerwc_shipping_rates
filter allows you to modify the shipping rates & methods available to customers using Simpler Checkout. It accepts the resolved shipping rates & the cart and should return the modified rates.
add_filter(’simplerwc_shipping_rates’, function($methods) {
return array_filter($methods, function ($el) {
return $el->get_id() ≠ “my custom method”
});
});
Retrieving pickup location id
If you use a shipping method that requires extra information on top of the shipping method id (for example a store pickup method that requires a store id property), you will need to obtain the store that the shopper selected in their Simpler session.
You can leverage the simplerwc_after_set_checkout_data
action to add your custom logic to the order creation process like this :
add_action('simplerwc_after_set_checkout_data', function($order, $request) {
$methodId = $request->get_order()->get_shipping()->get_method_id();
if ($methodId != "local_pickup_method") {
// if not a local pickup order, return
return;
}
$pickupId = $request->get_order()->get_shipping()->get_pickup_location_id();
if (!$pickupId) {
throw new Error('invalid pickup id')
}
//you can use the pickup id in your custom logic here - for example :
$order->get_shipping_rate()->set_meta_data('store_location_id', $pickupId);
});
Disabling Simpler for Specific Products or Carts
If you want to implement custom logic for when the Simpler button is available,
you can use the simplerwc_should_render_product_button
and simplerwc_should_render_cart_button
hooks.
For example, if you want to hide the Simpler button whenever the cart contains a product that belongs to a specific category :
add_filter('simplerwc_should_render_cart_button', function($cart) {
$categoryIdToDisable = 19;
foreach($cart->get_cart_contents() as $item) {
if (in_array($categoryIdToDisable, $item['data']['category_ids'])) {
return false;
}
}
return true;
});
You can apply the same logic for the product page button by using the simplerwc_should_render_product_button
filter :
add_filter('simplerwc_should_render_cart_button', function($product) {
$categoryIdToDisable = 19;
return in_array($categoryIdToDisable, $product['data']['category_ids']);
});
Adding payment methods to Quote
The simplerwc_quotation_payment_method
(available since v0.7.4) filter allows you to modify the payment methods available per shipping method during the quote process. It accepts the Quotation
that the payment methods will be attached to and any previously attached payment methods.
add_filter('simplerwc_quotation_payment_method', function (array $paymentMethods, Quotation $quotation )
{
$availablePaymentMethods = WC()->payment_gateways()->get_available_payment_gateways();
// COD is not configured
if (!isset($availablePaymentMethods['cod'])) {
return $paymentMethods;
}
// COD is disabled
if ($availablePaymentMethods['cod']->enabled != 'yes') {
return $paymentMethods;
}
// COD isn't enabled for any shipping method
if ( ! ($cod = $availablePaymentMethods['cod'])->enable_for_methods) {
return $paymentMethods;
}
// COD isn't enabled for quote's shipping method (e.g. local pickup or BoxNow)
if ( ! in_array($quotation->get_shipping_rate()->get_method_id(), $cod->enable_for_methods)) {
return $paymentMethods;
}
$paymentMethods[] = new PaymentMethod(
$cod->id,
$cod->title,
PaymentMethod::COD,
Money::to_cents($cod->cost),
null,
null
);
return $paymentMethods;
}, 10, 2);
Changing the COD extra fee
The simplerwc_cod_cost
filter (available since v1.1.0) allows you to modify the COD fee applied to an order during the submission process.
It receives the cost
, which is automatically parsed from another plugin, and lets you customize it according to your needs.
function custom_cod_cost($cost)
{
return 2.5;
}
add_filter('simplerwc_cod_cost', 'custom_cod_cost', 10, 2);
Adding a fee to an Order
The simplerwc_order_fees
(available since v0.7.4) filter allows you to modify the fee closures that will be applied to the order during the order submission process. It accepts the OrderRequest
send by Simpler Checkout and any previously added fees to the order.
add_filter('simplerwc_order_fees', function (array $closures, OrderRequest $orderRequest): array
{
if (!($paymentMethod = $orderRequest->get_order()->get_payment_method())
|| $paymentMethod->getType() != PaymentMethod::COD) {
return $closures;
}
$availablePaymentGateways = WC()->payment_gateways()->get_available_payment_gateways();
$paymentGateway = $availablePaymentGateways[$orderRequest->get_order()->get_payment_method()->getId()] ?? null;
$closures[] = function () use ($paymentGateway, $paymentMethod) {
WC()->cart->add_fee(
$paymentMethod->getName() ?: $paymentGateway->title,
$paymentMethod->getTotalCents() != null ? Money::from_cents($paymentMethod->getTotalCents()) : $paymentGateway->settings['extra_fee']
);
};
return $closures;
}, 10 , 2);
Retrieving Invoice Details
When company invoices are enabled in your checkout, you can retrieve the company invoicing data and attach them to the order using the simplerwc_after_set_checkout_data
action.
add_action('simplerwc_after_set_checkout_data', 'save_simpler_invoice_data', 10, 2);
// supposing your invoice implementation adds data to the order metadata array
function save_simpler_invoice_data($order, $request)
{
/**
* @var \WC_Order $order
* @var \Simpler\Models\OrderRequest $request
* */
if (!$request->is_invoice()) {
return;
}
$details = $request->get_invoice_details();
$order->add_meta_data('tax_id', $details->get_tax_id(), true);
$order->add_meta_data('company_name', $details->get_company_name(), true);
$order->add_meta_data('company_address', $details->get_company_address(), true);
$order->add_meta_data('company_activity', $details->get_company_activity(), true);
$order->add_meta_data('tax_authority', $details->get_tax_authority(), true);
}