Last active
November 25, 2015 18:12
-
-
Save WPprodigy/0826f54c0cddb80196b0 to your computer and use it in GitHub Desktop.
Show products from a category conditionally on the cart page
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 | |
function get_products_in_cart2() { | |
$cart_ids = array(); | |
foreach( WC()->cart->get_cart() as $cart_item_key => $values ) { | |
$cart_product = $values['data']; | |
$cart_ids[] = $cart_product->id; | |
} | |
return $cart_ids; | |
} | |
function get_categories_in_cart2( $cart_ids ) { | |
$cart_categories = array(); | |
foreach( $cart_ids as $id ) { | |
$products_categories = get_the_terms( $id, 'product_cat' ); | |
if ( ! empty( $products_categories ) ) { | |
// Loop through each product category and add it to our $cart_categories array | |
foreach ( $products_categories as $products_category ) { | |
$cart_categories[] = $products_category->slug; | |
} | |
} | |
} | |
return $cart_categories; | |
} | |
function check_for_category_in_cart2( $category, $cart_categories ) { | |
if ( in_array( $category, $cart_categories ) ) { | |
$category_in_cart = true; | |
} else { | |
$category_in_cart = false; | |
} | |
return $category_in_cart; | |
} | |
add_action( 'woocommerce_after_cart_table', 'wc_ninja_add_categories_to_cart2' ); | |
function wc_ninja_add_categories_to_cart2() { | |
$cart_categories = get_categories_in_cart2( get_products_in_cart2() ); | |
$floral_bunches_in_cart = check_for_category_in_cart2( 'floral-bunches', $cart_categories ); | |
$gifts_in_cart = check_for_category_in_cart2( 'gifts', $cart_categories ); | |
if ( $floral_bunches_in_cart && ! $gifts_in_cart ) { | |
echo do_shortcode( '[product_category category="gifts" per_page="15" columns="5" orderby="date" order="ASC"]' ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment