add_filter( 'woocommerce_cart_item_name', 'add_opakowanie_item_under_cart_item', 10, 3 ); function add_opakowanie_item_under_cart_item( $product_name, $cart_item, $cart_item_key ) { ?> get_price()); // Check if the product is a variable product if ( $product->is_type( 'variable' ) ) { // Get the chosen variation $variation_id = $cart_item['variation_id']; $variation = wc_get_product($variation_id); // Get the variation price $variation_price = wc_price($variation->get_price()); $product_name .= ' - ' . $variation_price; } else { // If not a variable product, simply append the regular price $product_name .= ' - ' . $product_price; } $categories = get_the_terms( $product_id, 'product_cat' ); if ( $categories ) { foreach ( $categories as $category ) { if ( strtolower( $category->name ) === 'pizza' ) { // Append the HTML for "Opakowanie" item $product_name .= '

Opakowanie koszt + 2.00 zł
SUP + 0,31 zł

'; } if ( strtolower( $category->name ) === 'zapiekanki' ) { $product_name .= '

Opakowanie koszt + 1.00 zł
SUP + 0,31 zł

'; } if (strtolower($category->name) === 'makarony' || strtolower($category->name) === 'sałatki' || strtolower($category->name) === 'sosy' || strtolower($category->name) === 'słodkości') { $product_name .= '
SUP + 0,31 zł

'; } } } return $product_name; } // Recalculate cart totals to include additional cost for pizzas add_action( 'woocommerce_before_calculate_totals', 'add_pizza_item_cost' ); function add_pizza_item_cost( $cart ) { if ( is_admin() && ! defined( 'DOING_AJAX' ) ) { return; } // Iterate through cart items foreach ( $cart->get_cart() as $cart_item ) { // Get the product ID $product_id = $cart_item['product_id']; // Get product categories $categories = get_the_terms( $product_id, 'product_cat' ); // Check if the product belongs to the "pizza" category if ( $categories ) { foreach ( $categories as $category ) { if ( strtolower( $category->name ) === 'pizza' ) { // Adjust the price for pizza item $cart_item['data']->set_price( $cart_item['data']->get_price() + 2.31 ); // 2.31 break; } if ( strtolower( $category->name ) === 'zapiekanki' ) { // Adjust the price for pizza item $cart_item['data']->set_price( $cart_item['data']->get_price() + 1.31 ); // 1.31 break; } if (strtolower($category->name) === 'makarony' || strtolower($category->name) === 'sałatki' || strtolower($category->name) === 'sosy' || strtolower($category->name) === 'słodkości') { // Adjust the price for pizza item $cart_item['data']->set_price($cart_item['data']->get_price() + 0.31); // 1.31 break; } } } } } // Update the cart total dynamically add_filter( 'woocommerce_cart_totals_order_total', 'update_cart_total', 10, 1 ); function update_cart_total( $total ) { // Get the cart object $cart = WC()->cart; // Initialize total $cart_total = $cart->cart_contents_total + $cart->shipping_total + $cart->tax_total; // Calculate subtotal for pizzas $pizza_count = 0; // Initialize pizza count $zapiekanki_count = 0; foreach ( $cart->get_cart() as $cart_item ) { // Get product ID $product_id = $cart_item['product_id']; // Get product categories $categories = get_the_terms( $product_id, 'product_cat' ); // Check if the product belongs to the "pizza" category if ( $categories ) { foreach ( $categories as $category ) { if ( strtolower( $category->name ) === 'pizza' ) { // Increment pizza count $pizza_count += $cart_item['quantity']; } if ( strtolower( $category->name ) === 'zapiekanki' ) { // Increment pizza count $zapiekanki_count += $cart_item['quantity']; } } } } // Calculate subtotal for pizzas $pizza_subtotal = $pizza_count * 2; // Add +2 for each pizza item $zapiekanki_subtotal = $zapiekanki_count * 2; // Add extra cost for pizzas to the main total if ( $pizza_subtotal > 0 ) { $cart_total += $pizza_subtotal; // Add the extra cost for pizzas to the main total } if ( $zapiekanki_subtotal > 0 ) { $cart_total += $zapiekanki_subtotal; // Add the extra cost for pizzas to the main total } return wc_price( $cart_total ); }