Skip to content

Instantly share code, notes, and snippets.

@growdev
Created July 26, 2021 23:34
Show Gist options
  • Save growdev/454447f0f84e65160380e95a6a74ca06 to your computer and use it in GitHub Desktop.
Save growdev/454447f0f84e65160380e95a6a74ca06 to your computer and use it in GitHub Desktop.
Check Min/Max quantity for line items and stop save if it is over.
<?php
/**
* Hooked into `jgtb_allow_edit_qty_for_subscription`, this will check
* the new quantity of items in the subscription and allow/disallow the
* edit.
*
* @param string $allowed 'yes' or 'no'.
* @param \WC_Subscription $subscription Instance of current subscription.
* @return string 'yes' or 'no'.
*/
function sp_custom_allow_edit_quantity( $allowed, $subscription ) {
if ( 'no' === $allowed ) {
return $allowed;
}
foreach ( $subscription_items as $item_id => $item ) {
$old_qty = $item->get_quantity();
// Get new quantity of item from request
$new_qty = ( isset( $_REQUEST[ 'new_quantity_' . $item_id ] ) ) ? $_REQUEST[ 'new_quantity_' . $item_id ] : null;
if ( null !== $new_qty && $new_qty != $old_qty ) {
// TODO: Check product's Min/Max quantity and if the new quantity is over then set $allowed to false.
$max_qty = '';
if ( $new_qty > $max_qty ) {
$allowed = false;
}
}
return $allowed
}
add_filter( 'jgtb_allow_edit_qty_for_subscription', 'sp_custom_allow_edit_quantity', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment