Jump to content
thirty bees forum

Wordpress Integration - API


haylau

Recommended Posts

I am testing the Wordpress / Prestashop integration modules to display prestashop products on Wordpress for my blog. 

All the modules use the API system, and the ones I have tested so far all have a common issue - getting the product image, they want some variation of  image type "home_default"

image.png.80ec734813e060387f1e746dbca059e7.png

Which with Niara and probably other modules no longer exists.

If I manually edit that code in the browser console panel to a Niara image type it works

image.png.f5d4a0c8cf33c5a1208a88ceb799842f.png

 

Three posible solutions:

 

1) asimple workaround is of course to create that image type in TB

2) A bug in Webservice? I did wonder, as they all have the same issue if this is something to do with changes to the ThirtyBees webservice -  so could the webservice be delivering the wrong image name? or is it simply old modules that never worked on anything but the old Prestashop themes

3) Modify the module code - I did reach out to one of the developers who told me 

Quote

The image data is obtained by the "Webservice / API" of Prestashop.

In the includes/classes/class-didpdmain.php file, inside the
didpd_clean_values function you will see that it mounts the "$values"
array, which is used in the template (templates/public/didpd_model_a.php
) to paint the data.

It is in that function (didpd_clean_values) where you would have to
adapt, since it is possible that it uses other names to tare the image
data. Modify that part of the code where it refers to the image.

 

This level of programming is beyond me, so can anyone see how to modify this code to work?

 

<?php
namespace did_prestashop_display\includes\classes;



final class DIDPDMain
{
    protected static $_instance = null;
    public $didpd_admin = null;
    public $didpd_read = null;

    public function __construct()
    {
        $this->includes();
        $this->didpd_read = new DIDPDRead();
        $this->didpd_admin = new DIDPDAdmin($this->didpd_read);

        $this->init_hooks();
    }


    /**
     * Includes
     * @since 0.0.1
     */
    public function includes()
    {
        include_once dirname(__FILE__) . '/class-didpdadmin.php';
        include_once dirname(__FILE__) . '/class-didpdread.php';
    }

    /**
     * Hook into actions and filters.
     *
     * @since 0.0.1
     */
    private function init_hooks()
    {


        add_action('admin_menu', array( $this->didpd_admin,  'didpd_admin_menu'));
        add_shortcode('dp_product', array( $this,  'didpd_shortcode_product'));

        // enqueue
        add_action('wp_enqueue_scripts', array( $this,'didpd_enqueue'));
        add_action('admin_enqueue_scripts', array( $this,'didpd_enqueue'));

    }



    public function search_logo($name_img)
    {

      $attachments = get_page_by_title($name_img, OBJECT, 'attachment');

       if ( $attachments ){
           $attachment_url = $attachments->guid;

       } else {
           return false;
       }

       return $attachment_url;
    }



    /**
    * Creation display product
    *
    */
    public function didpd_shortcode_product($atts)
    {
        $models_display = array('a','b');

        $current_atts = shortcode_atts(array(
            'id'          => '3',
            'type'        => get_option('didpd_default_type'),
            'lang'        => get_option('didpd_default_lang'),
            'domain'      => get_option('didpd_default_domain'),
            'size_img'    => get_option('didpd_default_size_img'),
            'name'        => 'void',
            'description' => 'void',
            'button'      => get_option('didpd_default_button'),
            'status'      => 'success',
            'status_msg'  => '',
            'with_taxes'  => get_option('didpd_default_with_taxes'),
            'with_price'  => get_option('didpd_default_with_price'),
            'currency'    => get_option('didpd_default_currency'),
            'dev'         => '0'
          ), $atts, 'atributes');

        if (!in_array($current_atts['type'], $models_display)) {
            $current_atts['type'] = 'a';
        }

        $arr = get_option('didpd_domains');


        $arr_domain = (isset($arr[$current_atts['domain']]))?$arr[$current_atts['domain']]:null;

        if($arr_domain !== null) {


        $dat = $arr_domain; //$arr[$current_atts['domain']];
        $dataFont = $dat['didpd_url_webservice_'.$current_atts['domain']];
        $font = $dataFont;
        $result = $this->didpd_read->didpd_read(
                                              $arr_domain, //$arr[$current_atts['domain']],
                                              $current_atts['domain'],
                                              $current_atts['id'],
                                              $current_atts['type']

                                              );

        } else {
          ob_start();

          $display = ob_get_clean();

          return $display;
        }



        ob_start();

        if($current_atts['dev'] == 1
          || $current_atts['dev'] == 2) {
          $this->showStatus($result, $current_atts);
        }


        $url_service = $arr[$current_atts['domain']]['didpd_url_webservice_'.$current_atts['domain']];

        if($result['status'] == "error") {
          $current_atts['status'] = 'error';
          $current_atts['status_msg'] =  $result['error_txt'];

          $values = $this->didpd_clean_values_aternate($current_atts, $url_service, $result);

          $without_product_data = false;
          foreach($result['error_txt'] as $key => $value){
            if($key == 'inactive'
              || $key == '404'
              || $key == 'not_product'){
              $without_product_data = true;
            }
          }

          $img_logo = $this->search_logo($url_service);

          if(!$result['alternate_error'] && !$without_product_data) {

            $getTemplate = 'didpd_model_alternate_home_';


            $path_template = $this->didpd_create_path_template($getTemplate.$current_atts['type'].'.php');
            include($path_template);

          } else {

            $path_template = $this->didpd_create_path_template('didpd_model_alternate_home_'.$current_atts['type'].'.php');
            include($path_template);

          }

        } else {

          $values = $this->didpd_clean_values($current_atts, $url_service, $result);

          $path_template = $this->didpd_create_path_template('didpd_model_'.$current_atts['type'].'.php');

          include($path_template);

        }
        $display = ob_get_clean();

        return $display;
    }




    public function didpd_create_path_template($name_template)
    {
      return __DIR__.'/../../templates/public/'.$name_template;
    }


    public function getIdLang($current_atts, $result) {
      $id_lang = (int)$current_atts['lang'];
      if($id_lang == 0){
        $id_lang = (int)$result['id_current_lang'];
      }
      return $id_lang;
    }


    public function didpd_clean_values_aternate($current_atts, $url_service, $result)
    {
        $is_category = false;
        $id_lang    =  $this->getIdLang($current_atts, $result);

        $values = array('name_category' => 'void',
                        'description_category' => 'void',
                        'name_button' => 'void',
                        'link'        => 'void',
                        'img_product' => 'void',
                        'is_category' => $is_category,
                        'url_category' => 'void',
                        'description_category' => 'void');


        $errors = [];
        $vist_website = false;
        foreach($result['error_txt'] as $key => $value){
            $errors[] = $key;
        }


        if(in_array('inactive', $errors) && !in_array('404', $errors)){
          $vist_website = true;

        } else if(in_array('without_stock', $errors) && !in_array('404', $errors)){
          $vist_website = false;

        } else if(in_array('404', $errors) ){
          $vist_website = true;

        }

        $name_category = '';
        $description_category = '';
        $src_img_product = '';
        $url_category = '';

        if (!$result['alternate_error'] && !$vist_website) {
            $is_category = true;

            $name_button =  __('Visit our category', 'didprestashopdisplay');


            $url_category = $url_service.'/index.php?controller=category&id_category=' . $result['alternate_data']['category']->category->id;


            if(!isset($result['alternate_data']['category']->category->name->language[$id_lang])) {
              $name_category =  $result['alternate_data']['category']->category->name->language;
            } else {
              $name_category =  $result['alternate_data']['category']->category->name->language[$id_lang];
            }

            $description_category = $result['alternate_data']['category']->category->description->language[$id_lang];
            $id_lang    =  (int)$current_atts['lang'];
            $id_product = $result['alternate_data']['product']->id;

            if(!isset($result['alternate_data']['product']->product->link_rewrite->language[$id_lang])) {
              $slug   = $result['alternate_data']['product']->product->link_rewrite->language;
            } else {
              $slug   = $result['alternate_data']['product']->product->link_rewrite->language[$id_lang];
            }


            //$slug = ($slug == '')?'image':$slug;
            $src_img_product =  $url_service.'/'
                      .$result['alternate_data']['product']->product->id_default_image.'-'
                      .$current_atts['size_img']."/".$slug.".jpg";


          } else {
            $name_button =  __('Visit our Website', 'didprestashopdisplay');

          }

          $values['name_category']= strip_tags($name_category);
          $values['description_category']= strip_tags($description_category);
          $values['name_button']  = strip_tags($name_button);
          $values['link']         = $url_service;
          $values['is_category']  = $is_category;
          $values['img_product']  = $src_img_product;
          $values['url_category'] = $url_category;

        return $values;
    }


    public function didpd_clean_values($current_atts, $url_service, $result)
    {
        $values = array('name'        => 'void',
                        'description' => 'void',
                        'name_button' => 'void',
                        'url_product' => 'void',
                        'img_product' => 'void',
                        'price'       => 'void',
                        'taxes'       => 'void',
                        'with_taxes'  => 'void',
                        'with_price'  => 'void');

        if ($current_atts['status'] == 'success') {
            $id_lang    =  $this->getIdLang($current_atts, $result);
            $id_product = $result['content']['product_obj']->id;
            $slug       = $result['content']['product_obj']->link_rewrite->language[$id_lang];
            $with_taxes = $current_atts['with_taxes'];

            $all_prices = $result['all_prices'];

            $name = $current_atts['name'];

            if ($name == 'void') {
              if($result['content']['product_obj']->name->language[$id_lang] == NULL) {
                $name = $result['content']['product_obj']->name->language;
              } else {
                $name = $result['content']['product_obj']->name->language[$id_lang];
              }
            }


            $description = $current_atts['description'];
            if ($description == 'void') {
              if($result['content']['product_obj']->description_short->language[$id_lang] == NULL) {
                $description = $result['content']['product_obj']->description_short->language;
              } else {
                $description = $result['content']['product_obj']->description_short->language[$id_lang];
              }

                if ($description == '') {
                  if($result['content']['product_obj']->description->language[$id_lang] == NULL) {
                    $description = $result['content']['product_obj']->description->language;
                  } else {
                    $description = $result['content']['product_obj']->description->language[$id_lang];
                  }

                }
                $description = strip_tags($description);
                if (strlen($description) > 125) {
                    $description = substr($description, 0, 125)."...";
                }
            }


            $name_button = $current_atts['button'];
            if ($name_button == 'void') {
                $name_button = __('More information...', 'didprestashopdisplay');
            }
            $slug = ($slug == '')?'image':$slug;
            $src_img =  $url_service.'/'
                      .$result['content']['product_obj']->id_default_image.'-'
                      .$current_atts['size_img']."/".$slug.".jpg";



            $url_product = $url_service.'/index.php?controller=product&id_product=' . $id_product;

            $values['name']        = $name;
            $values['description'] = $description;
            $values['name_button'] = $name_button;
            $values['url_product'] = $url_product;
            $values['img_product'] = $src_img;
            $values['with_taxes']  = $with_taxes;
            $values['all_prices']  = $all_prices;
            $values['with_price']  = $current_atts['with_price'];
            $values['currency']    = $current_atts['currency'];
        }

        return $values;
    }



    /**
     * Main DIDPDMain  Instance.
     *
     *
     * @since 0.0.1
     * @static
     * @see didis()
     * @return DIDPDMain - Main instance.
     */
    public static function instance()
    {

        if (is_null(self::$_instance)) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }




    /**
    * Include CSS and JS
    * @return void
    */
    public function didpd_enqueue()
    {
        wp_register_style('barsandmore_4', plugin_dir_url( __FILE__ ) .'../../css/bootstrap.min.css', false, null, 'all');
        wp_enqueue_style('barsandmore_4');

        wp_register_style('did-web-css', plugin_dir_url( __FILE__ ) .'../../css/did-web.css', false, null, 'all');
        wp_enqueue_style('did-web-css');

        wp_enqueue_script('jquery');

        wp_register_script('boot3', plugin_dir_url( __FILE__ ) .'../../js/bootstrap.min.js', array('jquery'), '1.0', true);
        wp_enqueue_script('boot3');

    }



    private function showStatus($result, $atts) {

      $error_txt = '';
      if(isset($result['error_txt'])
        && gettype($result['error_txt']) == 'array') {
        $error_txt = implode(', ', $result['error_txt']);
      }

      $id         = 'Sin ID';
      $statusHttp = 'Sin Http';
      $cantidad   = (isset($result['stock_available']))?$result['stock_available']:0;
      $url_font   = $result['url_font'];

      if(isset($result['content'])) {
        $id = ($result['content']['product_obj']->id)?$result['content']['product_obj']->id:'Sin Id';
        $statusHttp = $result['content']['httpCode'];
      }

      $status = $result['status'];

      $css_status = 'success';
      if($status == 'error') {
        $css_status = 'danger';
      }
      echo "<div class='col-12 col-lg-12 bg-light-success border boreder-success boreder-rounded'>";
      echo "<small class='text-primary'>Solicitud: ".$atts['id']." </small><br>";
      echo "<small class='text-primary'>".$error_txt." </small><br>";

      echo "<small class='text-".$css_status."'>Status [".$id."]: <b>".$result['status']."</b> / Http: <b>".$statusHttp."</b></small><br>";

      echo "<small class='text-".$css_status."'>Cantidad: <b>".$cantidad."</b></small><br>";
      echo "<a class='text-".$css_status."' target='_blank'  href='".$url_font."'><small>".$url_font."</small> </a>";

      if($atts['dev'] == 2) {
        echo "<pre>".var_dump($atts)." </pre><br>";
        echo "<pre>".var_dump($result['content'])." </pre><br>";
      }
      echo "</div>";

    }

}

 

Link to comment
Share on other sites

the wp plugin uses this code to construct image src:

            $src_img_product =  $url_service.'/'
                      .$result['alternate_data']['product']->product->id_default_image.'-'
                      .$current_atts['size_img']."/".$slug.".jpg";

where $result and $slug comes from webservice response, but $current_atts['size_img'] comes from plugin configuration:

'size_img'    => get_option('didpd_default_size_img')

There is probably some UI element to set this config key, or maybe now. I don't know wordpress.

Anyway, this is not tb webservice issue

Link to comment
Share on other sites

4 hours ago, datakick said:

the wp plugin uses this code to construct image src:


            $src_img_product =  $url_service.'/'
                      .$result['alternate_data']['product']->product->id_default_image.'-'
                      .$current_atts['size_img']."/".$slug.".jpg";

where $result and $slug comes from webservice response, but $current_atts['size_img'] comes from plugin configuration:


'size_img'    => get_option('didpd_default_size_img')

There is probably some UI element to set this config key, or maybe now. I don't know wordpress.

Anyway, this is not tb webservice issue

Thanks, that gives me something to work with

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...