Skip to content

Instantly share code, notes, and snippets.

@bolderelements
Created October 3, 2018 02:52
Show Gist options
  • Save bolderelements/3bd124d6eaa9a86616fde8c16f5f7578 to your computer and use it in GitHub Desktop.
Save bolderelements/3bd124d6eaa9a86616fde8c16f5f7578 to your computer and use it in GitHub Desktop.
Change a Product's True Quantity for Calculating Shipping
/**
* Adjust the calculated quantity of items in the cart based on a specific product
* Code snippets should be added to the functions.php file of your child theme
*
* @return array
*/
function adjust_item_qty_for_shipping( $data, $items ) {
// the Product ID of the product that needs to be adjusted
$item_to_change = 13;
// the multiplier for adjusting the quantity
$multiplier = 5;
// exit if no valid items are provided
if( ! is_array( $items ) ) return $data;
// check each product in the cart
$qty_change = 0;
foreach( $items as $key => $item ) {
if( $item['product_id'] == $item_to_change || $item['variation_id'] ) {
$qty_change = $item['quantity'] * $multiplier - $item['quantity'];
}
}
// adjust the order's quantity
$data['quantity'] = $data['quantity'] + $qty_change;
return $data;
}
add_action( 'betrs_calculated_totals-per_order', 'adjust_item_qty_for_shipping', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment