Created
October 3, 2018 02:52
-
-
Save bolderelements/3bd124d6eaa9a86616fde8c16f5f7578 to your computer and use it in GitHub Desktop.
Change a Product's True Quantity for Calculating Shipping
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
/** | |
* 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