Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_filter('woocommerce_add_cart_item_data', 'custom_codedropz_change_price', 100, 2 );
- add_filter('woocommerce_before_calculate_totals','set_codedropz_calculate_price', 20, 1 );
- function custom_codedropz_change_price( $cart_item_data, $product_id ){
- // Bail early
- if( ! isset( $cart_item_data['dnd-wc-file-upload'] ) ){
- return $cart_item_data;
- }
- // Get total no. of files
- $total_files = count( $cart_item_data['dnd-wc-file-upload'] );
- // Set Custom Price
- if( $total_files > 1 ){
- $product = wc_get_product($product_id);
- $price = ( $product->is_on_sale() ? $product->get_sale_price() : $product->get_regular_price() );
- $cart_item_data['new_price'] = ( $price * $total_files );
- }
- return $cart_item_data;
- }
- function set_codedropz_calculate_price( $cart ){
- // Bail early
- if ( is_admin() && ! defined( 'DOING_AJAX' ) ){
- return;
- }
- // Adjust Price only if new_price is set
- foreach ( $cart->get_cart() as $cart_item ) {
- if( isset( $cart_item['new_price'] ) ){
- $cart_item['data']->set_price( $cart_item['new_price'] );
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement