autoship_cart_has_valid_autoship_items (function)

(src/cart.php) This function checks the cart for items selected for Autoship & returns a boolean. It is used to add the Autoship Free Shipping Option in the cart and checkout, and can be used anytime needing to check the cart for items selected for Autoship.

Example: Hide PayPal Checkout Option When Autoship Items are in the Cart

Example: Hide Amazon Pay When Autoship Items In Cart

Example: Display Autoship Supported Gateway Only When Autoship Items in Cart

Code

/**
 * Checks the cart to see if it's a valid
 * Autoship cart.  By default just one Autoship item
 * Validates the cart.
 *
 * NOTE Developers can override the required number of Autoship items
 * to make a cart valid using {@see autoship_valid_cart_item_count}
 * NOTE Developers can override the required Autoship frequency type
 * required for an item to be counted using {@see autoship_valid_cart_item_frequency}
 * NOTE Developers can override the required minimum total number of
 * Valid Autoship items in cart to be valid using {@see autoship_valid_cart_total_min_required}
 *
 * @return bool True if the requirement is met. False if not.
 */
function autoship_cart_has_valid_autoship_items(){

  $cart = WC()->cart;

  // Don't bother if empty or if being called pre-wp loaded
	if ( empty( $cart ) || !did_action( 'wp_loaded' ) )
	return false;

  // Developers can override the required frequency to be valid.
  $min_required_frequency = apply_filters( 'autoship_valid_cart_item_min_frequency', 1 );

  // Developers can override the required frequency type to be valid.
  // By default any type is ok.
  $required_frequency_type = apply_filters( 'autoship_valid_cart_item_frequency', '' );

  // Loop through the cart checking for Autoship frequency.
	$autoship_items_count = 0;
	foreach ( $cart->get_cart() as $item ) {

		if ( autoship_item_data_has_valid_schedule( $item, $min_required_frequency, $required_frequency_type ) )
		$autoship_items_count++;

  }

	return (bool) apply_filters( 'autoship_valid_cart_total_min_required', $autoship_items_count );

}

Example:

function detect_autoship_items(){
	return autoship_cart_has_valid_autoship_items() ? print_r("<h3>Autoship Items Detected</h3>") : print_r("<h3>No Autoship Items</h3>");
  }
  add_action( 'woocommerce_before_cart_contents', 'detect_autoship_items', 10, 1 );

Still need help? Contact Us Contact Us