custom/plugins/IdeaBooks/src/Storefront/Subscriber/ProductSubscriber.php line 52

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace IdeaBooks\Storefront\Subscriber;
  3. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Shopware\Core\Framework\Struct\ArrayEntity;
  6. use \Mysqli;
  7. use GeoIp2\WebService\Client as GeoIp2Client;
  8. class ProductSubscriber implements EventSubscriberInterface
  9. {
  10.     public static function getSubscribedEvents(): array
  11.     {
  12.         return [
  13.           ProductPageLoadedEvent::class => 'addBuyFrom'
  14.         ];
  15.     }
  16.     private function OpenCon()
  17.      {
  18.         //DATABASE_URL="mysql://ideabooks.nl:i9cIbFBadWuxdwgb@localhost:3306/ideabooks_albertvanderveen_nl"
  19.         $databaseUrl getenv('DATABASE_URL');
  20.         $databaseUrlParts explode(':'$databaseUrl);
  21.         $dbuser trim($databaseUrlParts[1],'/');
  22.         $dbpass explode('@'$databaseUrlParts[2])[0];
  23.         $dbhost explode('@'$databaseUrlParts[2])[1];
  24.         $db explode('/'$databaseUrlParts[3])[1];
  25.          /*
  26.          $dbhost = "localhost";
  27.          $dbuser = "app";
  28.          $dbpass = "SAg8XKmloD8cjS2dCvneYFMnH70onxgr";
  29.          $db = "ideanew2";
  30.          */
  31.          $mysqli = new mysqli($dbhost$dbuser$dbpass,$db) or die("Connect failed: %s\n"$conn -> error);
  32.          return $mysqli;
  33.      }
  34.      
  35.     private function CloseCon($mysqli)
  36.      {
  37.          $mysqli -> close();
  38.      }
  39.      public function addBuyFrom(ProductPageLoadedEvent $event): void
  40.     {
  41.         /*
  42.         $request = $event->getRequest();
  43.         $contextToken = $request->headers->get(PlatformRequest::HEADER_CONTEXT_TOKEN);
  44.         if ($contextToken) {
  45.             $persistedParams = $this->contextPersister->load($contextToken);
  46.             $persistedCountryId = $persistedParams[SalesChannelContextService::COUNTRY_ID] ?? null;
  47.             dump($persistedCountryId);
  48.         }
  49.         */
  50.         //$country = $event->getPage()->getHeader()->activeShippingLocation->getCountry();
  51.         //dump($country);
  52.         //$isocode2 = $country->get('iso');
  53.         //$isocode2 = $_SERVER['GEOIP_CITY_COUNTRY_CODE'];
  54.         $mysqli $this->OpenCon();
  55.         $ip $_SERVER['REMOTE_ADDR'];
  56.         $query "SELECT isocode2 FROM idea_ip WHERE ip = '".$ip."'";
  57.         if ($result $mysqli->query($query)) {
  58.             if ($result->num_rows == 1){
  59.                 $row mysqli_fetch_assoc($result);
  60.                 $isocode2 $row['isocode2'];
  61.                 file_put_contents(getenv('APP_DIR') . '/var/log/ProductSubscriber.log'PHP_EOL.date("Y-m-d H:i:s").chr(9).'isocode2 uit lokale tabel gehaald'.PHP_EOLFILE_APPEND);
  62.             }
  63.             else {
  64.                 $client = new GeoIp2Client(448110'HnTnKEMimEMUwKN3');
  65.                 $record $client->country($ip);
  66.                 $isocode2 $record->country->isoCode;
  67.                 file_put_contents(getenv('APP_DIR') . '/var/log/ProductSubscriber.log'PHP_EOL.date("Y-m-d H:i:s").chr(9).'isocode2 via GeoIp2Client'.PHP_EOLFILE_APPEND);
  68.                 $query "INSERT INTO idea_ip VALUES('" $ip "','" $isocode2 "')";
  69.                 file_put_contents(getenv('APP_DIR') . '/var/log/ProductSubscriber.log'PHP_EOL.date("Y-m-d H:i:s").chr(9).'insert query ip/isocode2: '.$query.PHP_EOLFILE_APPEND);
  70.                 if ($result $mysqli->query($query)) {
  71.                     file_put_contents(getenv('APP_DIR') . '/var/log/ProductSubscriber.log'PHP_EOL.date("Y-m-d H:i:s").chr(9).'ip/isocode2 toegevoegd aan lokale tabel'.PHP_EOLFILE_APPEND);
  72.                 }
  73.             }
  74.         }
  75.         else $isocode2 'NL';//default als er iets misgaat
  76.         file_put_contents(getenv('APP_DIR') . '/var/log/ProductSubscriber.log'PHP_EOL.date("Y-m-d H:i:s").chr(9).'ip: '.$_SERVER['REMOTE_ADDR'].PHP_EOLFILE_APPEND);
  77.         file_put_contents(getenv('APP_DIR') . '/var/log/ProductSubscriber.log'PHP_EOL.date("Y-m-d H:i:s").chr(9).'isocode2: '.$isocode2.PHP_EOLFILE_APPEND);
  78.         $product $event->getPage()->getProduct();
  79.         $titelnummer trim($product->get('productNumber'), 'idea');//strip 'idea'
  80.         file_put_contents(getenv('APP_DIR') . '/var/log/ProductSubscriber.log'PHP_EOL.date("Y-m-d H:i:s").chr(9).'titelnummer: idea'.$titelnummer.PHP_EOLFILE_APPEND);
  81.         $isSaleable $product->get('stock') > && $product->get('available');
  82.         $addtocartUrl '';
  83.         //dump($titelnummer);
  84.        //dump($isSaleable);
  85.         //idearegio ophalen
  86.         $idearegio null;
  87.         $query "SELECT idearegio FROM idea_land WHERE isocode2 = '".$isocode2."'";
  88.         file_put_contents(getenv('APP_DIR') . '/var/log/ProductSubscriber.log'PHP_EOL.date("Y-m-d H:i:s").chr(9).'query idearegio: '.$query.PHP_EOLFILE_APPEND);
  89.         if ($result $mysqli->query($query)) {
  90.             //printf("Select returned %d rows.\n", $result->num_rows);
  91.             $row mysqli_fetch_all($resultMYSQLI_ASSOC);
  92.             $idearegio $row[0]['idearegio'];
  93.         }
  94.         file_put_contents(getenv('APP_DIR') . '/var/log/ProductSubscriber.log'PHP_EOL.date("Y-m-d H:i:s").chr(9).'idearegio: '.$idearegio.PHP_EOLFILE_APPEND);
  95.         //dump($idearegio);
  96.         //eerst in het land zelf zoeken:
  97.         //$titelboekhandelurl_collection_land = Mage::getModel('ideabooks/titelboekhandelurl')->getCollection()
  98.         //->addFieldToFilter('titelnummer', $titelnummer)->addFieldToFilter('isocode2', $isocode2);
  99.         $boekhandels = array();
  100.         $query "SELECT url, naam, isocode3 FROM idea_titelboekhandelurl WHERE titelnummer = '".$titelnummer."' AND isocode2 = '".$isocode2."' LIMIT 30";
  101.         file_put_contents(getenv('APP_DIR') . '/var/log/ProductSubscriber.log'PHP_EOL.date("Y-m-d H:i:s").chr(9).'query idea_titelboekhandelurl: '.$query.PHP_EOLFILE_APPEND);
  102.         if ($result $mysqli->query($query)) {
  103.             //printf("Select returned %d rows.\n", $result->num_rows);
  104.             //dump($result->num_rows);
  105.             $boekhandels mysqli_fetch_all($resultMYSQLI_ASSOC);
  106.             //file_put_contents(getenv('APP_DIR') . '/var/log/ProductSubscriber.log', PHP_EOL.date("Y-m-d H:i:s").chr(9).'boekhandels:'.print_r($boekhandels, true).PHP_EOL, FILE_APPEND);
  107.         }
  108.         //vervolgens in idearegio, maar met uitzondering van het land zelf (dat is immers al verwerkt)
  109.         /*
  110.         $titelboekhandelurl_collection_regio = Mage::getModel('ideabooks/titelboekhandelurl')->getCollection()
  111.         ->addFieldToFilter('titelnummer', $titelnummer)
  112.         ->addFieldToFilter('idearegio', $idearegio)
  113.         ->addFieldToFilter('isocode2', array('neq' => $isocode2));
  114.         */
  115.         if ($idearegio){
  116.             $query "SELECT url, naam, isocode3 FROM idea_titelboekhandelurl WHERE titelnummer = '".$titelnummer."' AND isocode2 != '".$isocode2."' AND idearegio='".$idearegio."'";
  117.             if ($result $mysqli->query($query)) {
  118.                 //printf("Select returned %d rows.\n", $result->num_rows);
  119.                 //$boekhandels = mysqli_fetch_all($result, MYSQLI_ASSOC);
  120.                 //dump($result->num_rows);
  121.                 $boekhandels array_merge($boekhandelsmysqli_fetch_all($resultMYSQLI_ASSOC));
  122.                 //file_put_contents(getenv('APP_DIR') . '/var/log/ProductSubscriber.log', PHP_EOL.date("Y-m-d H:i:s").chr(9).'boekhandels na merge:'.print_r($boekhandels, true).PHP_EOL, FILE_APPEND);
  123.             }
  124.         }
  125.         file_put_contents(getenv('APP_DIR') . '/var/log/ProductSubscriber.log'PHP_EOL.date("Y-m-d H:i:s").chr(9).'boekhandels: '.print_r($boekhandelstrue).PHP_EOLFILE_APPEND);
  126.         $this->CloseCon($mysqli);
  127.         //dump($boekhandels);
  128.         $saleOptionAvailable false;
  129.         $buyFrom '';
  130.         //limiet nu anders oplossen: break uit foreach na 10 loops
  131.         $count 0;
  132.         foreach ($boekhandels as $boekhandel) {
  133.             $saleOptionAvailable true;
  134.             $boekhandelUrl $boekhandel['url'];
  135.             if (stristr($boekhandelUrl'http') === false$boekhandelUrl 'http://'.$boekhandelUrl;
  136.             $buyFrom .= '<a href="'.$boekhandelUrl.'" onClick="ga(\'send\', \'event\', {
  137.   \'eventCategory\': \'Boekhandel links\',
  138.   \'eventAction\': \'click\',
  139.   \'eventLabel\': \''.$boekhandelUrl.'\',
  140.   \'hitCallback\': function() {
  141.     //alert(\'hit sent\');
  142.     //window.open(\''.$boekhandelUrl.'\',\'_blank\')
  143.     window.location.href = \''.$boekhandelUrl.'\'
  144.   }
  145. }); return false;">'.ucwords(strtolower($boekhandel['naam'])).'</a> ('.$boekhandel['isocode3'].')<br />';
  146.             $count++;
  147.             if ($count == 30) break;
  148.         }
  149.         //direct via Ideabooks - niet toevoegen, in template
  150.         /*if ($isSaleable){
  151.             $saleOptionAvailable = true;
  152.             $buyFrom .= '<a href="'.$addtocartUrl.'">Idea Books Direct</a>';
  153.         }*/
  154.         //if (!$saleOptionAvailable) $buyFrom .= '';
  155.         //$buyFrom = '<h3>Buy this title from:</h3>'.$buyFrom;
  156.         //dump($buyFrom);
  157.         /* debug van $event->getPage() geeft o.a. deze key:
  158.             #extensions: array:1 [▼
  159.                 "product_buyfrom" => Shopware\Core\Framework\Struct\ArrayEntity {#264113 ▼
  160.                     #data: array:1 [▼
  161.                         0 => "test.html"
  162.                     ]
  163.             de array data daarin is direct te benaderen, dus bijv via page.extensions.product_buyfrom.0: geeft string(9) "test.html"
  164.         */
  165.         $buyFromStruct = new ArrayEntity();
  166.         $buyFromStruct->set(0,$buyFrom);
  167.         $event->getPage()->addExtension('product_buyfrom'$buyFromStruct);
  168.         //dump($event);
  169.     }
  170. }