Skip to content

Instantly share code, notes, and snippets.

@yojance
Created February 24, 2015 02:32
Show Gist options
  • Save yojance/d484cbe19a6914e07e91 to your computer and use it in GitHub Desktop.
Save yojance/d484cbe19a6914e07e91 to your computer and use it in GitHub Desktop.
Functions required for the incentive program
<?php
/**
* Will extract the Variation ID if available otherwise it will get the Product ID
* @param $product Product
* @param bool $check_variations Whether or not to check for variation IDs
* @return mixed
*/
function get_id_from_product( $product, $check_variations = true ) {
// Are we taking variations into account?
if( $check_variations ) {
// Ternary Operator
// http://php.net/manual/en/language.operators.comparison.php
return ( isset( $product['variation_id'] )
&& ! empty( $product['variation_id'])
&& $product['variation_id'] != 0 )
? $product['variation_id']
: $product['product_id'];
} else {
// No variations, just need the product IDs
return $product['product_id'];
}
}
/**
* Checks the existence of a specific product in the cart
* @param $product_required The Product ID required to be in the cart
* @return bool
*/
function qualifies_basedon_specific_product( $product_required ) {
/*
* We only want to run this on the cart or checkout page
*/
if( is_cart() || is_checkout () ) {
foreach( WC()->cart->cart_contents as $key => $product_in_cart ) {
if( $product_required == get_id_from_product( $product_in_cart ) ) {
return true;
}
}
// Return false in case anything fails
return false;
}
}
/**
* Checks the cart for the weight required to qualify for the incentive
* @param $weight_required Weight Required
* @return bool
*/
function qualifies_basedon_weight( $weight_required ) {
/*
* We only want to run this on the cart or checkout page
*/
if( is_cart() || is_checkout () ) {
if( $weight_required >= WC()->cart->cart_contents_weight ) {
return true;
}
}
// Return false in case anything fails
return false;
}
/**
* Checks the cart for the Total excluding taxes
* @param $total_required
* @return bool
*/
function qualifies_basedon_cart_total( $total_required ) {
/*
* We only want to run this on the cart or checkout page
*/
if( is_cart() || is_checkout () ) {
if( WC()->cart->subtotal_ex_tax >= $total_required ) {
return true;
}
}
// Return false in case anything fails
return false;
}
/**
* Checks the cart to verify whether or not a product from a Category is in the cart
* @param $category Accepts the Product Category Name, ID, Slug or array of them
* @return bool
*/
function qualifies_basedon_product_category( $category ) {
foreach( WC()->cart->cart_contents as $key => $product_in_cart ) {
if( has_term( $category, 'product_cat', get_id_from_product( $product_in_cart, false ) ) ) {
return true;
}
}
// Return false in case anything fails
return false;
}
/**
* Adds a specific product to the cart
* @param $product_id Product to be added to the cart
*/
function add_incentive_to_cart( $product_id ) {
// Check the cart for this product
$cart_id = WC()->cart->generate_cart_id( $product_id );
$prod_in_cart = WC()->cart->find_product_in_cart( $cart_id );
// Add the product only if it's not in the cart already
if( ! $prod_in_cart ) {
WC()->cart->add_to_cart( $product_id );
}
}
/**
* Removes a specific product from the cart
* @param $product_id Product ID to be removed from the cart
*/
function remove_incentive_from_cart( $product_id ) {
$prod_unique_id = WC()->cart->generate_cart_id( $product_id );
// Remove it from the cart by un-setting it
unset( WC()->cart->cart_contents[$prod_unique_id] );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment