Last active
September 23, 2020 14:22
-
-
Save Preciousomonze/4fc8197851991bb970016d3a573bb706 to your computer and use it in GitHub Desktop.
This MU-Plugin helps sort Predefined Products to behave a certain way when they're added to the cart. Requires WooCommerce to work. Use at your risk, enjoy! I mean, what's life without taking risks? π
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 | |
/** | |
* WooCommerce Cart Stand-alone Custom Sorting. | |
* | |
* @package pekky-wc-stand-alone-force-sells-sorting | |
* @author Precious Omonzejele (CodeXplorer π€Ύπ½ββοΈπ₯π¦π€‘) | |
* | |
* @wordpress-plugin | |
* Plugin Name: WooCommerce Cart Stand-alone Custom Sorting | |
* Plugin URI: https://gist.github.com/Preciousomonze/f41ebbd43b751d00e86fa4207e88413f | |
* Description: This MU-Plugin helps sort Predefined Products to behave a certain way when they're added to the cart. Requires WooCommerce to work. | |
* Author: Precious Omonzejele (CodeXplorer π€Ύπ½ββοΈπ₯π¦π€‘) | |
* Author URI: https://codexplorer.ninja | |
* Version: 1.0.0 | |
* Requires at least: 5.0 | |
* Tested up to: 5.4 | |
* WC requires at least: 4.0 | |
* WC tested up to: 4.5 | |
*/ | |
class Pekky_WC_Stand_Alone_Cart_Sorting { | |
// The grouped Products. | |
/** | |
* Stand Alone Product id(s) | |
* | |
* Sample values: array( 23,45,43 ); | |
* | |
* @var array | |
*/ | |
protected static $wc_stand_alone_products = array(); | |
/** | |
* Boostrap and shoot. | |
*/ | |
public static function init() { | |
// Manipulate when added to cart. | |
add_filter( 'woocommerce_add_to_cart_validation', array( __CLASS__, 'sort_product_add_to_cart_validation' ), 20, 3 ); | |
} | |
/** | |
* Cart Item adding validation | |
* | |
* Sorts a product being added to cart, | |
* checking if its standalone | |
* | |
* @param bool $passed return true or not. | |
* @param int $product_id Product being added to cart. | |
* @param int $quantity quantity of product. | |
* @return bool | |
*/ | |
public static function sort_product_add_to_cart_validation( $passed, $product_id, $quantity ) { | |
$stand_alone_products = self::$wc_stand_alone_products; | |
if ( in_array( $product_id, $stand_alone_products, true ) ) { | |
// The standalone product was found. | |
self::pekky_sort_stand_alone_products( $product_id, $stand_alone_products, true ); | |
} else { | |
// Another product, check if a standalone product is already in the cart. | |
foreach ( $stand_alone_products as $p_id ) { | |
// Is this standalone product in the cart? | |
if ( in_array( $p_id, array_column( WC()->cart->get_cart(), 'product_id' ), true ) ) { | |
// Prevent other products from being added and reserve runtime abeg :). | |
wc_add_notice( | |
__( 'This Product cannot be addede because the current product(s) in the cart are standalone.', 'woocommerce' ), | |
'error' | |
); | |
$passed = false; | |
break; | |
} | |
} | |
} | |
return $passed; | |
} | |
// Inner functions to help run some parole. | |
/** | |
* Sorts the stand alone products. | |
* | |
* @param int $product_id Standalone Product Id. | |
* @param array $stand_alone_products List of standalone products. | |
* @param bool $add_to_cart (optional) true, add to cart, if false, removes, default is true. | |
* @return bool false if empty field, true otherwise. | |
*/ | |
private static function pekky_sort_stand_alone_products( $product_id, $stand_alone_products, $add_to_cart = true ) { | |
if ( empty( $product_id ) || ( empty( $stand_alone_products ) || ! is_array( $stand_alone_products ) ) ) { | |
return false; | |
} | |
$wc_cart = WC()->cart; | |
$cart_func = 'remove_cart_item'; | |
if ( $add_to_cart ) { | |
$cart_func = 'add_to_cart'; | |
// Empty cart cause a standalone program is being added. | |
$wc_cart->empty_cart(); | |
} | |
$p_id = ( $add_to_cart ? $product_id : $wc_cart->generate_cart_id( $product_id ) ); | |
// var_dump("<br><br>".$cart_func); | |
// var_dump("<br>product_id:".$product_id."<br>"); | |
// var_dump( "<br>function executed: "); | |
if ( ! $add_to_cart ) { | |
$wc_cart->{"{$cart_func}"}( $p_id ); | |
} | |
return true; | |
} | |
} | |
// Launch! | |
Pekky_WC_Stand_Alone_Cart_Sorting::init(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment