-
-
Save webdados/15ae368bc5f1ee3b4a04f3b111052947 to your computer and use it in GitHub Desktop.
Apply your own rules for order approval on the "Simple WooCommerce Order Approval" plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* https://ptwooplugins.com/product/simple-woocommerce-order-approval/ */ | |
/* Add this to you (child-)theme functions.php file, on a (mu-)plugin or with a plugin like Code Snippets https://wordpress.org/plugins/code-snippets/ */ | |
/** | |
* In this case, we'll only deal with $needs_approval being false, as we want to apply our rules only if the plugin rules didn't applied, which is the most common scenario | |
* | |
* @param bool $needs_approval As defined by the plugin rules. | |
* @param object $cart_or_order WC_Cart or WC_Order. | |
* @return bool | |
*/ | |
function my_swoa_needs_approval( $needs_approval, $cart_or_order ) { | |
if ( ! $needs_approval ) { | |
// First of all, you will need to check if you're looking at the cart or at an order, and only then apply your logic | |
if ( is_a( $cart_or_order, 'WC_Cart' ) ) { | |
// We're checking a cart and can return true depending on our own rules | |
if ( $whatever ) { | |
return true; | |
} | |
} elseif ( is_a( $cart_or_order, 'WC_Order' ) ) { | |
// We're checking an order and can return true depending on our own rules | |
if ( $whatever ) { | |
return true; | |
} | |
} | |
} | |
return $needs_approval; | |
} | |
add_filter( 'swoa_needs_approval', 'my_swoa_needs_approval', 10, 2 ); | |
/** | |
* In this case, we'll make an exception if the applied rule is 'product_on_backorder' for a specific product id, which we always want to sell without approval | |
* | |
* @param bool $needs_approval As defined by the plugin rules. | |
* @param object $cart_or_order WC_Cart or WC_Order. | |
* @param string $applied_rule The plugin applied rule. | |
* @param mixed $object_id The object id the rule was applied because of. | |
* @return bool | |
*/ | |
function my_other_swoa_needs_approval( $needs_approval, $cart_or_order, $applied_rule, $object_id ) { | |
if ( $needs_approval && $applied_rule === 'product_on_backorder' && $object_id === 1904 ) { | |
return false; | |
} | |
return $needs_approval; | |
} | |
add_filter( 'swoa_needs_approval', 'my_other_swoa_needs_approval', 10, 4 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment