Last active
August 29, 2015 13:56
-
-
Save anunay/8896208 to your computer and use it in GitHub Desktop.
WooCommerce Before Cart
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
<?php | |
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' ); | |
function add_custom_price( $cart_object ) { | |
$custom_price = 0; // This will be your custome price | |
//Your Custom Price Forumula goes here | |
$totalWeight = 50; // you can this variable value from post/get or cookie etc. | |
$unitWeight = 8; //8lbs default unit for basic unit price; | |
$unitPricePerWeight = (float) 0.44; //your unit price per 8lbs weight; | |
$noOfOrderDays = 10; // you can populate this varaible value from post/get/cookie or session. | |
$custom_price = (float) ($totalWeight/$unitWeight) * $unitPricePerWeight * $noOfOrderDays; | |
$target_product_id = 12 // you conditional product ID | |
foreach ( $cart_object->cart_contents as $key => $value ) { | |
if ( $value['product_id'] == $target_product_id ) { | |
$value['data']->price = $custom_price; | |
} | |
//$value['data']->price = $custom_price; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment