Last active
July 12, 2021 16:22
-
-
Save jorpdesigns/1d1931973d37a96b5d4f1a254b0a46df to your computer and use it in GitHub Desktop.
Snippet to set price of items in WooCommerce cart
This file contains hidden or 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', 'custom_cart_price' ); | |
function custom_cart_price( $cart_object ) { | |
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) | |
return; | |
foreach ( $cart_object->get_cart() as $key => $value ) { | |
// CHANGE PRICE FOR ALL CART ITEMS TO 10 | |
$value['data']->set_price( 10 ); | |
// CHANGE PRICE FOR PRODUCT ID 55 TO 10 | |
if ( $value['product_id'] == 55 ){ // Replace with your own product id | |
$value['data']->set_price( 10 ); | |
} | |
// CHANGE PRICE FOR PRODUCT IN CATEGORY ID 25 | |
if ( in_array( 25, $value['data']->get_category_ids() ) ){ // Replace with your own category id | |
$value['data']->set_price( 10 ); | |
} | |
// CHANGE PRICE TO 10 IF QUANTITY IS MORE THAN 5 | |
if ( $value['quantity'] > 5 ){ // Replace with your own condition | |
$value['data']->set_price( 10 ); | |
} | |
// CHANGE PRICE IF PRODUCT PRICE IS ABOVE 100 | |
if ( WC()->cart->get_product_price( $value['data'] ) > 100 ){ // Replace with your own price | |
$value['data']->set_price( 10 ); | |
} | |
// CHANGE PRICE TO 10 FOR ITEM THAT HAS CUSTOM META | |
if ( isset( $value['custom_meta'] ) ) { // Replace with your own custom meta | |
$value['data']->set_price( 10 ); | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment