HOAB

History of a bug

Adding a custom order to woocommerce do not update the post_status

Rédigé par gorki Aucun commentaire

Problem :

Adding a status to Woocommerce.

A lot of resources on internet, for example here

It was not working… I had the status in the list, but when saving the command, the old post status was kept…

1h search….

Solution :

My new order key, especially the post status / order status : wc-clickreserver-ready
To be used in : 

register_post_status( wc-clickreserver-ready, ...

Well, -ready is not a good idea

Perfectly working with :

register_post_status( wc-clickreserver, ...

Lire la suite de Adding a custom order to woocommerce do not update the post_status

OceanWP, Woocommerce and custom menu

Rédigé par gorki Aucun commentaire

Problem :

A wordpress site with OceanWP theme, Woocommerce plugin.

The requirement was to have 3 parts in the website :

  • Each parts has a different menu
  • Each parts has a different colors for menu and header

With OceanWP, you can set for each page : menu, custom header, colors...

A bit tedious to maintain from my point of view, but works, except for Woocommerce pages, especially categories.

Solution :

As everything in wordpress works with extensions, even OceanWP andWoocommerce have their own set of paid extensions... A world of business here (as 50% of the websites run wordpress...). Paid extension could be really usefull in some cases, for my simple task, I simply do it like that :

  1. Export OceanWP parameters
  2. Create a child theme of OceanWP and activate it
  3. Import OceanWP parameters
  4. Advantages :
    1. You can copy / paste of the parent theme in the child theme to modify it
    2. You can add custom functions
  5. Here is the main part, as OceanWP is very customizable (to authorize paid plugins), they already have a lot of hook ready
    1. ocean_custom_header_template : a hook to select the template
    2. ocean_custom_menu : a hook to select the menu

Adding hook to select the right theme for Woocommerce page :

function my_custom_header_function($template) {
       $result=$template; 
        if ( is_shop() || is_product() || is_product_category() ) {
          if ('<name-of-my-slug>' == get_queried_object()->slug) {
// Exception for one category
// We have created custom OceanWP header in OceanWP Theme Library, here is the ID (shown in the URL when editing the custom header)
              $result=10849;
          } else {
// All others categories have another header
              $result=10244;
          }
	}
	return $result;
}
add_filter( 'ocean_custom_header_template', 'my_custom_header_function' );

And for the menu :

function my_custom_menu($menu) {
        if ( is_shop() || is_product() || is_product_category() ) {
                if ('<name-of-my-slug>' != get_queried_object()->slug) {
                        // Except for one category, I return the custom menu to display
                        return '493';
                }        
        }
        return $menu;
}

add_filter( 'ocean_custom_menu', 'my_custom_menu');

Well, done, not perfect but done thanks to OceanWP possible customization and Wordpress ecosystem of paid plugins.

Could be done :

  • Suggest a pull request to OceanWP with an GUI to select the custom header/menu for a given slug
  • Select the custom header in a list instead of putting direct ID...

But no time today !

And I tried a mix of extensions :

But as everything, they are designed to work for a given use case and OceanWP+Woocommerce for categories was not in the scope.

Personaliser Adminer

Rédigé par gorki Aucun commentaire

Le problème :

Personnaliser l'interface d'Adminer avec du SQLite.
La documentation sur les extensions n'est pas tout à fait assez explicite : si elle indique bien des méthodes à utiliser, la configuration de la base n'est pas clair.

Par défaut si rien n'est configuré une page de login est affichée. Dans mon cas, le login est géré ailleurs dans mon application.

Solution :

Il suffit de suivre les requêtes faites par le login et au final on passe en $_GET les paramètres utilisés.

<?php 
include("../../includes/init.php");

function adminer_object() {

    class AdminerSoftware extends Adminer {

        function name() {
            // custom name in title and heading
            return 'MystoreAdmin';
        }

        function selectLimitProcess() {
            return '250';
        }
    }

    return new AdminerSoftware;
}


$_GET["sqlite"]="";
$_GET["username"]="";
$_GET['db']="../../databases/mystore.db";

session_start();
include("adminer-4.2.5.php");
session_write_close();

?>

Pour la description des fonctions et leur utilisation, un petit tour dans les sources n'est jamais inutile.

PHP Soapclient et connexion HTTPS via un proxy

Rédigé par gorki Aucun commentaire

Le problème :

En passant via mon proxy entreprise ou local (SQUID) mes requêtes SOAP recevaient les erreurs :

  • Parsing WSDL: Couldn't load from 'https://host/service?wsdl' : failed to load external entity 'https://host/service?wsdl'
  • Could not connect to host

Solution :

Trouver l'origine du problème, activer les traces :

$client = new SoapClient("http://www.webservicex.net/ConverPower.asmx?WSDL", array('trace' => 1));
echo "====== REQUEST HEADERS =====" . PHP_EOL;
var_dump($client->__getLastRequestHeaders());
echo "========= REQUEST ==========" . PHP_EOL;
var_dump($client->__getLastRequest());
echo "========= RESPONSE =========" . PHP_EOL;
var_dump($response);
array
(
    'trace' => 1
    'proxy_host' => 127.0.0.1
    'proxy_port' => 3128
)
// Attention ! vérifier que ces directives sont bien appelées (par exemple pas dans un fichier classe... sifflotements...)
ini_set('soap.wsdl_cache_enabled',0);
ini_set('soap.wsdl_cache_ttl',0)
$context = stream_context_create(array(
    'ssl' => array(
        'verify_peer' => false,
        'verify_peer_name' => false,
        'allow_self_signed' => true
    )
));

$client  = new SoapClient(null, array( 
    'location' => 'https://...',
    'uri' => '...', 
    'stream_context' => $context
));

 

 

PHP : téléchargement de fichier corrompu avec des caractères BOM

Rédigé par gorki Aucun commentaire

Le problème :

Un script PHP lit un fichier sur le disque et le renvoie à l'utilisateur, cependant le fichier est corrompu. Rapidement l'analyse montre des caractères en plus, merci wikipedia :

EF BB BF is the UTF-8 encoding Byte Order Mark (BOM).

Qui les envoie ?

Solution :

Premièrement vérifier que le fichier sur le disque n'est pas corrompu en le téléchargeant via FTP (mode binaire bien sur).

Ensuite un peu de recherche :

  1. Il semble donc que ces caractères soient au début d'un fichier PHP exécuté. Normal après tout, on demande un fichier au serveur (ici un script PHP), le serveur commence par fournir le contenu du fichier jusqu'à ce que le module PHP s'en mêle... ou s'emmêle...
  2. Pas 36 méthodes :
    • Regarder le fichier PHP appelé dans le flux HTTP
    • Editer ce fichier et vérifier l'encoding avec un éditeur (via notepad++ par exemple)
    • Au besoin, ouvrir les fichiers inclus (require / require_once).

Trucs pratiques :

  • l'outil mode développeur de Firefox en activant "la journalisation des réponses et requêtes"
  • Notepad++ (avec le module deprecated mais qui marche toujours : HEX-Editor)
  • L'encoding BOM est :
    • 77u/ dans le développeur
    • EF BB BF dans le module HEX-Editor
    • xD0 xCF quand on édite le fichier comme une brute
Fil RSS des articles de cette catégorie