Producten kopiëren in een OpenCart multistore

OpenCart heeft de functionaliteit om meerdere winkels via 1 beheerinterface te beheren, zogenaamde ‘Multistores’. Onderstaand snippet kan je gebruiken om alle producten van winkel A ook naar winkel B te kopieren.

Verander voor gebruik de betreffende nummers van de winkel, plak het vervolgens in een bestand, bijvoorbeeld de HeaderController. Bezoek vervolgens je homepage 1 maal en haal de snippet weer uit de Controller. Alle producten en categorieen van winkel A staan nu ook in winkel B!


        $sourceStore = 0;  // Dit is winkel A
        $targetStore = 3; // Dit is winkel B


            $sql = "SELECT category_id FROM oc_category_to_store WHERE store_id = $sourceStore";
            $rows = $this->db->query($sql)->rows;


            foreach($rows as $row)
            {
                $sql = "SELECT * FROM oc_category_to_store WHERE store_id = $targetStore AND category_id = ".(int) $row['category_id'];
                $res = $this->db->query($sql)->row;
                if(empty($res))
                {
                    $sql = "INSERT INTO oc_category_to_store (store_id, category_id) VALUES ($targetStore, ".(int) $row['category_id'].")";
                    $this->db->query($sql);
                }
            }


            $sql = "SELECT product_id FROM oc_product_to_store WHERE store_id = 0";
            $rows = $this->db->query($sql)->rows;

            foreach($rows as $row)
            {
                $sql = "SELECT * FROM oc_product_to_store WHERE store_id = $targetStore AND product_id = ".(int) $row['product_id'];
                $res = $this->db->query($sql)->row;
                if(empty($res))
                {
                    $sql = "INSERT INTO oc_product_to_store (store_id, product_id) VALUES ($targetStore, ".(int) $row['product_id'].")";
                    $this->db->query($sql);
                }
            }