If you’re in Sri Lanka and running a WordPress e-commerce store via WooCommerce, by now you might have integrated Mintpay as a payment option for your customers or if you are from a web development agency, you might have worked on e-commerce websites that require this payment option that allows your client’s customers to buy now and pay later.
You might have a requirement to enable Mintpay, only for specific categories on the WooCommerce website. By default, Mintpay does not allow this, however, we can write a couple of PHP functions to achieve this functionality along with some CSS.
The PHP code below is something that I wrote that would initially hide Mintpay from the website and enable it based on the categories provided in the code below.
You can paste the following PHP code into your child theme’s function.php
file. NOTE: you should change the categories “mens
” and “womens
” based on your requirements and categories.
function load_custom_css_for_category()
{
if (!class_exists('WooCommerce')) return;
if (!is_product()) {
// Get the list of available payment gateways
$available_gateways = WC()->payment_gateways->get_available_payment_gateways();
// Check if 'mintpay' is in the list of available gateways
if (!isset($available_gateways['mintpay'])) {
// 'mintpay' is available, enqueue the custom CSS file
wp_enqueue_style('custom-mintpay-css', get_stylesheet_directory_uri() . '/inc/css/mintpay-custom-css.css');
}
}
// Check if we are on a single product page
if (is_product()) {
// Get the product categories for the current product
$product_categories = get_the_terms(get_the_ID(), 'product_cat');
// Define the categories to exclude
$allowed_categories = array('mens', 'womens');
// Flag to check if the product is in an excluded category
$is_allowed_category = false;
// Check if the current product is in an excluded category
if ($product_categories && !is_wp_error($product_categories)) {
foreach ($product_categories as $product_category) {
// Check if the category name is in the excluded categories array
if (in_array(strtolower($product_category->name), $allowed_categories)) {
$is_allowed_category = true;
break; // Exit the loop if an excluded category is found
}
}
}
// If the product is not in an excluded category, enqueue the custom CSS file
if (!$is_allowed_category || get_woocommerce_currency() != 'LKR') {
wp_enqueue_style('mintpay-custom-css', get_stylesheet_directory_uri() . '/inc/css/mintpay-custom-css.css');
}
}
if (!is_product()) {
if (get_woocommerce_currency() != 'LKR') {
wp_enqueue_style('mintpay-custom-css', get_stylesheet_directory_uri() . '/inc/css/mintpay-custom-css.css');
}
}
}
add_action('wp_enqueue_scripts', 'load_custom_css_for_category');
// Add a filter to modify available payment gateways on the cart and checkout pages
add_filter('woocommerce_available_payment_gateways', 'disable_mintpay_gateway');
function disable_mintpay_gateway($available_gateways)
{
// Check if we are on the cart or checkout page
if (is_cart() || is_checkout() && get_woocommerce_currency() === 'LKR') {
// Check if 'Mintpay' is in the list of available gateways
if (isset($available_gateways['mintpay'])) {
// Check if the cart contains allowed categories products
if (cart_contains_only_allowed_categories_products() && get_woocommerce_currency() === 'LKR') {
// 'Mintpay' is available and cart contains only allowed categories products,
// so we keep the gateway enabled.
} else if (cart_contains_allowed_categories_products() && get_woocommerce_currency() === 'LKR') {
// Remove the 'Mintpay' gateway from the available gateways
unset($available_gateways['mintpay']);
// Display a notice message on the cart and checkout pages
add_action('woocommerce_before_cart', 'display_allowed_categories_only_notice');
add_action('woocommerce_before_checkout_form', 'display_allowed_categories_only_notice');
} else {
// Remove the 'Mintpay' gateway from the available gateways
unset($available_gateways['mintpay']);
}
}
} else {
unset($available_gateways['mintpay']);
}
return $available_gateways;
}
// Function to check if the cart contains allowed categories products
function cart_contains_allowed_categories_products()
{
// Initialize a flag
$has_allowed_category = false;
// Get the cart contents
$cart = WC()->cart->get_cart();
// Loop through the cart items and check if any are allowed categories products
foreach ($cart as $cart_item) {
$product = wc_get_product($cart_item['product_id']);
if ($product && has_term(array('mens', 'womens'), 'product_cat', $product->get_id())) {
$has_allowed_category = true;
break; // Exit the loop if a allowed categories product is found
}
}
return $has_allowed_category;
}
// Function to check if the cart contains only allowed categories products
function cart_contains_only_allowed_categories_products()
{
// Get the cart contents
$cart = WC()->cart->get_cart();
// Loop through the cart items and check if any are not allowed categories products
foreach ($cart as $cart_item) {
$product = wc_get_product($cart_item['product_id']);
if (!$product || !has_term(array('mens', 'womens'), 'product_cat', $product->get_id())) {
return false; // Exit and return false if a non-allowed categories product is found
}
}
return true; // All items in the cart are allowed categories products
}
// Function to display a notice for products other than allowed categories
function display_allowed_categories_only_notice()
{
// Define your custom class name
$custom_class = 'custom-notice';
wc_print_notice(__('Please note that only [mention the categories] products are eligible for payment with Mintpay. Kindly check out the supported brands separately if you wish to proceed with Mintpay.', 'woocommerce'), 'error');
}
Once you’re done adding and customizing the above code, simply paste the following CSS into the same child theme’s directory. I prefer maintaining the files in a beautiful folder structure. Please paste the following CSS into the following path, if you do not have one, I would suggest creating the path or updating the CSS file’s path in the PHP code above.
/inc/css/mintpay-custom-css.css
/*
* Author: Minhaz Irphan Mohamed (www.minhazimohamed.com)
*/
.product-price-installments-not-in-variation-single, .mintpay-product-price-installment-in-variation, .mintpay-checkout-price-installment, .product-price-installments-not-in-variation {
display: none !important;
}
.related-products>.product-price-installments-not-in-variation {
display: none !important;
}
That’s it! Now you have Mintpay working for certain products and categories on your WooCommerce store. Leave a comment below if you have any questions and if you found this article helpful, please share it. Cheers.
Leave a Reply