Skip to content

Instantly share code, notes, and snippets.

@anunay
Last active August 29, 2015 13:56
Show Gist options
  • Save anunay/8896208 to your computer and use it in GitHub Desktop.
Save anunay/8896208 to your computer and use it in GitHub Desktop.
WooCommerce Before Cart
<?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