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.
Example: Removing a shipping rate from Simpler
add_filter(’simplerwc_shipping_rates’, function($methods) {
return array_filter($methods, function ($el) {
return $el->get_id() ≠ “my custom method”
});
});
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.
Example: Adding COD payment method to the quote
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);
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.
Example: Adding COD fee 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);
}
Updated about 2 months ago