0

I need to remove an item from an array if that has a product which has a product_type bundle. I was able to recreate my array without the excluded products but I can't override the original array with it. I have tried also unset, but the $key value always just hodl the flag not the actual index to the element.

Can someone point me to the right direction?

My code:

add_filter('wc_szamlazz_xml','wc_szamlazz_xml_product_bundle_torlese',10,2);

/* 
https://www.php.net/manual/en/class.simplexmlelement.php#112420
*/
function wc_szamlazz_xml_product_bundle_torlese($xml,$order) {

    $elementsToKeep = array();

    error_log("xml->tetelek ARRAY:");
    error_log(print_r($xml->tetelek, true)); 

    foreach($xml->tetelek->tetel as $tetel) {
        
        //error_log("KEY: " . $key );

        if ($tetel->azonosito){
            $product_id = iconic_get_product_id_by_sku($tetel->azonosito);

            $product = wc_get_product( $product_id ); 
            $product_type = $product->get_type();


            /* error_log("Product_ID: " . $product_id);
            error_log("Product_type: " . $product_type);

            error_log("NEV:" . $tetel->megnevezes);
            error_log(print_r($tetel, true)); */

            if ($product_type != "bundle"){
                $elementsToKeep[] = $tetel;
            }
        } else {
            $elementsToKeep[] = $tetel;
        }
    }

    error_log("elementsToKeep ARRAY:");
    error_log(print_r($elementsToKeep, true)); 

    foreach($xml->tetelek->tetel as $tetel) {
        foreach ($elementsToKeep as $uj_elem){
            $tetel = $uj_elem;
        }
    }

    //$xml->tetelek->tetel = $elementsToKeep;

    error_log("xml->tetelek ARRAY:");
    error_log(print_r($xml->tetelek, true)); 

    return $xml;
}

Original array with all the values:

SimpleXMLElement Object
(
    [tetel] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [megnevezes] => Villanypásztor szett kutyáknak, macskáknak 30m hosszú kerítésekhez gyűrűszigetelőkkel teszt
                    [azonosito] => vpszettkutyszig-30-bundle
                    [mennyiseg] => 1
                    [mennyisegiEgyseg] => SimpleXMLElement Object
                        (
                        )

                    [nettoEgysegar] => 0
                    [afakulcs] => 0
                    [nettoErtek] => 0
                    [afaErtek] => 0
                    [bruttoErtek] => 0
                    [megjegyzes] => SimpleXMLElement Object
                        (
                        )

                )

            [1] => SimpleXMLElement Object
                (
                    [megnevezes] => Fencee Mini M03 Villanypásztor Készülék 0,3 J
                    [azonosito] => fenceeminiM03
                    [mennyiseg] => 1
                    [mennyisegiEgyseg] => SimpleXMLElement Object
                        (
                        )

                    [nettoEgysegar] => 22826.77
                    [afakulcs] => 27
                    [nettoErtek] => 22826.77
                    [afaErtek] => 6163
                    [bruttoErtek] => 28989.77
                    [megjegyzes] => SimpleXMLElement Object
                        (
                        )

                )

            [2] => SimpleXMLElement Object
                (
                    [megnevezes] => Farmline Villanypásztor Gyűrűszigetelő, 1 db,
                    [azonosito] => farmlinegyuruszig
                    [mennyiseg] => 25
                    [mennyisegiEgyseg] => SimpleXMLElement Object
                        (
                        )

                    [nettoEgysegar] => 51.1812
                    [afakulcs] => 27
                    [nettoErtek] => 1279.53
                    [afaErtek] => 345
                    [bruttoErtek] => 1624.53
                    [megjegyzes] => SimpleXMLElement Object
                        (
                        )

                )

            [3] => SimpleXMLElement Object
                (
                    [megnevezes] => Villanypásztor Földelő rúd profil, hossza 100 cm, horganyzott
                    [azonosito] => foldrud100
                    [mennyiseg] => 1
                    [mennyisegiEgyseg] => SimpleXMLElement Object
                        (
                        )

                    [nettoEgysegar] => 3141.73
                    [afakulcs] => 27
                    [nettoErtek] => 3141.73
                    [afaErtek] => 848
                    [bruttoErtek] => 3989.73
                    [megjegyzes] => SimpleXMLElement Object
                        (
                        )

                )

            [4] => SimpleXMLElement Object
                (
                    [megnevezes] => Fence Guard ECONOMY MINI Villanypásztor Vezeték  100 m, 2mm, 3 vezetőszál - 40Kg - 14,4 Ω/m
                    [azonosito] => economymini100m
                    [mennyiseg] => 1
                    [mennyisegiEgyseg] => SimpleXMLElement Object
                        (
                        )

                    [nettoEgysegar] => 1488.19
                    [afakulcs] => 27
                    [nettoErtek] => 1488.19
                    [afaErtek] => 402
                    [bruttoErtek] => 1890.19
                    [megjegyzes] => SimpleXMLElement Object
                        (
                        )

                )

            [5] => SimpleXMLElement Object
                (
                    [megnevezes] => MPL Házhozszállítás
                    [mennyiseg] => 1
                    [mennyisegiEgyseg] => SimpleXMLElement Object
                        (
                        )

                    [nettoEgysegar] => 394
                    [afakulcs] => 27
                    [nettoErtek] => 394
                    [afaErtek] => 106
                    [bruttoErtek] => 500
                    [megjegyzes] => SimpleXMLElement Object
                        (
                        )

                )

        )

)

In this case I need to remove the element with the azonosito = vpszettkutyszig-30-bundle which is working.

Array
(
    [0] => SimpleXMLElement Object
        (
            [megnevezes] => Fencee Mini M03 Villanypásztor Készülék 0,3 J
            [azonosito] => fenceeminiM03
            [mennyiseg] => 1
            [mennyisegiEgyseg] => SimpleXMLElement Object
                (
                )

            [nettoEgysegar] => 22826.77
            [afakulcs] => 27
            [nettoErtek] => 22826.77
            [afaErtek] => 6163
            [bruttoErtek] => 28989.77
            [megjegyzes] => SimpleXMLElement Object
                (
                )

        )

    [1] => SimpleXMLElement Object
        (
            [megnevezes] => Farmline Villanypásztor Gyűrűszigetelő, 1 db,
            [azonosito] => farmlinegyuruszig
            [mennyiseg] => 25
            [mennyisegiEgyseg] => SimpleXMLElement Object
                (
                )

            [nettoEgysegar] => 51.1812
            [afakulcs] => 27
            [nettoErtek] => 1279.53
            [afaErtek] => 345
            [bruttoErtek] => 1624.53
            [megjegyzes] => SimpleXMLElement Object
                (
                )

        )

    [2] => SimpleXMLElement Object
        (
            [megnevezes] => Villanypásztor Földelő rúd profil, hossza 100 cm, horganyzott
            [azonosito] => foldrud100
            [mennyiseg] => 1
            [mennyisegiEgyseg] => SimpleXMLElement Object
                (
                )

            [nettoEgysegar] => 3141.73
            [afakulcs] => 27
            [nettoErtek] => 3141.73
            [afaErtek] => 848
            [bruttoErtek] => 3989.73
            [megjegyzes] => SimpleXMLElement Object
                (
                )

        )

    [3] => SimpleXMLElement Object
        (
            [megnevezes] => Fence Guard ECONOMY MINI Villanypásztor Vezeték  100 m, 2mm, 3 vezetőszál - 40Kg - 14,4 Ω/m
            [azonosito] => economymini100m
            [mennyiseg] => 1
            [mennyisegiEgyseg] => SimpleXMLElement Object
                (
                )

            [nettoEgysegar] => 1488.19
            [afakulcs] => 27
            [nettoErtek] => 1488.19
            [afaErtek] => 402
            [bruttoErtek] => 1890.19
            [megjegyzes] => SimpleXMLElement Object
                (
                )

        )

    [4] => SimpleXMLElement Object
        (
            [megnevezes] => MPL Házhozszállítás
            [mennyiseg] => 1
            [mennyisegiEgyseg] => SimpleXMLElement Object
                (
                )

            [nettoEgysegar] => 394
            [afakulcs] => 27
            [nettoErtek] => 394
            [afaErtek] => 106
            [bruttoErtek] => 500
            [megjegyzes] => SimpleXMLElement Object
                (
                )

        )

)

But the returned $xml always contains the original array. Thanks for the help.

beamkiller
  • 188
  • 2
  • 12
  • Relevant: [Filtering out items using SimpleXML](https://stackoverflow.com/q/12413707/2943403) and [PHP: filter XML by attribute?](https://stackoverflow.com/q/35846288/2943403) and [SimpleXML xpath to filter out objects](https://stackoverflow.com/q/26819406/2943403) – mickmackusa Nov 13 '22 at 10:54

0 Answers0