Skip to content

Instantly share code, notes, and snippets.

@MonteLogic
Last active January 29, 2022 01:18
Show Gist options
  • Select an option

  • Save MonteLogic/ae900cf3c7052dec15ea81c54992725d to your computer and use it in GitHub Desktop.

Select an option

Save MonteLogic/ae900cf3c7052dec15ea81c54992725d to your computer and use it in GitHub Desktop.
2 for each loops that matches a a course that needs to have a book.

This logic matches a book with a course. This PHP logic is installed on the checkout page and it fires in notice if the user doesn't have the book that goes with the course. I think that it's bloated maybe somebody else can help me but as it is right now i t works fine but it's just fine.

<?php
$array2 = array("a2" => "Second", "a3" => "Third");
$array2 = array("a2" => array("Newarray" => "Value of array"));
//var_dump($array2);
//echo "<br>";
$products_in_cart = array();
$cross_ids_in_cart = array();
$cart_item_details = array();
$add_these_books = array();
$cartHasCrossIds = false;
$count;
// Get size of cart
$countCart = WC()->cart->cart_contents_count;
$countCartTwo = WC()->cart->get_cart();
//echo sizeof($countCartTwo) . "<br>";
$counterInc = sizeof($countCartTwo);
// echo $countCart . "<br>";
$goodCounter = 0;
$staleCounter = 0;
$i = 0;
foreach ( WC()->cart->get_cart() as $cart_item ) :
$CartItemId = $cart_item['data']->get_id();
$CartItemQuantity = $cart_item['quantity'];
// Find cross id's
$findCrossIdsInCart = get_post_meta( $CartItemId, '_crosssell_ids' );
if(isset($findCrossIdsInCart[0][0])){
$findCrossIdsInCart = $findCrossIdsInCart[0][0];
}
// echo "Cart Item Quantity= " . $CartItemQuantity . " For: " . $CartItemId . "<br>";
// echo "Thus, " . $CartItemId . " Needs ". $CartItemQuantity . " From: " . $findCrossIdsInCart;
if( $findCrossIdsInCart){
$staleCounter = $goodCounter;
foreach ( WC()->cart->get_cart() as $cart_item_two ) :
// echo "<br> 505";
$CartItemIdTwo = $cart_item_two['data']->get_id();
$CartItemQuantityTwo = $cart_item_two['quantity'];
if($findCrossIdsInCart == $CartItemIdTwo && $CartItemQuantityTwo == $CartItemQuantity ){
// echo "Your golden for: " . $CartItemId;
$goodCounter++;
}
endforeach;
// at the end of this foreach we can conclude about the content it was iterating upon.
// After this, we have compared all the books with the rest of the cart.
if($staleCounter == $goodCounter){
$add_these_books[$i] = $findCrossIdsInCart;
//echo "504" . $add_these_books[$i];
}
$goodCounter = $staleCounter;
}
// echo "<br>Good counter = " . $goodCounter;
endforeach
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment