Search code examples
phpwordpresswoocommercecartcheckout

Make the same selection of product measurements in the cart appear on the checkout page in WooCommerce


This code multiplies the measures of the product and in the cart everything appears well, but on the payment page it simplifies it too much and I need them to appear on the payment page exactly as they appear in the cart.

What can I do to display the result on the checkout page?

add_filter( 'woocommerce_cart_item_quantity', 'change_cart_item_displayed_quantity', 10, 3 );
function change_cart_item_displayed_quantity( $product_quantity, $cart_item_key, $cart_item ) {
    $escribiunidades = 0;   

    $posareacubierta = strpos($cart_item['data'], "_area");
    $areacubierta = substr($cart_item['data'],$posareacubierta);
    $areacubierta = str_replace("\"","", $areacubierta);
    $areacubierta = str_replace("area","", $areacubierta);
    $areacubierta = str_replace("value","", $areacubierta);
    $areacubierta = str_replace(",","", $areacubierta);
    $areacubierta = str_replace("_","", $areacubierta);
    $areacubierta = str_replace(":","", $areacubierta);
    $areacubierta = str_replace("]","", $areacubierta);
    $areacubierta = str_replace("}","", $areacubierta);
    if (is_numeric($areacubierta)) {
        $areacubierta = $cart_item['quantity'] * $areacubierta;
        $product_quantity =  $product_quantity . " <font style='font-size:15px; padding-left: 20px; padding-right: 10px;'>CAJA(S)</font><br><font style='font-size:15px;'>(Total: " . number_format($areacubierta,2) . " m2)</font>";

        $escribiunidades = 1;
    }

    $poswidth = strpos($cart_item['data'], "width");
    $posheight = strpos($cart_item['data'], "height");
    $areamaterial = substr($cart_item['data'],$poswidth,($posheight-$poswidth));
    $areamaterial = str_replace("\"","", $areamaterial);
    $areamaterial = str_replace("width","", $areamaterial);
    $areamaterial = str_replace("value","", $areamaterial);
    $areamaterial = str_replace(",","", $areamaterial);
    $areamaterial = str_replace("_","", $areamaterial);
    $areamaterial = str_replace(":","", $areamaterial);
    $areamaterial = str_replace("]","", $areamaterial);
    $areamaterial = str_replace("}","", $areamaterial);

    if (is_numeric($areamaterial) && $escribiunidades == 0) {
        $areamaterial = $cart_item['quantity'] * $areamaterial;
        $product_quantity =  $product_quantity . "<font style='font-size:15px; margin-left:-25px; float:center !important;'>METRO(S) LINEAL(ES)</font><br><font style='font-size:15px;'>(Total: " . number_format($areamaterial,2) . " m2)</font>";

        $escribiunidades = 1;
    }   


    return $product_quantity;
}

Solution

    • To answer your question, to display this on the checkout page, you can use the woocommerce_checkout_cart_item_quantity hook
    // Display in checkout
    function filter_woocommerce_checkout_cart_item_quantity( $item_qty, $cart_item, $cart_item_key ) { 
    
        // Do some magic...
    
        return $item_qty;
    }
    add_filter( 'woocommerce_checkout_cart_item_quantity', 'filter_woocommerce_checkout_cart_item_quantity', 10, 3 );
    

    • Since I notice that you use a lot of different string functions in your current code, in this answer I show a simple representation of how to obtain the width and height of a product as a numerical value. I believe this is also the intention in your original code. By forumulating my answer differently I believe you can rewrite your original code in a simple way
    // Display in checkout
    function filter_woocommerce_checkout_cart_item_quantity( $item_qty, $cart_item, $cart_item_key ) { 
    
        // Get width & height
        $poswidth = (int) $cart_item['data']->get_width();
        $posheight = (int) $cart_item['data']->get_height();
        
        // Do a certain calculation
        $areamaterial = $poswidth * $posheight;
    
        // Condition
        if ( is_numeric( $areamaterial ) ) {
            $areamaterial = $cart_item['quantity'] * $areamaterial;
    
            // Notice the use of .= opposite =
            // This is adding to versus overwriting the original value
            $item_qty .= '<span style="display:block">METRO(S) LINEAL(ES) Total: "' . number_format( $areamaterial, 2 ) . '" m2)</span>';
        }
    
        return $item_qty;
    }
    add_filter( 'woocommerce_checkout_cart_item_quantity', 'filter_woocommerce_checkout_cart_item_quantity', 10, 3 );