autoship_add_cart_item (function)

( src/cart/php) Adjust the price of new items being added to the cart based on their schedule, so it only fires if the item being aded is NEW and doesn't exist already.

NOTE This filters data after autoship_add_cart_item_data() function, so Schedule data has already been attached / defaulted.

Function

/**
 * Adjust the price of new items being added to the cart based on their schedule
 * so it only fires if the item being aded is NEW and doesn't exist already.
 *
 * NOTE This filters data after @see autoship_add_cart_item_data() so Schedule
 * data has already been attached / defaulted.
 *
 * @param array  $cart_item_data The current cart item's data array.
 * @param string $cart_item_key The current cart item's hash key
 * @param array  $schedule_data Optional. Optional Schedule Data to re-assign to the item
 *
 * @return array The updated Cart item Data
 */
function autoship_add_cart_item( $cart_item_data, $cart_item_key, $schedule_data = NULL ) {

	$product_id    = $cart_item_data['product_id'];
	$variation_id  = $cart_item_data['variation_id'];
	$quantity      = $cart_item_data['quantity'];

	// Check if Schedule Data was supplied and if so refresh the current items data
	if ( isset( $schedule_data ) && is_array( $schedule_data ) )
	$cart_item_data = autoship_update_cart_item_data( $cart_item_data, $product_id, $variation_id, $quantity, $schedule_data );

	// No autoship schedule bail
	$schedule_values = autoship_get_item_data_schedule_values( $cart_item_data );
	if ( !autoship_item_data_has_valid_schedule( $schedule_values ) )
	return $cart_item_data;

	// Otherwise let's get the schedule data so we can calculate the price in case
	// there's a checkout price
	extract( $schedule_values );

	// Set / adjust for the scheduled item if we need to
	$checkout_price = autoship_get_product_checkout_price( $variation_id ? $variation_id : $product_id, $autoship_frequency_type, $autoship_frequency );

	if ( !empty( $checkout_price ) )
	$cart_item_data['data']->set_price( $checkout_price );

	return $cart_item_data;

}

Related