Skip to content

Instantly share code, notes, and snippets.

@gonzalesc
Created December 22, 2024 19:57
Show Gist options
  • Save gonzalesc/5337cdba6dc5754099a7ab16ce1853d9 to your computer and use it in GitHub Desktop.
Save gonzalesc/5337cdba6dc5754099a7ab16ce1853d9 to your computer and use it in GitHub Desktop.
Example plugin - Set mininum order in WooCommerce
<?php
/**
* @link https://www.letsgodev.com/
* @since 1.0.0
* @package Plugins/LetsGoDev/PluginExample
*
* Plugin Name: Set minimum order
* Plugin URI: https://www.letsgodev.com/
* Description: This plugin is an example
* Version: 1.0.0
* Author: Lets Go Dev
* Author URI: https://www.letsgodev.com/
* Developer: Alexander Gonzales
* Developer URI: https://vcard.gonzalesc.org/
* License: GPL-3.0+
* License URI: https://www.gnu.org/licenses/gpl-3.0.txt
* Text Domain: custom plugin, example plugin, create plugin
* Requires Plugins: woocommerce
* WP stable tag: 6.5.0
* WP requires at least: 6.5.0
* WP tested up to: 6.7.0
* WC requires at least: 9.2.0
* WC tested up to: 9.4.1
*/
add_action( 'woocommerce_check_cart_items', 'setMinimumOrder2' );
/**
* Set minimum order
* @return void
*/
function setMinimumOrder2(): void {
if ( ! is_cart() && ! is_checkout() ) {
return;
}
$limitSubtotal = 150;
// Check if subtotal
if ( WC()->cart->get_subtotal() > $limitSubtotal ) {
return;
}
$message = sprintf(
esc_html__(
'Remember that to finalize your purchase you must reach a minimum amount of %s.',
'letsgo'
),
wc_price( $limitSubtotal )
);
wc_add_notice( $message, 'error' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment