Skip to content

Instantly share code, notes, and snippets.

@kartikparmar
Created September 30, 2018 10:39
Show Gist options
  • Save kartikparmar/8e9f4b2e1f93a0d582dae4b664d7379d to your computer and use it in GitHub Desktop.
Save kartikparmar/8e9f4b2e1f93a0d582dae4b664d7379d to your computer and use it in GitHub Desktop.
Set price to 0 for a perticular product.
<?php
function add_custom_price( $cart_object ) {
$cart_total = 25; // If cart total is more than this value.
$free_product_id = 6332; // Set price to 0 of this free product.
$carttotal = 0;
foreach ( $cart_object->cart_contents as $key => $value ) {
$_product = $value['data'];
if ( $_product->get_id() == $free_product_id ){
continue;
}
$carttotal += $value['line_total'];
}
if ( $carttotal >= $cart_total ) {
$custom_price = 0; // This will be your custome price
foreach ( $cart_object->cart_contents as $key => $value ) {
$_product = $value['data'];
if ( $_product->get_id() == $free_product_id ){
$value['data']->set_price( $custom_price );
}
}
}
}
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment