SHARE
TWEET

Untitled

guillaumebertrand52 Dec 10th, 2018 189 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. wp_enqueue_style('animate-css', 'https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.2/animate.min.css');
  3. wp_enqueue_script('wow-js', 'https://cdnjs.cloudflare.com/ajax/libs/wow/1.1.2/wow.min.js');
  4. wp_enqueue_script('wow-init-js', get_stylesheet_directory_uri() . '/js/wow-init.js', array('wow-js'), '1.0.1', true);
  5. wp_enqueue_script('qty-add-to-cart', get_stylesheet_directory_uri() . '/js/qty-add-to-cart.js', array('jquery'), '1.0.1', true);
  6. remove_action( 'load-update-core.php', 'wp_update_themes' );
  7. add_filter( 'pre_site_transient_update_themes', create_function( '$a', "return null;" ) );
  8. function stop_plugin_update( $value ) { unset( $value->response['webriti-companion/webriti-companion.php'] ); return $value; }
  9. add_filter( 'site_transient_update_plugins', 'stop_plugin_update' );
  10. ///
  11. define('QUALITY_TEMPLATE_DIR_URI',get_template_directory_uri());
  12. define('QUALITY_TEMPLATE_DIR',get_template_directory());
  13. define('QUALITY_THEME_FUNCTIONS_PATH',QUALITY_TEMPLATE_DIR.'/functions');
  14. define('QUALITY_THEME_OPTIONS_PATH',QUALITY_TEMPLATE_DIR_URI.'/functions/theme_options');
  15. require( QUALITY_THEME_FUNCTIONS_PATH . '/menu/new_Walker.php'); //NEW Walker Class Added.
  16. require( QUALITY_THEME_FUNCTIONS_PATH . '/menu/default_menu_walker.php');
  17. require_once( QUALITY_THEME_FUNCTIONS_PATH . '/scripts/scripts.php');     //Theme Scripts And Styles
  18. require( QUALITY_THEME_FUNCTIONS_PATH . '/resize_image/resize_image.php'); //Image Resizing
  19. require( QUALITY_THEME_FUNCTIONS_PATH . '/commentbox/comment-function.php'); //Comment Handling
  20. require( QUALITY_THEME_FUNCTIONS_PATH . '/widget/custom-sidebar.php'); //Sidebar Registration
  21. //Customizer
  22. require( QUALITY_THEME_FUNCTIONS_PATH . '/customizer/customizer-slider.php');
  23. require( QUALITY_THEME_FUNCTIONS_PATH . '/customizer/customizer-copyright.php');
  24. require( QUALITY_THEME_FUNCTIONS_PATH . '/customizer/customizer-home.php');
  25. require( QUALITY_THEME_FUNCTIONS_PATH . '/customizer/customizer-blog.php');
  26. require( QUALITY_THEME_FUNCTIONS_PATH . '/customizer/customizer-pro.php');
  27. require( QUALITY_THEME_FUNCTIONS_PATH . '/customizer/customizer_recommended_plugin.php');
  28. require( QUALITY_THEME_FUNCTIONS_PATH . '/customizer/customizer_import_data.php');
  29. require( QUALITY_THEME_FUNCTIONS_PATH . '/font/font.php');
  30. //Quality Demo Image
  31. require_once( get_template_directory() . '/quality-demo-image/quality-prevdem.php' );
  32. $repeater_path = trailingslashit( get_template_directory() ) . '/functions/customizer-repeater/functions.php';
  33. if ( file_exists( $repeater_path ) ) { require_once( $repeater_path ); }
  34. //wp title tag starts here
  35. function quality_head( $title, $sep ) {
  36.     global $paged, $page;
  37.     if ( is_feed() ) { return $title; }
  38.     // Add the site name.
  39.     $title .= get_bloginfo( 'name' );
  40.     // Add the site description for the home/front page.
  41.     $site_description = get_bloginfo( 'description' );
  42.     if ( $site_description && ( is_home() || is_front_page() ) )
  43.         $title = "$title $sep $site_description";
  44.     // Add a page number if necessary.
  45.     if ( $paged >= 2 || $page >= 2 )
  46.         $title = "$title $sep " . sprintf( _e( 'Page', 'quality' ), max( $paged, $page ) );
  47.     return $title;
  48. }
  49. add_filter( 'wp_title', 'quality_head', 10, 2);
  50. add_action( 'after_setup_theme', 'quality_setup' );
  51. function quality_setup() {
  52.     //content width
  53.     if ( ! isset( $content_width ) ) $content_width = 700;//In PX
  54.     // Load text domain for translation-ready
  55.     load_theme_textdomain( 'quality', QUALITY_THEME_FUNCTIONS_PATH . '/lang' );
  56.     add_theme_support( 'post-thumbnails' ); //supports featured image
  57.     // This theme uses wp_nav_menu() in one location.
  58.     register_nav_menu( 'primary', __( 'Primary Menu', 'quality' ) ); //Navigation
  59.     // theme support
  60.     add_theme_support( 'automatic-feed-links');
  61.     // Add theme support for selective refresh for widgets.
  62.     add_theme_support( 'customize-selective-refresh-widgets' );
  63.     //Title tag
  64.     add_theme_support( "title-tag" );
  65.     require_once('theme_setup_data.php');
  66.     // setup admin pannel defual data for index page
  67.     $quality_pro_options=theme_data_setup();
  68.     if ( is_admin() ) { require( QUALITY_THEME_FUNCTIONS_PATH . '/quality-info/welcome-screen.php'); }
  69. }
  70. // Read more tag to formatting in blog page
  71. function quality_new_content_more($more) {
  72.     global $post;
  73.     return '<div class="blog-btn-col"><a href="' . get_permalink() . "#more-{$post->ID}\" class=\"qua_blog_btn\">Read More<i class='fa fa-long-arrow-right'></i></a></div>";
  74. }
  75. add_filter( 'the_content_more_link', 'quality_new_content_more' );
  76. add_filter( "the_excerpt", "quality_add_class_to_excerpt" );
  77. function quality_add_class_to_excerpt( $excerpt ) {
  78.     return str_replace('<p', '<p class="qua-blog-post-description"', $excerpt);
  79. }
  80. add_action( 'admin_init', 'quality_detect_button' );
  81. function quality_detect_button() {
  82.     wp_enqueue_style( 'quality-info-button', get_template_directory_uri() . '/css/import-button.css' );
  83. }
  84. function quality_theme_plugin_notify() {
  85.     ?><div class="notice-warning notice is-dismissible">
  86.     <p><b><?php echo sprintf( esc_html__( "Important Notice: We hope you are enjoying this theme, and we thank you for updating it. Please install our Webriti Companion plugin to manage your Services/Projects sections. Don't worry, nothing is lost, just install the Companion Plugin. Go to the Customizer and install the Webriti Companion plugin from there.", 'quality' ) ); ?></b></p>
  87.     <p><a href="<?php echo esc_url( admin_url( '/customize.php' ) ); ?>" class="button" style="text-decoration: none;"><?php _e( 'Go To Customizer', 'quality' ); ?></a></p>
  88.     </div><?php
  89. }
  90. $old_theme = get_option( 'quality_pro_options');
  91. if($old_theme!='') {
  92.     include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
  93.     if( is_plugin_active( 'webriti-companion/webriti-companion.php' ) ) {}  else { add_action( 'admin_notices', 'quality_theme_plugin_notify' ); }
  94. }
  95. ///FOR NEW USER TO BE STAGIAIRE IN DEFAULT
  96. function my_new_customer_data($new_customer_data){
  97.     $new_customer_data['role'] = 'stagiaire';
  98.     return $new_customer_data;
  99. }
  100. ///FOR NEW USER TO BE FORMATEUR IN DEFAULT
  101. function my_new_customer_data_F($new_customer_data){
  102.     $new_customer_data['role'] = 'formateur';
  103.     return $new_customer_data;
  104. }
  105. function get_height_from_png($file) {
  106.     exec('convert '.$file.' /var/www/clients/client0/web1/web/docs/img.png 2>/dev/null');
  107.     exec('cd /var/www/clients/client0/web1/web/docs/; ls img*.png',$rr);
  108.     exec('cp /var/www/clients/client0/web1/web/docs/'.$rr[count($rr)-1].' /var/www/clients/client0/web1/web/docs/last0.png');
  109.     exec('rm /var/www/clients/client0/web1/web/docs/img*.png');
  110.     exec('convert  -background "#FF0000" -flatten /var/www/clients/client0/web1/web/docs/last0.png /var/www/clients/client0/web1/web/docs/last.png 2>/dev/null');
  111.     exec('rm /var/www/clients/client0/web1/web/docs/last0.png');
  112.     $img = imagecreatefrompng("/var/www/clients/client0/web1/web/docs/last.png");
  113.     $w = imagesx($img);
  114.     $h = imagesy($img);
  115.     $minx=999999;
  116.     $miny=999999;
  117.     $maxx=0;
  118.     $maxy=0;
  119.     $ok=0;
  120.     for($y=$h-1;$y>0;$y=$y-1) {
  121.         for($x=$w-1;$x>0;$x=$x-20) {
  122.             $rgb = imagecolorat($img, $x, $y);
  123.             $colors = imagecolorsforindex($img, $rgb);
  124.             if($colors['red']==255 && $colors['green']==255 && $colors['blue']==255) {
  125.                 $minx=min($x,$minx);
  126.                 $maxx=max($x,$maxx);
  127.                 $miny=min($y,$miny);
  128.                 $maxy=max($y,$maxy);
  129.                 $ok=1;
  130.             }
  131.             if($ok==1) { break; }
  132.         }
  133.         if($ok==1) { break; }
  134.     }
  135.     exec('rm /var/www/clients/client0/web1/web/docs/last.png');
  136.     return $miny-50;
  137. }
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144. //Remove sort dropdown
  145. remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
  146. add_action( 'woocommerce_before_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 5 );
  147.  
  148.  
  149.  
  150. //Remove sort dropdown
  151. remove_action( 'woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 30 );
  152. //Remove number of results
  153. remove_action( 'woocommerce_before_shop_loop' , 'woocommerce_result_count', 20 );
  154. //Nombre products per page
  155. function new_loop_shop_per_page( $nbr )  { $nbr = 9999; return $nbr; }
  156. add_filter('loop_shop_per_page','new_loop_shop_per_page', 999);
  157. add_action( 'woocommerce_product_query', 'woocommerce_product_query' );
  158. function woocommerce_product_query( $q ) {
  159.     if ( $q->is_main_query() && ( $q->get( 'wc_query' ) === 'product_query' ) ) {
  160.         $q->set( 'posts_per_page', '999' );
  161.     }
  162. }
  163. add_filter('loop_shop_per_page', create_function('$cols', 'return 999;'));
  164. //Nombre products per row
  165. function loop_columns() { return 1; }
  166. add_filter('loop_shop_columns', 'loop_columns', 999);
  167. //Nombre de produit RELATED PRODUCTS && Nombre colonne
  168. function woo_related_products_limit() { global $product; $args['posts_per_page'] = 99; return $args; }
  169. add_filter( 'woocommerce_output_related_products_args', 'jk_related_products_args' );
  170. function jk_related_products_args( $args ) { $args['posts_per_page'] = 99; $args['columns'] = 2; return $args; }
  171. //Nombre de produit UPSELL PRODUCTS && Nombre colonne
  172. add_filter( 'woocommerce_output_related_products_args', 'wc_change_number_related_products' );
  173. function wc_change_number_related_products( $args ) { $args['posts_per_page']=99; $args['columns']=2; return $args; }
  174. //Change default Country
  175. add_filter( 'default_checkout_billing_country', 'change_default_checkout_country' );
  176. function change_default_checkout_country() { return 'FR'; }
  177. //Remove Billing State
  178. add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
  179. function custom_override_checkout_fields( $fields ) {
  180.     unset($fields['billing']['billing_state']);
  181.     return $fields;
  182. }
  183. //ADD the woocommerce_checkout_before_order_review texte
  184. function action_woocommerce_before_checkout_form() {
  185.     $user=wp_get_current_user(); if($user->roles[0]=='formateur') { return 0; }
  186.     echo '
  187.   <script>
  188.     jQuery( document ).ready(function() {
  189.         jQuery(\'#qui\').val(\'\');
  190.         jQuery(\'#info_pro\').hide();
  191.         jQuery(\'#siret_field\').hide();
  192.         jQuery(\'#customer_details\').hide();
  193.         jQuery(\'#order_review_heading\').hide();
  194.         jQuery(\'#order_review\').hide();
  195.         jQuery(\'#billing_country_field\').hide();
  196.     });
  197.     function show_hide(value) {
  198.         jQuery(\'#qui\').val(\'\');
  199.         jQuery(\'#info_pro\').hide();
  200.         jQuery(\'#siret_field\').hide();
  201.         if(value==\'formateur\'){
  202.             jQuery(\'#qui\').val(\'formateur\');
  203.             jQuery(\'#info_pro\').show();
  204.             jQuery(\'#siret_field\').show();
  205.         }
  206.         if(value==\'societe\'){
  207.             jQuery(\'#qui\').val(\'societe\');
  208.             jQuery(\'#info_pro\').hide();
  209.             jQuery(\'#siret_field\').hide();
  210.         }
  211.         if(value==\'personne\'){
  212.             jQuery(\'#qui\').val(\'personne\');
  213.             jQuery(\'#info_pro\').hide();
  214.             jQuery(\'#siret_field\').hide();
  215.         }
  216.         jQuery(\'#customer_details\').show();
  217.         jQuery(\'#order_review_heading\').show();
  218.         jQuery(\'#order_review\').show();
  219.     }
  220.   </script>
  221.   <div style="text-align:center;" class="qua_sidebar_widget">
  222.   <h2 style="text-align:left;font-weight:800; margin-bottom:36px; margin-top : 0">Vous êtes :</h2>
  223. <a class="flex_btn" href="#" onclick="show_hide(\'societe\'); return false;">Une société <i class="fa fa-chevron-circle-right"></i></a>
  224. <a class="flex_btn" href="#" onclick="show_hide(\'personne\'); return false;">Une personne <i class="fa fa-chevron-circle-right"></i></a>
  225. <a class="flex_btn" href="#" onclick="show_hide(\'formateur\'); return false;">Un formateur <i class="fa fa-chevron-circle-right"></i></a>    
  226.   </div>';
  227. }
  228. add_action( 'woocommerce_before_checkout_form', 'action_woocommerce_before_checkout_form', 10, 0 );
  229. //AFFICHER LES IMAGES DES CATEGORIES DE FORMATIONS
  230. add_action( 'woocommerce_archive_description', 'woocommerce_category_image', 2 );
  231. function woocommerce_category_image() {
  232.     if ( is_product_category() ){
  233.         global $wp_query;
  234.         $cat = $wp_query->get_queried_object();
  235.         $thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
  236.         $image = wp_get_attachment_url( $thumbnail_id );
  237.         if ( $image ) { echo '<img src="' . $image . '" alt="' . $cat->name . '" />'; }
  238.     }
  239. }
  240. //ADD PLUS DE MENU DANS LE THEME
  241. register_nav_menus( array( 'Top2' => 'Menu principale 2', ) );
  242. register_nav_menus( array( 'Header' => 'Menu header', ) );
  243.  
  244. //////////////////////////////////////////////////////////////
  245. //ADD FIELD SIRET TO CHECKOUT
  246. function my_custom_checkout_field( $checkout ) {
  247.     $user=wp_get_current_user(); if($user->roles[0]=='formateur') { return 0; }
  248.     echo '<input name="qui" type="hidden" id="qui" value="'.$checkout->get_value( 'qui' ).'">
  249.         <div id="info_pro"><h3>' . __('Informations Professionnelles') . '</h3>';
  250.  
  251.     woocommerce_form_field( 'siret', array(
  252.         'type' => 'text',
  253.         'class' => array('my-field-class form-row-wide'),
  254.         'label' => __('Numéro de Siret'),
  255.         'placeholder' => __('siret'),
  256.     ), $checkout->get_value( 'siret' ));
  257.  
  258.     echo '</div>';
  259. }
  260. add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );
  261. ///
  262. function my_custom_checkout_field_update_order_meta( $order_id ) {
  263.     if ( ! empty( $_POST['siret'] ) ) { update_post_meta( $order_id, 'siret', sanitize_text_field( $_POST['siret'] ) ); }
  264.     if ( ! empty( $_POST['qui'] ) ) { update_post_meta( $order_id, 'qui', sanitize_text_field( $_POST['qui'] ) ); }
  265. }
  266. add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' );
  267.  
  268.  
  269.  
  270.  
  271.  
  272.  
  273.  
  274.  
  275.  
  276.  
  277. /////////////////////////////////////////////////////////////////////////////////////
  278. //SAVE PRODUCT : ADD CATEGORY IN CRM && ADD PRODUCT IN CRM
  279. function reorder_products() {
  280.     global $wpdb;
  281.     $args = array('post_type'     => array('product'),'posts_per_page' => '-1',);
  282.     $query = new WP_Query( $args );
  283.     if($query->posts) { foreach($query->posts as $the_post) { $the_product = new WC_Product($the_post->ID); $order[$the_product->get_title()]=$the_post->ID; } }
  284.     ksort($order); $list = join(', ', $order); $wpdb->query( 'SELECT @i:=-1' );
  285.     $result = $wpdb->query("UPDATE wp_posts SET menu_order = ( @i:= @i+1 ) WHERE ID IN ( $list ) AND post_type='product' ORDER BY FIELD( ID, $list );");
  286. }
  287. /////////////////////////////////////////////////////////////////////////////////////
  288. add_action('acf/save_post', 'srb_add_info_on_save_post_product_after_acf', 20);
  289. function srb_add_info_on_save_post_product_after_acf( $post_id ) {
  290.     $the_product = wc_get_product($post_id); $tp=print_r($the_product,true);
  291.     if($tp=='') { return 0; }
  292.     if($the_product->is_downloadable()) { return 0; }
  293.     reorder_products();
  294.     /////////////////////////////////////////////////////////////////////////////////////
  295.     $url = "http://suitecrm.straformation.fr/service/v4_1/rest.php";
  296.     $username = "admin";
  297.     $password = "waRR10RP0wa";
  298.     function call($method, $parameters, $url) {
  299.         ob_start();
  300.         $curl_request = curl_init();
  301.         curl_setopt($curl_request, CURLOPT_URL, $url);
  302.         curl_setopt($curl_request, CURLOPT_POST, 1);
  303.         curl_setopt($curl_request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
  304.         curl_setopt($curl_request, CURLOPT_HEADER, 1);
  305.         curl_setopt($curl_request, CURLOPT_SSL_VERIFYPEER, 0);
  306.         curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1);
  307.         curl_setopt($curl_request, CURLOPT_FOLLOWLOCATION, 0);
  308.         $jsonEncodedData = json_encode($parameters);
  309.         $post = array(
  310.             "method" => $method,
  311.             "input_type" => "JSON",
  312.             "response_type" => "JSON",
  313.             "rest_data" => $jsonEncodedData
  314.         );
  315.         curl_setopt($curl_request, CURLOPT_POSTFIELDS, $post);
  316.         $result = curl_exec($curl_request);
  317.         curl_close($curl_request);
  318.         $result = explode("\r\n\r\n", $result, 2);
  319.         $response = json_decode($result[1]);
  320.         ob_end_flush();
  321.         return $response;
  322.     }
  323.     //////////////////////////////////////////////////////
  324.     //login ---------------------------------------------
  325.     $login_parameters = array(
  326.         "user_auth" => array(
  327.             "user_name" => $username,
  328.             "password" => md5($password),
  329.             "version" => "1"
  330.         ),
  331.         "application_name" => "Woocommerce",
  332.         "name_value_list" => array(),
  333.     );
  334.     $login_result = call("login", $login_parameters, $url); $session_id = $login_result->id; //echo "<pre>".print_r($login_result,true)."</pre>";
  335.     //////////////////////////////////////////////////////
  336.     //get WOO Categorie -------------------------------------
  337.     $terms = get_the_terms($post_id, 'product_cat');
  338.     foreach ($terms as $term) { $product_cat=$term; break; }
  339.     if ($product_cat->name!='') { $product_cat_title=str_replace("'","`",$product_cat->name); }
  340.     $aos_product_categories_entry_id=get_term_meta($product_cat->term_id, 'aos_product_categories_entry_id', true);
  341.     $aos_product_categories_entry_id=$aos_product_categories_entry_id.'';
  342.     $query=(($aos_product_categories_entry_id!='')?"aos_product_categories.id='".$aos_product_categories_entry_id."'":"aos_product_categories.name='".$product_cat_title."'");
  343.     //////////////////////////////////////////////////////
  344.     //get AOS_Product_Categories -------------------------------------
  345.     $get_entry_list_parameters = array(
  346.         'session' => $session_id,
  347.         'module_name' => 'AOS_Product_Categories',
  348.         'query' => $query,     //The SQL WHERE clause without the word "where".
  349.         'order_by' => "",                              //The SQL ORDER BY clause without the phrase "order by".
  350.         'offset' => '0',                               //The record offset from which to start.
  351.         'select_fields' => array(                      //Optional. A list of fields to include in the results.
  352.             'id',
  353.             'name',
  354.         ),
  355.         'link_name_to_fields_array' => array(),
  356.         'max_results' => '1',      //The maximum number of results to return.
  357.         'deleted' => '0',          //To exclude deleted records
  358.         'Favorites' => false,      //If only records marked as favorites should be returned.
  359.     );
  360.     $get_entry_list_result = call('get_entry_list', $get_entry_list_parameters, $url); //echo "<pre>".print_r($get_entry_list_result,true)."</pre>";
  361.     $aos_product_categories_entry_id = $get_entry_list_result->entry_list[0]->id; //echo "<pre>AOS_Product_Categories LOAD : ".print_r($aos_product_categories_entry_id,true)."</pre>";
  362.     //////////////////////////////////////////////////////
  363.     //create or update AOS_Product_Categories -------------------------------------
  364.     $set_entry_parameters = array(
  365.         "session" => $session_id,
  366.         "module_name" => "AOS_Product_Categories",
  367.         "name_value_list" => array(
  368.             array("name" => "assigned_user_name", "value" => 'Administrator'),
  369.             array("name" => "assigned_user_id", "value" => '1'),
  370.             array("name" => "id", "value" => $aos_product_categories_entry_id.''), // TO UPDATE
  371.             array("name" => "name", "value" => $product_cat_title),
  372.             array("name" => "is_parent", "value" => "1"),
  373.         ),
  374.     );
  375.     $set_entry_result = call("set_entry", $set_entry_parameters, $url); //echo "<pre>".print_r($set_entry_result,true)."</pre>";
  376.     $aos_product_categories_entry_id = $set_entry_result->id; //echo "<pre>AOS_Product_Categories CREATE OR UPDATE : ".print_r($aos_product_categories_entry_id,true)."</pre>";
  377.     update_term_meta($product_cat->term_id, 'aos_product_categories_entry_id', $aos_product_categories_entry_id);
  378.     //////////////////////////////////////////////////////
  379.     //get WOO Title -------------------------------------
  380.     $product_title=str_replace("'","`",$the_product->get_title());
  381.     $product_sku=str_replace("'","`",$the_product->get_sku());
  382.     $product_price=str_replace("'","`",$the_product->get_price());
  383.     $product_url=get_permalink($post_id);
  384.     $aos_products_entry_id=get_post_meta($post_id, 'aos_products_entry_id',true).'';
  385.     $aos_products_entry_id=$aos_products_entry_id.'';
  386.     $query="aos_products.id='".$aos_products_entry_id."'";
  387.     //////////////////////////////////////////////////////
  388.     //get AOS_Products -------------------------------------
  389.     $get_entry_list_parameters = array(
  390.         'session' => $session_id,
  391.         'module_name' => 'AOS_Products',
  392.         'query' => $query,     //The SQL WHERE clause without the word "where".
  393.         'order_by' => "",                              //The SQL ORDER BY clause without the phrase "order by".
  394.         'offset' => '0',                               //The record offset from which to start.
  395.         'select_fields' => array(                      //Optional. A list of fields to include in the results.
  396.             'id',
  397.             'name',
  398.         ),
  399.         'link_name_to_fields_array' => array(),
  400.         'max_results' => '1',      //The maximum number of results to return.
  401.         'deleted' => '0',          //To exclude deleted records
  402.         'Favorites' => false,      //If only records marked as favorites should be returned.
  403.     );
  404.     $get_entry_list_result = call('get_entry_list', $get_entry_list_parameters, $url); //echo "<pre>".print_r($get_entry_list_result,true)."</pre>";
  405.     $aos_products_entry_id = $get_entry_list_result->entry_list[0]->id; //echo "<pre>AOS_Products LOAD : ".print_r($aos_products_entry_id,true)."</pre>";
  406.     //////////////////////////////////////////////////////
  407.     //create or update AOS_Products -------------------------------------
  408.     $set_entry_parameters = array(
  409.         "session" => $session_id,
  410.         "module_name" => "AOS_Products",
  411.         "name_value_list" => array(
  412.             array("name" => "assigned_user_name", "value" => 'Administrator'),
  413.             array("name" => "assigned_user_id", "value" => '1'),
  414.             array("name" => "id", "value" => $aos_products_entry_id.''), // TO UPDATE
  415.             array("name" => "currency_id", "value" => "-99"),
  416.             array("name" => "name", "value" => $product_title),
  417.             array("name" => "description", "value" => $product_title),
  418.             array("name" => "maincode", "value" => "XXXX"),
  419.             array("name" => "part_number", "value" => $product_sku),
  420.             array("name" => "type", "value" => "Service"),
  421.             array("name" => "cost", "value" => "0"),
  422.             array("name" => "cost_usdollar", "value" => "0"),
  423.             array("name" => "currency_id", "value" => "-99"),
  424.             array("name" => "price", "value" => $product_price),
  425.             array("name" => "price_usdollar", "value" => $product_price),
  426.             array("name" => "url", "value" => $product_url),
  427.             array("name" => "aos_product_category_id", "value" => $aos_product_categories_entry_id),
  428.             array("name" => "duree_c", "value" => (strip_tags(get_field('duree',$post_id))+0) ),
  429.             /**
  430.             array("name" => "public_concerne_c", "value" => get_field('public_concerne',$post_id) ),
  431.             array("name" => "pre_requis_c", "value" => get_field('pre-requis',$post_id) ),
  432.             array("name" => "but_de_la_formation_c", "value" => get_field('but_de_la_formation',$post_id) ),
  433.             array("name" => "programme_c", "value" => get_field('programme',$post_id) ),
  434.             array("name" => "methodes_pedagogiques_c", "value" => get_field('methodes_pedagogiques',$post_id) ),
  435.             array("name" => "moyens_pedagogiques_c", "value" => get_field('moyens_pedagogiques',$post_id) ),
  436.             array("name" => "equipements_necessaires_c", "value" => get_field('equipements_necessaires',$post_id) ),
  437.             array("name" => "documents_necessaires_c", "value" => get_field('documents_necessaires',$post_id) ),
  438.             array("name" => "fin_de_la_formation_c", "value" => get_field('fin_de_la_formation',$post_id) ),
  439.              **/
  440.         ),
  441.     );
  442.     $set_entry_result = call("set_entry", $set_entry_parameters, $url); //echo "<pre>".print_r($set_entry_result,true)."</pre>";
  443.     ////////////////////////////////////////////////////
  444.     // BUG AVEC LES CHAMPS HTML
  445.     $db_host="127.0.0.1"; $db_user="root"; $db_pass="root"; $db_db="suitecrm";
  446.     $con=mysqli_connect($db_host, $db_user,$db_pass,$db_db) or die("erreur de connexion au serveur");
  447.     if (mysqli_connect_errno()) { printf("Échec de la connexion : %s\n", mysqli_connect_error()); exit(); }
  448.     mysqli_query($con,"SET NAMES UTF8");
  449.     mysqli_query($con,"UPDATE aos_products_cstm SET public_concerne_c='".str_replace("'","\\'",get_field('public_concerne',$post_id))."' WHERE id_c='".$aos_products_entry_id."'");
  450.     mysqli_query($con,"UPDATE aos_products_cstm SET pre_requis_c='".str_replace("'","\\'",get_field('pre-requis',$post_id))."' WHERE id_c='".$aos_products_entry_id."'");
  451.     mysqli_query($con,"UPDATE aos_products_cstm SET but_de_la_formation_c='".str_replace("'","\\'",get_field('but_de_la_formation',$post_id))."' WHERE id_c='".$aos_products_entry_id."'");
  452.     mysqli_query($con,"UPDATE aos_products_cstm SET programme_c='".str_replace("'","\\'",get_field('programme',$post_id))."' WHERE id_c='".$aos_products_entry_id."'");
  453.     mysqli_query($con,"UPDATE aos_products_cstm SET methodes_pedagogiques_c='".str_replace("'","\\'",get_field('methodes_pedagogiques',$post_id))."' WHERE id_c='".$aos_products_entry_id."'");
  454.     mysqli_query($con,"UPDATE aos_products_cstm SET moyens_pedagogiques_c='".str_replace("'","\\'",get_field('methodes_pedagogiques',$post_id))."' WHERE id_c='".$aos_products_entry_id."'");
  455.     mysqli_query($con,"UPDATE aos_products_cstm SET equipements_necessaires_c='".str_replace("'","\\'",get_field('equipements_necessaires',$post_id))."' WHERE id_c='".$aos_products_entry_id."'");
  456.     mysqli_query($con,"UPDATE aos_products_cstm SET documents_necessaires_c='".str_replace("'","\\'",get_field('documents_necessaires',$post_id))."' WHERE id_c='".$aos_products_entry_id."'");
  457.     mysqli_query($con,"UPDATE aos_products_cstm SET fin_de_la_formation_c='".str_replace("'","\\'",get_field('fin_de_la_formation',$post_id))."' WHERE id_c='".$aos_products_entry_id."'");
  458.     ////////////////////////////////////////////////////
  459.     $aos_products_entry_id = $set_entry_result->id; //echo "<pre>AOS_Products CREATE OR UPDATE : ".print_r($aos_products_entry_id,true)."</pre>";
  460.     update_post_meta($post_id, 'aos_products_entry_id',$aos_products_entry_id);
  461. }
  462.  
  463.  
  464.  
  465.  
  466.  
  467.  
  468.  
  469.  
  470.  
  471.  
  472.  
  473. /////////////////////////////////////////////////////////////////////////////////////
  474. // ADD META FIELD account_entry_id... TO USER PROFIL
  475. function crf_show_extra_profile_fields( $user ) {
  476.     ?>
  477.     <h3>Informations supplémentaires</h3>
  478.     <table class="form-table">
  479.         <tr>
  480.             <th><label for="account_entry_id">account_entry_id</label></th>
  481.             <td><input type="text" id="account_entry_id" name="account_entry_id" value="<?php echo esc_html( get_the_author_meta( 'account_entry_id', $user->ID ) ); ?>" class="regular-text" /></td>
  482.         </tr>
  483.         <tr>
  484.             <th><label for="contact_entry_id">contact_entry_id</label></th>
  485.             <td><input type="text" id="contact_entry_id" name="contact_entry_id" value="<?php echo esc_html( get_the_author_meta( 'contact_entry_id', $user->ID ) ); ?>" class="regular-text" /></td>
  486.         </tr>
  487.         <tr>
  488.             <th><label for="formateur_entry_id">formateur_entry_id</label></th>
  489.             <td><input type="text" id="formateur_entry_id" name="formateur_entry_id" value="<?php echo esc_html( get_the_author_meta( 'formateur_entry_id', $user->ID ) ); ?>" class="regular-text" /></td>
  490.         </tr>
  491.         <tr>
  492.             <th><label for="siret">siret</label></th>
  493.             <td><input type="text" id="siret" name="siret" value="<?php echo esc_html( get_the_author_meta( 'siret', $user->ID ) ); ?>" class="regular-text" /></td>
  494.         </tr>
  495.     </table>
  496.     <?php
  497. }
  498. add_action( 'show_user_profile', 'crf_show_extra_profile_fields' );
  499. add_action( 'edit_user_profile', 'crf_show_extra_profile_fields' );
  500. ///
  501. function crf_update_profile_fields( $user_id ) {
  502.     if ( ! current_user_can( 'edit_user', $user_id ) ) { return false; }
  503.     if ( ! empty( $_POST['account_entry_id'] )) { update_user_meta( $user_id, 'account_entry_id', $_POST['account_entry_id'] ); }
  504.     if ( ! empty( $_POST['contact_entry_id'] )) { update_user_meta( $user_id, 'contact_entry_id', $_POST['contact_entry_id'] ); }
  505.     if ( ! empty( $_POST['formateur_entry_id'] )) { update_user_meta( $user_id, 'formateur_entry_id', $_POST['formateur_entry_id'] ); }
  506.     if ( ! empty( $_POST['siret'] )) { update_user_meta( $user_id, 'siret', $_POST['siret'] ); }
  507. }
  508. add_action( 'personal_options_update', 'crf_update_profile_fields' );
  509. add_action( 'edit_user_profile_update', 'crf_update_profile_fields' );
  510.  
  511.  
  512.  
  513.  
  514.  
  515.  
  516.  
  517.  
  518. /////////////////////////////////////////////////////////////////////////////////////
  519. //ADD META FIELD aos_product_categories_entry_id TO PRODUCT CATEGORY
  520. function wh_taxonomy_edit_meta_field($term) {
  521.     //getting term ID
  522.     $term_id = $term->term_id;
  523.     $aos_product_categories_entry_id = get_term_meta($term_id, 'aos_product_categories_entry_id', true);
  524.     ?>
  525.     <tr class="form-field">
  526.         <th scope="row" valign="top"><label for="aos_product_categories_entry_id"><?php _e('aos_product_categories_entry_id', 'wh'); ?></label></th>
  527.         <td>
  528.             <input type="text" name="aos_product_categories_entry_id" id="aos_product_categories_entry_id" value="<?php echo esc_attr($aos_product_categories_entry_id) ? esc_attr($aos_product_categories_entry_id) : ''; ?>">
  529.             <p class="description"><?php _e('ID dans SuiteCRM de la catégorie de produit équivalente (modifié et crée par le système)', 'wh'); ?></p>
  530.         </td>
  531.     </tr>
  532.     <?php
  533. }
  534. add_action('product_cat_edit_form_fields', 'wh_taxonomy_edit_meta_field', 10, 1);
  535. function wh_save_taxonomy_custom_meta($term_id) {
  536.     if($term_id=='88') { return 0; } //DEVIS CAT
  537.     if($term_id=='89') { return 0; } //FACTURE CAT
  538.     /////////////////////////////////////////////////////////////////////////////////////
  539.     $url = "http://suitecrm.straformation.fr/service/v4_1/rest.php";
  540.     $username = "admin";
  541.     $password = "waRR10RP0wa";
  542.     function call($method, $parameters, $url) {
  543.         ob_start();
  544.         $curl_request = curl_init();
  545.         curl_setopt($curl_request, CURLOPT_URL, $url);
  546.         curl_setopt($curl_request, CURLOPT_POST, 1);
  547.         curl_setopt($curl_request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
  548.         curl_setopt($curl_request, CURLOPT_HEADER, 1);
  549.         curl_setopt($curl_request, CURLOPT_SSL_VERIFYPEER, 0);
  550.         curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1);
  551.         curl_setopt($curl_request, CURLOPT_FOLLOWLOCATION, 0);
  552.         $jsonEncodedData = json_encode($parameters);
  553.         $post = array(
  554.             "method" => $method,
  555.             "input_type" => "JSON",
  556.             "response_type" => "JSON",
  557.             "rest_data" => $jsonEncodedData
  558.         );
  559.         curl_setopt($curl_request, CURLOPT_POSTFIELDS, $post);
  560.         $result = curl_exec($curl_request);
  561.  
  562.         curl_close($curl_request);
  563.         $result = explode("\r\n\r\n", $result, 2);
  564.         $response = json_decode($result[1]);
  565.         ob_end_flush();
  566.         return $response;
  567.     }
  568.     //////////////////////////////////////////////////////
  569.     //login ---------------------------------------------
  570.     $login_parameters = array(
  571.         "user_auth" => array(
  572.             "user_name" => $username,
  573.             "password" => md5($password),
  574.             "version" => "1"
  575.         ),
  576.         "application_name" => "Woocommerce",
  577.         "name_value_list" => array(),
  578.     );
  579.     $login_result = call("login", $login_parameters, $url); $session_id = $login_result->id; //echo "<pre>".print_r($login_result,true)."</pre>";
  580.     //////////////////////////////////////////////////////
  581.     //get WOO Categorie -------------------------------------
  582.     $product_cat=get_term($term_id, 'product_cat');
  583.     if ($product_cat->name!='') { $product_cat_title=str_replace("'","`",$product_cat->name); }
  584.     $aos_product_categories_entry_id=get_term_meta($term_id, 'aos_product_categories_entry_id', true);
  585.     $aos_product_categories_entry_id=$aos_product_categories_entry_id.'';
  586.     $query="aos_product_categories.id='".$aos_product_categories_entry_id."'";
  587.     //////////////////////////////////////////////////////
  588.     //get AOS_Product_Categories -------------------------------------
  589.     $get_entry_list_parameters = array(
  590.         'session' => $session_id,
  591.         'module_name' => 'AOS_Product_Categories',
  592.         'query' => $query,     //The SQL WHERE clause without the word "where".
  593.         'order_by' => "",                              //The SQL ORDER BY clause without the phrase "order by".
  594.         'offset' => '0',                               //The record offset from which to start.
  595.         'select_fields' => array(                      //Optional. A list of fields to include in the results.
  596.             'id',
  597.             'name',
  598.         ),
  599.         'link_name_to_fields_array' => array(),
  600.         'max_results' => '1',      //The maximum number of results to return.
  601.         'deleted' => '0',          //To exclude deleted records
  602.         'Favorites' => false,      //If only records marked as favorites should be returned.
  603.     );
  604.     $get_entry_list_result = call('get_entry_list', $get_entry_list_parameters, $url); //echo "<pre>".print_r($get_entry_list_result,true)."</pre>";
  605.     $aos_product_categories_entry_id = $get_entry_list_result->entry_list[0]->id; //echo "<pre>AOS_Product_Categories LOAD : ".print_r($aos_product_categories_entry_id,true)."</pre>";
  606.     //////////////////////////////////////////////////////
  607.     //create or update AOS_Product_Categories -------------------------------------
  608.     $set_entry_parameters = array(
  609.         "session" => $session_id,
  610.         "module_name" => "AOS_Product_Categories",
  611.         "name_value_list" => array(
  612.             array("name" => "assigned_user_name", "value" => 'Administrator'),
  613.             array("name" => "assigned_user_id", "value" => '1'),
  614.             array("name" => "id", "value" => $aos_product_categories_entry_id.''), // TO UPDATE
  615.             array("name" => "name", "value" => $product_cat_title),
  616.             array("name" => "is_parent", "value" => "1"),
  617.         ),
  618.     );
  619.     $set_entry_result = call("set_entry", $set_entry_parameters, $url); //echo "<pre>".print_r($set_entry_result,true)."</pre>";
  620.     $aos_product_categories_entry_id = $set_entry_result->id; //echo "<pre>AOS_Product_Categories CREATE OR UPDATE : ".print_r($aos_product_categories_entry_id,true)."</pre>";
  621.     update_term_meta($term_id, 'aos_product_categories_entry_id', $aos_product_categories_entry_id);
  622. }
  623. add_action('edited_product_cat', 'wh_save_taxonomy_custom_meta', 10, 1);
  624. add_action('create_product_cat', 'wh_save_taxonomy_custom_meta', 10, 1);
  625.  
  626.  
  627.  
  628.  
  629.  
  630.  
  631.  
  632.  
  633.  
  634.  
  635.  
  636.  
  637.  
  638.  
  639.  
  640.  
  641.  
  642.  
  643.  
  644.  
  645.  
  646. /////////////////////////////////////////////////////////////////////////////////////
  647. //When USER Change MyAccount Infos save them to CRM
  648. function action_woocommerce_save_account_details( $user_id ) {
  649.     $user=wp_get_current_user();
  650.     //echo '<pre>'.print_r($_POST,true).'</pre>'; exit;
  651.     /////////////////////////////////////////////////////////////////////////////////////
  652.     $url = "http://suitecrm.straformation.fr/service/v4_1/rest.php";
  653.     $username = "admin";
  654.     $password = "waRR10RP0wa";
  655.     function call($method, $parameters, $url) {
  656.         ob_start();
  657.         $curl_request = curl_init();
  658.         curl_setopt($curl_request, CURLOPT_URL, $url);
  659.         curl_setopt($curl_request, CURLOPT_POST, 1);
  660.         curl_setopt($curl_request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
  661.         curl_setopt($curl_request, CURLOPT_HEADER, 1);
  662.         curl_setopt($curl_request, CURLOPT_SSL_VERIFYPEER, 0);
  663.         curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1);
  664.         curl_setopt($curl_request, CURLOPT_FOLLOWLOCATION, 0);
  665.         $jsonEncodedData = json_encode($parameters);
  666.         $post = array(
  667.             "method" => $method,
  668.             "input_type" => "JSON",
  669.             "response_type" => "JSON",
  670.             "rest_data" => $jsonEncodedData
  671.         );
  672.         curl_setopt($curl_request, CURLOPT_POSTFIELDS, $post);
  673.         $result = curl_exec($curl_request);
  674.         curl_close($curl_request);
  675.         $result = explode("\r\n\r\n", $result, 2);
  676.         $response = json_decode($result[1]);
  677.         ob_end_flush();
  678.         return $response;
  679.     }
  680.     //////////////////////////////////////////////////////
  681.     //login ---------------------------------------------
  682.     $login_parameters = array(
  683.         "user_auth" => array(
  684.             "user_name" => $username,
  685.             "password" => md5($password),
  686.             "version" => "1"
  687.         ),
  688.         "application_name" => "Woocommerce",
  689.         "name_value_list" => array(),
  690.     );
  691.     $login_result = call("login", $login_parameters, $url); $session_id = $login_result->id; //echo "<pre>".print_r($login_result,true)."</pre>";
  692.     ///
  693.     $account_entry_id=get_the_author_meta('account_entry_id',$user_id);
  694.  
  695.     if($user->roles[0]=='formateur') {
  696.         $formateur_entry_id=get_the_author_meta('formateur_entry_id',$user_id).'';
  697.         //////////////////////////////////////////////////////
  698.         //get formateur -------------------------------------
  699.         $get_entry_list_parameters = array(
  700.             'session' => $session_id,
  701.             'module_name' => 'sess_Formateurs',
  702.             'query' => "sess_formateurs.id='".$formateur_entry_id."'", //The SQL WHERE clause without the word "where".
  703.             'order_by' => "",                              //The SQL ORDER BY clause without the phrase "order by".
  704.             'offset' => '0',                               //The record offset from which to start.
  705.             'select_fields' => array(                      //Optional. A list of fields to include in the results.
  706.                 'id',
  707.                 'name',
  708.             ),
  709.             'link_name_to_fields_array' => array(),
  710.             'max_results' => '1',      //The maximum number of results to return.
  711.             'deleted' => '0',          //To exclude deleted records
  712.             'Favorites' => false,      //If only records marked as favorites should be returned.
  713.         );
  714.         $get_entry_list_result_formateur = call('get_entry_list', $get_entry_list_parameters, $url);
  715.         $formateur_entry_id = $get_entry_list_result_formateur->entry_list[0]->id;
  716.         //echo "<pre>Formateurs LOAD : ".print_r($formateur_entry_id,true)."</pre>"; //echo "<pre style='font-size:9px;'>".print_r($get_entry_list_result_formateur,true)."</pre>";
  717.         if(isset($_POST['billing_company'])) {
  718.             //////////////////////////////////////////////////////
  719.             //create or update formateur -------------------------------------
  720.             $formateur_fname=str_replace("\\'","'",$_POST['billing_first_name']);
  721.             $formateur_lname=str_replace("\\'","'",$_POST['billing_last_name']);
  722.             $addr1=str_replace("\\'","'",$_POST['billing_address_1']);
  723.             $addr2=str_replace("\\'","'",$_POST['billing_address_2']);
  724.             $cp=str_replace("\\'","'",$_POST['billing_postcode']);
  725.             $ville=str_replace("\\'","'",$_POST['billing_city']);
  726.             $pays='France';
  727.             $tel=str_replace("\\'","'",$_POST['billing_phone']);
  728.             $email=str_replace("\\'","'",$_POST['billing_email']);
  729.             //ADD_COMMENT
  730.             $commentaire=str_replace("\\'","'",$_POST['order_comments']);
  731.             ///
  732.             $set_entry_parameters = array(
  733.                 "session" => $session_id,
  734.                 "module_name" => "sess_Formateurs",
  735.                 "name_value_list" => array(
  736.                     array("name" => "assigned_user_name", "value" => 'Administrator'),
  737.                     array("name" => "assigned_user_id", "value" => '1'),
  738.                     array("name" => "id", "value" => $formateur_entry_id.""), // TO UPDATE
  739.                     array("name" => "name", "value" => $formateur_fname.' '.$formateur_lname),
  740.                     array("name" => "full_name", "value" => $formateur_fname.' '.$formateur_lname),
  741.                     //array("name" => "salutation", "value" => ""),
  742.                     array("name" => "first_name", "value" => $formateur_fname),
  743.                     array("name" => "last_name", "value" => $formateur_lname),
  744.                     //array("name" => "description", "value" => ""),
  745.                     array("name" => "primary_address_street", "value" => $addr1),
  746.                     array("name" => "primary_address_street_2", "value" => $addr2),
  747.                     array("name" => "primary_address_city", "value" => $ville),
  748.                     array("name" => "primary_address_postalcode", "value" => $cp),
  749.                     array("name" => "primary_address_country", "value" => $pays),
  750.                     array("name" => "phone_home", "value" => $tel),
  751.                     array("name" => "phone_mobile", "value" => $tel),
  752.                     array("name" => "phone_work", "value" => $tel),
  753.                     array("name" => "phone_other", "value" => $tel),
  754.                     //array("name" => "website", "value" => ""),
  755.                     array("name" => "email1", "value" => $email),
  756.                     //array("name" => "birthdate", "value" => ''),
  757.                     //ADD_COMMENT
  758.                     array("name" => "order_comments", "value" => $commentaire),
  759.                 ),
  760.             );
  761.         } else {
  762.             $formateur_fname=str_replace("\\'","'",$_POST['account_first_name']);
  763.             $formateur_lname=str_replace("\\'","'",$_POST['account_last_name']);
  764.             $email=str_replace("\\'","'",$_POST['account_email']);
  765.             //ADD_COMMENT
  766.             $commentaire=str_replace("\\'","'",$_POST['order_comments']);
  767.             ///
  768.             $set_entry_parameters = array(
  769.                 "session" => $session_id,
  770.                 "module_name" => "sess_Formateurs",
  771.                 "name_value_list" => array(
  772.                     array("name" => "assigned_user_name", "value" => 'Administrator'),
  773.                     array("name" => "assigned_user_id", "value" => '1'),
  774.                     array("name" => "id", "value" => $formateur_entry_id.""), // TO UPDATE
  775.                     array("name" => "name", "value" => $formateur_fname.' '.$formateur_lname),
  776.                     array("name" => "full_name", "value" => $formateur_fname.' '.$formateur_lname),
  777.                     //array("name" => "salutation", "value" => ""),
  778.                     array("name" => "first_name", "value" => $formateur_fname),
  779.                     array("name" => "last_name", "value" => $formateur_lname),
  780.                     //array("name" => "description", "value" => ""),
  781.                     //array("name" => "primary_address_street", "value" => $addr1),
  782.                     //array("name" => "primary_address_street_2", "value" => $addr2),
  783.                     //array("name" => "primary_address_city", "value" => $ville),
  784.                     //array("name" => "primary_address_postalcode", "value" => $cp),
  785.                     //array("name" => "primary_address_country", "value" => $pays),
  786.                     //array("name" => "phone_home", "value" => $tel),
  787.                     //array("name" => "phone_mobile", "value" => $tel),
  788.                     //array("name" => "phone_work", "value" => $tel),
  789.                     //array("name" => "phone_other", "value" => $tel),
  790.                     //array("name" => "website", "value" => ""),
  791.                     array("name" => "email1", "value" => $email),
  792.                     //array("name" => "lead_source", "value" => 'Web Site'),
  793.                     //array("name" => "birthdate", "value" => ''),
  794.                     //ADD_COMMENT
  795.                     array("name" => "order_comments", "value" => $commentaire),
  796.                 ),
  797.             );
  798.         }
  799.         $set_entry_result_formateur = call("set_entry", $set_entry_parameters, $url);
  800.         $formateur_entry_id = $set_entry_result_formateur->id;
  801.         update_user_meta($user_id,'formateur_entry_id',$formateur_entry_id);
  802.         //echo "<pre>Formateurs CREATE OR UPDATE : ".print_r($formateur_entry_id,true)."</pre>"; //echo "<pre style='font-size:9px;'>".print_r($set_entry_result_formateur,true)."</pre>";
  803.     } elseif($user->roles[0]=='stagiaire') {
  804.         $contact_entry_id=get_the_author_meta('contact_entry_id',$user_id).'';
  805.  
  806.         //////////////////////////////////////////////////////
  807.         //get contact -------------------------------------
  808.         $get_entry_list_parameters = array(
  809.             'session' => $session_id,
  810.             'module_name' => 'Contacts',
  811.             'query' => "contacts.id='".$contact_entry_id."'",     //The SQL WHERE clause without the word "where".
  812.             'order_by' => "",                              //The SQL ORDER BY clause without the phrase "order by".
  813.             'offset' => '0',                               //The record offset from which to start.
  814.             'select_fields' => array(                      //Optional. A list of fields to include in the results.
  815.                 'id',
  816.                 'name',
  817.             ),
  818.             'link_name_to_fields_array' => array(),
  819.             'max_results' => '1',      //The maximum number of results to return.
  820.             'deleted' => '0',          //To exclude deleted records
  821.             'Favorites' => false,      //If only records marked as favorites should be returned.
  822.         );
  823.         $get_entry_list_result_contact = call('get_entry_list', $get_entry_list_parameters, $url);
  824.         $contact_entry_id = $get_entry_list_result_contact->entry_list[0]->id;
  825.         //echo "<pre>Contacts LOAD : ".print_r($contact_entry_id,true)."</pre>"; //echo "<pre style='font-size:9px;'>".print_r($get_entry_list_result_contact,true)."</pre>";
  826.         if(isset($_POST['billing_company'])) {
  827.             //////////////////////////////////////////////////////
  828.             //create or update contact -------------------------------------
  829.             $account_entry_id=get_the_author_meta('account_entry_id',$user_id).'';
  830.             $contact_fname=str_replace("\\'","'",$_POST['billing_first_name']);
  831.             $contact_lname=str_replace("\\'","'",$_POST['billing_last_name']);
  832.             $addr1=str_replace("\\'","'",$_POST['billing_address_1']);
  833.             $addr2=str_replace("\\'","'",$_POST['billing_address_2']);
  834.             $cp=str_replace("\\'","'",$_POST['billing_postcode']);
  835.             $ville=str_replace("\\'","'",$_POST['billing_city']);
  836.             $pays='France';
  837.             $tel=str_replace("\\'","'",$_POST['billing_phone']);
  838.             $email=str_replace("\\'","'",$_POST['billing_email']);
  839.             //ADD_COMMENT
  840.             $commentaire=str_replace("\\'","'",$_POST['order_comments']);
  841.             ///
  842.             $set_entry_parameters = array(
  843.                 "session" => $session_id,
  844.                 "module_name" => "Contacts",
  845.                 "name_value_list" => array(
  846.                     //array("name" => "assigned_user_name", "value" => 'Administrator'),
  847.                     //array("name" => "assigned_user_id", "value" => '1'),
  848.                     array("name" => "id", "value" => $contact_entry_id.""), // TO UPDATE
  849.                     array("name" => "account_id", "value" => $account_entry_id),
  850.                     array("name" => "name", "value" => $contact_fname.' '.$contact_lname),
  851.                     array("name" => "full_name", "value" => $contact_fname.' '.$contact_lname),
  852.                     //array("name" => "salutation", "value" => ""),
  853.                     array("name" => "first_name", "value" => $contact_fname),
  854.                     array("name" => "last_name", "value" => $contact_lname),
  855.                     //array("name" => "description", "value" => ""),
  856.                     array("name" => "primary_address_street", "value" => $addr1),
  857.                     array("name" => "primary_address_street_2", "value" => $addr2),
  858.                     array("name" => "primary_address_city", "value" => $ville),
  859.                     array("name" => "primary_address_postalcode", "value" => $cp),
  860.                     array("name" => "primary_address_country", "value" => $pays),
  861.                     array("name" => "phone_home", "value" => $tel),
  862.                     array("name" => "phone_mobile", "value" => $tel),
  863.                     array("name" => "phone_work", "value" => $tel),
  864.                     array("name" => "phone_other", "value" => $tel),
  865.                     //array("name" => "website", "value" => ""),
  866.                     array("name" => "email1", "value" => $email),
  867.                     //array("name" => "birthdate", "value" => ''),
  868.                     //ADD_COMMENT
  869.                     array("name" => "order_comments", "value" => $commentaire),
  870.                 ),
  871.             );
  872.         } else {
  873.             $contact_fname=str_replace("\\'","'",$_POST['account_first_name']);
  874.             $contact_lname=str_replace("\\'","'",$_POST['account_last_name']);
  875.             $email=str_replace("\\'","'",$_POST['account_email']);
  876.             //ADD_COMMENT
  877.             $commentaire=str_replace("\\'","'",$_POST['order_comments']);
  878.             ///
  879.             $set_entry_parameters = array(
  880.                 "session" => $session_id,
  881.                 "module_name" => "Contacts",
  882.                 "name_value_list" => array(
  883.                     //array("name" => "assigned_user_name", "value" => 'Administrator'),
  884.                     //array("name" => "assigned_user_id", "value" => '1'),
  885.                     array("name" => "id", "value" => $contact_entry_id.""), // TO UPDATE
  886.                     array("name" => "account_id", "value" => $account_entry_id),
  887.                     array("name" => "name", "value" => $contact_fname.' '.$contact_lname),
  888.                     array("name" => "full_name", "value" => $contact_fname.' '.$contact_lname),
  889.                     //array("name" => "salutation", "value" => ""),
  890.                     array("name" => "first_name", "value" => $contact_fname),
  891.                     array("name" => "last_name", "value" => $contact_lname),
  892.                     //array("name" => "description", "value" => ""),
  893.                     //array("name" => "primary_address_street", "value" => $addr1),
  894.                     //array("name" => "primary_address_street_2", "value" => $addr2),
  895.                     //array("name" => "primary_address_city", "value" => $ville),
  896.                     //array("name" => "primary_address_postalcode", "value" => $cp),
  897.                     //array("name" => "primary_address_country", "value" => $pays),
  898.                     //array("name" => "phone_home", "value" => $tel),
  899.                     //array("name" => "phone_mobile", "value" => $tel),
  900.                     //array("name" => "phone_work", "value" => $tel),
  901.                     //array("name" => "phone_other", "value" => $tel),
  902.                     //array("name" => "website", "value" => ""),
  903.                     array("name" => "email1", "value" => $email),
  904.                     //array("name" => "lead_source", "value" => 'Web Site'),
  905.                     //array("name" => "birthdate", "value" => ''),
  906.                     //ADD_COMMENT
  907.                     array("name" => "order_comments", "value" => $commentaire),
  908.                 ),
  909.             );
  910.         }
  911.         $set_entry_result_contact = call("set_entry", $set_entry_parameters, $url);
  912.         $contact_entry_id = $set_entry_result_contact->id;
  913.         update_user_meta($user_id,'contact_entry_id',$contact_entry_id);
  914.         //echo "<pre>Contacts CREATE OR UPDATE : ".print_r($contact_entry_id,true)."</pre>"; //echo "<pre style='font-size:9px;'>".print_r($set_entry_result_contact,true)."</pre>";
  915.     } elseif($user->roles[0]=='customer' || $user->roles[0]=='administrator') {
  916.         //////////////////////////////////////////////////////
  917.         //get account -------------------------------------
  918.         $get_entry_list_parameters = array(
  919.             'session' => $session_id,
  920.             'module_name' => 'Accounts',
  921.             'query' => "accounts.id='".$account_entry_id."'",     //The SQL WHERE clause without the word "where".
  922.             'order_by' => "",                              //The SQL ORDER BY clause without the phrase "order by".
  923.             'offset' => '0',                               //The record offset from which to start.
  924.             'select_fields' => array(                      //Optional. A list of fields to include in the results.
  925.                 'id',
  926.                 'name',
  927.             ),
  928.             'link_name_to_fields_array' => array(),
  929.             'max_results' => '1',      //The maximum number of results to return.
  930.             'deleted' => '0',          //To exclude deleted records
  931.             'Favorites' => false,      //If only records marked as favorites should be returned.
  932.         );
  933.         $get_entry_list_result_account = call('get_entry_list', $get_entry_list_parameters, $url);
  934.         $account_entry_id = $get_entry_list_result_account->entry_list[0]->id;
  935.         //echo "<pre>Accounts LOAD : ".print_r($account_entry_id,true)."</pre>"; //echo "<pre style='font-size:9px;'>".print_r($get_entry_list_result_account,true)."</pre>";
  936.         //////////////////////////////////////////////////////
  937.         //create or update account -------------------------------------
  938.         if(isset($_POST['billing_company'])) {
  939.             $_POST['billing_company']=str_replace("\\'","'",$_POST['billing_company']);
  940.             $_POST['billing_first_name']=str_replace("\\'","'",$_POST['billing_first_name']);
  941.             $_POST['billing_last_name']=str_replace("\\'","'",$_POST['billing_last_name']);
  942.             $_POST['billing_email']=str_replace("\\'","'",$_POST['billing_email']);
  943.             $_POST['billing_country']=str_replace("FR",'France',$_POST['billing_country']);
  944.             $_POST['billing_address_1']=str_replace("\\'","'",$_POST['billing_address_1']);
  945.             $_POST['billing_address_2']=str_replace("\\'","'",$_POST['billing_address_2']);
  946.             $_POST['billing_postcode']=str_replace("\\'","'",$_POST['billing_postcode']);
  947.             $_POST['billing_city']=str_replace("\\'","'",$_POST['billing_city']);
  948.             $_POST['billing_state']=str_replace("\\'","'",$_POST['billing_state']);
  949.             $_POST['billing_phone']=str_replace("\\'","'",$_POST['billing_phone']);
  950.             $_POST['order_comments']=str_replace("\\'","'",$_POST['order_comments']);
  951.             $set_entry_parameters = array(
  952.                 "session" => $session_id,
  953.                 "module_name" => "Accounts",
  954.                 "name_value_list" => array(
  955.                     //array("name" => "assigned_user_name", "value" => 'Administrator'),
  956.                     //array("name" => "assigned_user_id", "value" => '1'),
  957.                     array("name" => "id", "value" => $account_entry_id.""), // TO UPDATE
  958.                     array("name" => "name", "value" => $_POST['billing_company']),
  959.                     array("name" => "fname_c", "value" => $_POST['billing_first_name']),
  960.                     array("name" => "lname_c", "value" => $_POST['billing_last_name']),
  961.                     //array("name" => "description", "value" => ""),
  962.                     //array("name" => "account_type", "value" => "Customer"),
  963.                     array("name" => "billing_address_street", "value" => $_POST['billing_address_1']),
  964.                     array("name" => "billing_address_street_2", "value" => $_POST['billing_address_2']),
  965.                     array("name" => "billing_address_city", "value" => $_POST['billing_city']),
  966.                     array("name" => "billing_address_postalcode", "value" => $_POST['billing_postcode']),
  967.                     array("name" => "billing_address_country", "value" => $_POST['billing_country']),
  968.                     array("name" => "phone_office", "value" => $_POST['billing_phone']),
  969.                     //array("name" => "website", "value" => ""),
  970.                     array("name" => "email1", "value" => $_POST['billing_email']),
  971.                     array("name" => "order_comments", "value" => $_POST['order_comments']),
  972.                 ),
  973.             );
  974.         } else {
  975.             $_POST['account_first_name']=str_replace("\\'","'",$_POST['account_first_name']);
  976.             $_POST['account_last_name']=str_replace("\\'","'",$_POST['account_last_name']);
  977.             $_POST['account_email']=str_replace("\\'","'",$_POST['account_email']);
  978.             $set_entry_parameters = array(
  979.                 "session" => $session_id,
  980.                 "module_name" => "Accounts",
  981.                 "name_value_list" => array(
  982.                     //array("name" => "assigned_user_name", "value" => 'Administrator'),
  983.                     //array("name" => "assigned_user_id", "value" => '1'),
  984.                     array("name" => "id", "value" => $account_entry_id.""), // TO UPDATE
  985.                     //array("name" => "name", "value" => $account_compagny),
  986.                     array("name" => "fname_c", "value" => $_POST['account_first_name']),
  987.                     array("name" => "lname_c", "value" => $_POST['account_last_name']),
  988.                     //array("name" => "description", "value" => ""),
  989.                     //array("name" => "account_type", "value" => "Customer"),
  990.                     //array("name" => "billing_address_street", "value" => $addr1),
  991.                     //array("name" => "billing_address_street_2", "value" => $addr2),
  992.                     //array("name" => "billing_address_city", "value" => $ville),
  993.                     //array("name" => "billing_address_postalcode", "value" => $cp),
  994.                     //array("name" => "billing_address_country", "value" => $pays),
  995.                     //array("name" => "phone_office", "value" => $tel),
  996.                     //array("name" => "website", "value" => ""),
  997.                     array("name" => "email1", "value" => $_POST['account_email']),
  998.                 ),
  999.             );
  1000.         }
  1001.         $set_entry_result_account = call("set_entry", $set_entry_parameters, $url);
  1002.         $account_entry_id = $set_entry_result_account->id;
  1003.         //update_user_meta($order->get_user_id(),'account_entry_id',$account_entry_id);
  1004.         //update_post_meta($order->get_id(), 'account_entry_id', $account_entry_id);
  1005.         //echo "<pre>Accounts CREATE OR UPDATE : ".print_r($account_entry_id,true)."</pre>"; //echo "<pre style='font-size:9px;'>".print_r($set_entry_result_account,true)."</pre>";
  1006.     }
  1007. }
  1008. add_action( 'woocommerce_save_account_details', 'action_woocommerce_save_account_details', 10, 1 );
  1009. add_action( 'woocommerce_customer_save_address', 'action_woocommerce_save_account_details', 10, 1 );
  1010.  
  1011.  
  1012.  
  1013.  
  1014.  
  1015.  
  1016.  
  1017.  
  1018.  
  1019.  
  1020.  
  1021.  
  1022.  
  1023.  
  1024.  
  1025.  
  1026.  
  1027.  
  1028.  
  1029.  
  1030.  
  1031.  
  1032.  
  1033.  
  1034. /////////////////////////////////////////////////////////////////////////////////////
  1035. //WIDGET PRODUCTS BY CATEGORY
  1036. if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
  1037. class Bi_Widget_Product extends WP_Widget
  1038. {
  1039.  
  1040.     function __construct()
  1041.     {
  1042.         parent::__construct(
  1043.             'woocommerce_ndbProducts',
  1044.             __( 'Products by category', 'ndb' ),
  1045.             array('description' => __( 'This plugin displays a list of products of a certain category on your website.', 'ndb' ))
  1046.         );
  1047.     }
  1048.     public function widget( $args, $instance ) {
  1049.         extract( $args );
  1050.         $title = apply_filters( 'widget_title', $instance['title']);
  1051.         $ids   = $instance['ids'];
  1052.         $arr_id = explode(',',$ids);
  1053.         $query_args = array(
  1054.             'orderby' => 'menu_order ID',
  1055.             'order'   => 'ASC',
  1056.             'post_status'    => 'publish',
  1057.             'post_type'      => 'product',
  1058.             'no_found_rows'  => 1,
  1059.             'posts_per_page' => -1, // -1 needed for displaying all posts of the category
  1060.             'tax_query' => array(
  1061.                 array(
  1062.                     'taxonomy' => 'product_cat', // The taxonomy we want to query
  1063.                     'field' => 'slug', // We use the slug to find our terms
  1064.                     'terms' => $arr_id // Our comma seperated list of product categories
  1065.                 )
  1066.             )
  1067.         );
  1068.         $r = new WP_Query( $query_args );
  1069.         if ( $r->have_posts() ) {
  1070.             echo $before_widget;
  1071.             if ( $title ) { echo $before_title . $title . $after_title; }
  1072.             echo '<ul class="product_list_widget">';
  1073.             while ( $r->have_posts()) {
  1074.                 $r->the_post();
  1075.                 global $product;
  1076.                 ?><li><a href="<?php echo esc_url( get_permalink( $product->id ) ); ?>" title="<?php echo esc_attr( $product->get_title() ); ?>"><?php echo $product->get_title(); ?></a></li><?php
  1077.             }
  1078.             echo '</ul>';
  1079.             echo $after_widget;
  1080.         }
  1081.         wp_reset_postdata();
  1082.     }
  1083.     // Widget Backend
  1084.     public function form($instance){
  1085.         $title = (isset($instance['title'])) ? $instance['title'] : __( '', 'ndb' );
  1086.         $ids = $instance['ids'];
  1087.         ?>
  1088.         <p>
  1089.             <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
  1090.             <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>"  name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
  1091.         </p>
  1092.         <p>
  1093.             <label for="<?php echo $this->get_field_id('ids'); ?>"><?php _e('Category:'); ?></label>
  1094.             <input class="widefat" id="<?php echo $this->get_field_id('ids'); ?>"  name="<?php echo $this->get_field_name( 'ids' ); ?>" type="text" value="<?php echo esc_attr( $ids ); ?>" />
  1095.         </p>
  1096.         <?php
  1097.     }
  1098.     public function update( $new_instance, $old_instance ) {
  1099.         $instance = array();
  1100.         $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
  1101.         $instance['ids'] = ( ! empty( $new_instance['ids'] ) ) ? strip_tags( $new_instance['ids'] ) : '';
  1102.         return $instance;
  1103.     }
  1104. }
  1105. add_action( 'widgets_init', function(){ register_widget( 'Bi_Widget_Product' ); } );
  1106.  
  1107.  
  1108.  
  1109.  
  1110.  
  1111.  
  1112.  
  1113.  
  1114.  
  1115.  
  1116.  
  1117.  
  1118.  
  1119.  
  1120.  
  1121.  
  1122.  
  1123.  
  1124.  
  1125.  
  1126.  
  1127.  
  1128. /////////////////////////////////////////////////////////////////////////////////////
  1129. //CHANGE DEFAULT ORDER STATUTS LABEL
  1130. function wc_renaming_order_status( $order_statuses ) {
  1131.     foreach ( $order_statuses as $key => $status ) {
  1132.         $new_order_statuses[ $key ] = $status;
  1133.         if ( 'wc-processing' === $key ) { $order_statuses['wc-processing'] = _x( 'Vous attendez le devis en cours de réalisation par StraFormation.', 'Order status', 'woocommerce' ); }
  1134.     }
  1135.     return $order_statuses;
  1136. }
  1137. add_filter( 'wc_order_statuses', 'wc_renaming_order_status' );
  1138. /////////////////////////////////////////////////////////////////////////////////////
  1139. //ADD EXTRA ORDER STATUTS
  1140. function register_new_order_status() {
  1141.     register_post_status( 'wc-devis-fait', array(
  1142.         'label'                     => 'StraFormation attend que le devis soit signé et renvoyé.',
  1143.         'public'                    => true,
  1144.         'exclude_from_search'       => false,
  1145.         'show_in_admin_all_list'    => true,
  1146.         'show_in_admin_status_list' => true,
  1147.         'label_count'               => _n_noop( 'Attente devis signé <span class="count">(%s)</span>', 'Attente devis signé <span class="count">(%s)</span>' )
  1148.     ) );
  1149.     register_post_status( 'wc-devis-signe', array(
  1150.         'label'                     => 'Vous devez avoir rempli les détails des stagiaires. StraFormation a reçu le devis signé et prépare la suite de votre dossier.',
  1151.         'public'                    => true,
  1152.         'exclude_from_search'       => false,
  1153.         'show_in_admin_all_list'    => true,
  1154.         'show_in_admin_status_list' => true,
  1155.         'label_count'               => _n_noop( 'Devis signé <span class="count">(%s)</span>', 'Devis signé <span class="count">(%s)</span>' )
  1156.     ) );
  1157.     register_post_status( 'wc-dossier-fin', array(
  1158.         'label'                     => 'StraFormation a fini de préparer votre dossier.',
  1159.         'public'                    => true,
  1160.         'exclude_from_search'       => false,
  1161.         'show_in_admin_all_list'    => true,
  1162.         'show_in_admin_status_list' => true,
  1163.         'label_count'               => _n_noop( 'Dossier prêt <span class="count">(%s)</span>', 'Dossier prêt <span class="count">(%s)</span>' )
  1164.     ) );
  1165. }
  1166. add_action( 'init', 'register_new_order_status' );
  1167. ///
  1168. function add_new_order_status_to_order_statuses( $order_statuses ) {
  1169.     $new_order_statuses = array();
  1170.     // add new order status after processing
  1171.     foreach ( $order_statuses as $key => $status ) {
  1172.         $new_order_statuses[ $key ] = $status;
  1173.         if ( 'wc-processing' === $key ) {
  1174.             $new_order_statuses['wc-devis-fait'] = 'StraFormation attend que le devis soit signé et renvoyé.';
  1175.             $new_order_statuses['wc-devis-signe'] = 'Vous devez avoir rempli les détails des stagiaires. StraFormation a reçu le devis signé et prépare la suite de votre dossier.';
  1176.             $new_order_statuses['wc-dossier-fin'] = 'StraFormation a fini de préparer votre dossier. La formation peut commencer.';
  1177.         }
  1178.     }
  1179.     return $new_order_statuses;
  1180. }
  1181. add_filter( 'wc_order_statuses', 'add_new_order_status_to_order_statuses' );
  1182. ///
  1183. function change_order_statuts_to_processing($order_id) {
  1184.     $order=wc_get_order($order_id);
  1185.     $order->update_status('wc-processing', 'Vous attendez le devis en cours de réalisation par StraFormation.');
  1186. }
  1187. function change_order_statuts_to_devis_fait($order_id) {
  1188.     $order=wc_get_order($order_id);
  1189.     $order->update_status('wc-devis-fait', 'Le client a reçu le devis. StraFormation attend que le devis soit signé et renvoyé.');
  1190. }
  1191. function change_order_statuts_to_devis_signe($order_id) {
  1192.     $order=wc_get_order($order_id);
  1193.     $order->update_status('wc-devis-signe', 'Vous devez avoir rempli les détails des stagiaires. StraFormation a reçu le devis signé et prépare la suite du dossier.');
  1194. }
  1195. function change_order_statuts_to_dossier_fin($order_id) {
  1196.     $order=wc_get_order($order_id);
  1197.     $order->update_status('wc-dossier-fin', 'StraFormation a préparé la suite du dossier. On attend la fin de la formation et le paiement pour termnier la vente.');
  1198. }
  1199. function change_order_statuts_to_completed($order_id) {
  1200.     $order=wc_get_order($order_id);
  1201.     $order->update_status('completed', 'StraFormation a reçu le paiement et a termnié la vente. Votre facture est disponible');
  1202. }
  1203. ///
  1204. function add_download_permissions($data, $order) {
  1205.     if ( $order->has_status( 'processing' ) ) { return true; }
  1206.     if ( $order->has_status( 'devis-fait' ) ) { return true; }
  1207.     if ( $order->has_status( 'devis-signe' ) ) { return true; }
  1208.     if ( $order->has_status( 'dossier-fin' ) ) { return true; }
  1209.     if ( $order->has_status( 'completed' ) ) { return true; }
  1210.     return $data;
  1211. }
  1212. add_filter('woocommerce_order_is_download_permitted', 'add_download_permissions', 10, 2);
  1213.  
  1214.  
  1215.  
  1216.  
  1217.  
  1218.  
  1219.  
  1220.  
  1221.  
  1222.  
  1223.  
  1224.  
  1225.  
  1226.  
  1227.  
  1228.  
  1229.  
  1230.  
  1231. /////////////////////////////////////////////////////////////////////////////////////
  1232. // ADD META CONTAINER ADMIN WOO ORDER PAGES
  1233. if ( ! function_exists( 'mv_add_meta_boxes' ) ) {
  1234.     function mv_add_meta_boxes() { add_meta_box( 'mv_other_fields', 'Détail des stagiaires', 'mv_add_other_fields_for_packaging', 'shop_order', 'normal', 'core' ); }
  1235. }
  1236. add_action( 'add_meta_boxes', 'mv_add_meta_boxes' );
  1237.  
  1238. // Adding Meta field in the meta container admin shop_order pages
  1239. if ( ! function_exists( 'mv_add_other_fields_for_packaging' ) ) {
  1240.     function mv_add_other_fields_for_packaging() {
  1241.         global $post;
  1242.         $order = wc_get_order($post->ID);
  1243.         $order_id = $order->get_id();
  1244.         $post=json_decode(get_post_meta($order->get_id(), 'post_form',true), true);
  1245.         $zz=0;
  1246.         foreach ($order->get_items() as $item_key => $item_values): $zzz++;
  1247.             $item_id = $item_values->get_id();
  1248.             $item_name = $item_values->get_name();
  1249.             $product_id = $item_values->get_product_id();
  1250.             $wc_product = $item_values->get_product();
  1251.             $item_data = $item_values->get_data();
  1252.             $product_name = $item_data['name'];
  1253.             $product_id = $item_data['product_id'];
  1254.             $variation_id = $item_data['variation_id'];
  1255.             $quantity = $item_data['quantity'];
  1256.             echo '<h3 style="width:100%;">Formation : '.$product_name.'</h3>';
  1257.             for($z=1;$z<=$quantity;$z++) { $zz++;
  1258.                 $stagiaire=woo_get_customer_by_billing_email($post['email'][($zz-1)]);
  1259.                 if($stagiaire) { $link='<div style="text-align:right;"><a href="http://straformation.test/wp-admin/user-edit.php?user_id='.$stagiaire->get_id().'">Afficher le stagiaire</a></div>'; } else { $link=''; }
  1260.                 echo '<div style="width:100%;">
  1261.                 <h4 style="width:100%;">Personne '.$z.' qui suivra la formation : '.$product_name.'</h4>
  1262.                   '.$link.'
  1263.                   <input type="hidden" name="account_entry_id[]" value="'.get_post_meta($order->get_id(), 'account_entry_id',true).'">
  1264.                   <input type="hidden" name="aos_quote_entry_id[]" value="'.get_post_meta($order->get_id(), 'aos_quote_entry_id',true).'">
  1265.                   <input type="hidden" name="aos_line_item_groups_entry_id[]" value="'.get_post_meta($order->get_id(), 'aos_line_item_groups_entry_id',true).'">
  1266.                   <input type="hidden" name="aos_products_entry_id[]" value="'.get_post_meta($order->get_id(), 'aos_products_entry_id_'.($zzz-1),true).'">
  1267.                   <input type="hidden" name="aos_product_categories_entry_id[]" value="'.get_post_meta($order->get_id(), 'aos_product_categories_entry_id_'.($zzz-1),true).'">
  1268.                   <input type="hidden" name="aos_products_quotes_entry_id[]" value="'.get_post_meta($order->get_id(), 'aos_products_quotes_entry_id_'.($zzz-1),true).'">
  1269.                   <input type="hidden" name="contact_entry_id[]" value="'.get_post_meta($order->get_id(), 'contact_entry_id_'.($zz-1),true).'">
  1270.                   <div style="width:100%;">
  1271.                     <div style="width:100%;text-align:right;">
  1272.                       <label for="nom'.$zz.'">Nom :</label>
  1273.                       <input value="'.$post['nom'][($zz-1)].'" id="nom'.$zz.'" type="text" name="nom[]" placeholder="Nom" '.(is_file('/var/www/clients/client0/web1/web/docs/devis_'.$order->get_id().'_'.get_post_meta($order->get_id(), 'aos_quote_entry_id',true).'_signed.pdf')?'':'readonly').'>
  1274.                     </div>
  1275.                     <div style="width:100%;text-align:right;">
  1276.                       <label for="prenom'.$zz.'">Prénom :</label>
  1277.                       <input value="'.$post['prenom'][($zz-1)].'" id="prenom'.$zz.'" type="text" name="prenom[]" placeholder="Prénom" '.(is_file('/var/www/clients/client0/web1/web/docs/devis_'.$order->get_id().'_'.get_post_meta($order->get_id(), 'aos_quote_entry_id',true).'_signed.pdf')?'':'readonly').'>
  1278.                     </div>
  1279.                   </div>
  1280.                   <div>
  1281.                     <div style="width:100%;text-align:right;">
  1282.                       <label for="addr1'.$zz.'">Adresse 1 :</label>
  1283.                       <input value="'.$post['addr1'][($zz-1)].'" id="addr1'.$zz.'" type="text" name="addr1[]" placeholder="Adresse ligne 1" '.(is_file('/var/www/clients/client0/web1/web/docs/devis_'.$order->get_id().'_'.get_post_meta($order->get_id(), 'aos_quote_entry_id',true).'_signed.pdf')?'':'readonly').'>
  1284.                     </div>
  1285.                     <div style="width:100%;text-align:right;">
  1286.                       <label for="addr2'.$zz.'">Adresse 2 :</label>
  1287.                       <input value="'.$post['addr2'][($zz-1)].'" id="addr2'.$zz.'" type="text" name="addr2[]" placeholder="Adresse ligne 2" '.(is_file('/var/www/clients/client0/web1/web/docs/devis_'.$order->get_id().'_'.get_post_meta($order->get_id(), 'aos_quote_entry_id',true).'_signed.pdf')?'':'readonly').'>
  1288.                     </div>
  1289.                   </div>
  1290.                   <div>
  1291.                     <div style="width:100%;text-align:right;">
  1292.                       <label for="cp'.$zz.'">Code postal :</label>
  1293.                       <input value="'.$post['cp'][($zz-1)].'" id="cp'.$zz.'" type="text" name="cp[]" placeholder="Code postal" pattern="(([0-8][0-9])|(9[0-5])|(2[ab])|(2[AB]))[0-9]{3}" '.(is_file('/var/www/clients/client0/web1/web/docs/devis_'.$order->get_id().'_'.get_post_meta($order->get_id(), 'aos_quote_entry_id',true).'_signed.pdf')?'':'readonly').'>
  1294.                     </div>
  1295.                     <div style="width:100%;text-align:right;">
  1296.                       <label for="ville'.$zz.'">Ville :</label>
  1297.                       <input value="'.$post['ville'][($zz-1)].'" id="ville'.$zz.'" type="text" name="ville[]" placeholder="Ville" '.(is_file('/var/www/clients/client0/web1/web/docs/devis_'.$order->get_id().'_'.get_post_meta($order->get_id(), 'aos_quote_entry_id',true).'_signed.pdf')?'':'readonly').'>
  1298.                     </div>
  1299.                     <div style="width:100%;text-align:right;">
  1300.                       <label for="pays'.$zz.'">Pays :</label>
  1301.                       <input value="'.$post['pays'][($zz-1)].'" id="pays'.$zz.'" type="text" name="pays[]" placeholder="Pays" value="France" '.(is_file('/var/www/clients/client0/web1/web/docs/devis_'.$order->get_id().'_'.get_post_meta($order->get_id(), 'aos_quote_entry_id',true).'_signed.pdf')?'':'readonly').'>
  1302.                     </div>
  1303.                   </div>
  1304.                   <div>
  1305.                     <div style="width:100%;text-align:right;">
  1306.                       <label for="tel'.$zz.'">Téléphone :</label>
  1307.                       <input value="'.$post['tel'][($zz-1)].'" id="tel'.$zz.'" type="tel" name="tel[]" placeholder="Téléphone" pattern="[0-9]{10}" '.(is_file('/var/www/clients/client0/web1/web/docs/devis_'.$order->get_id().'_'.get_post_meta($order->get_id(), 'aos_quote_entry_id',true).'_signed.pdf')?'':'readonly').'>
  1308.                     </div>
  1309.                     <div style="width:100%;text-align:right;">
  1310.                       <label for="email'.$zz.'">Email :</label>
  1311.                       <input value="'.$post['email'][($zz-1)].'" id="email'.$zz.'" type="email" name="email[]" placeholder="Email" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}" '.(is_file('/var/www/clients/client0/web1/web/docs/devis_'.$order->get_id().'_'.get_post_meta($order->get_id(), 'aos_quote_entry_id',true).'_signed.pdf')?'':'readonly').'>
  1312.                     </div>
  1313.                   </div>
  1314.                   <div>
  1315.                     <div style="width:100%;text-align:right;">
  1316.                       <label for="date'.$zz.'">Date de début souhaitée pour la formation :</label>
  1317.                       <input value="'.(isset($post['date'][($zz-1)])?$post['date'][($zz-1)]:date_i18n('Y-m-d')).'" id="date'.$zz.'" type="date" name="date[]" '.(!is_file('/var/www/clients/client0/web1/web/docs/devis_'.$order->get_id().'_'.get_post_meta($order->get_id(), 'aos_quote_entry_id',true).'_signed.pdf')?'':'readonly').'>
  1318.                     </div>
  1319.                     <div style="width:100%;text-align:right;">
  1320.                       <label for="nbr_h'.$zz.'">Nombre d\'heure(s) de formation :</label>
  1321.                       <input value="'.(isset($post['nbr_h'][($zz-1)])?$post['nbr_h'][($zz-1)]:(strip_tags(get_post_meta($product_id, 'duree',true))+0)).'" id="nbr_h'.$zz.'" type="number" name="nbr_h[]" step="1" min="1" max="999" '.(!is_file('/var/www/clients/client0/web1/web/docs/devis_'.$order->get_id().'_'.get_post_meta($order->get_id(), 'aos_quote_entry_id',true).'_signed.pdf')?'':'readonly').'>
  1322.                     </div>
  1323.                   </div>
  1324.                 </div>';
  1325.             }
  1326.         endforeach;
  1327.         echo '<button type="submit" class="button save_order button-primary" name="save" value="Mettre à jour">Mettre à jour</button>';
  1328.     }
  1329. }
  1330. // Save the data of the Meta field
  1331. add_action( 'save_post', 'mv_save_wc_order_other_fields', 10, 1 );
  1332. if ( ! function_exists( 'mv_save_wc_order_other_fields' ) ) {
  1333.     function mv_save_wc_order_other_fields( $post_id ) {
  1334.         if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { return $post_id; }
  1335.         if ( ! current_user_can( 'edit_post', $post_id ) ) { return $post_id; }
  1336.         if ( !isset($_POST['aos_quote_entry_id']) ) { return $post_id; }
  1337.         if ( get_post_type($post_id)!='shop_order' ) { return $post_id; }
  1338.  
  1339.         $post['account_entry_id']=$_POST['account_entry_id'];
  1340.         $post['aos_quote_entry_id']=$_POST['aos_quote_entry_id'];
  1341.         $post['aos_line_item_groups_entry_id']=$_POST['aos_line_item_groups_entry_id'];
  1342.         $post['aos_products_quotes_entry_id']=$_POST['aos_products_quotes_entry_id'];
  1343.         $post['aos_products_entry_id']=$_POST['aos_products_entry_id'];
  1344.         $post['aos_product_categories_entry_id']=$_POST['aos_product_categories_entry_id'];
  1345.         $post['contact_entry_id']=$_POST['contact_entry_id'];
  1346.         $post['date']=$_POST['date'];
  1347.         $post['nbr_h']=$_POST['nbr_h'];
  1348.         $post['nom']=$_POST['nom'];
  1349.         $post['prenom']=$_POST['prenom'];
  1350.         $post['addr1']=$_POST['addr1'];
  1351.         $post['addr2']=$_POST['addr2'];
  1352.         $post['cp']=$_POST['cp'];
  1353.         $post['ville']=$_POST['ville'];
  1354.         $post['pays']=$_POST['pays'];
  1355.         $post['tel']=$_POST['tel'];
  1356.         $post['email']=$_POST['email'];
  1357.         $p=json_encode($post);
  1358.         //file_put_contents('/var/www/clients/client0/web1/web/docs/formulaire_'.$post_id.'_'.$post['aos_quote_entry_id'][0].'.json',$p);
  1359.         update_post_meta($post_id, 'post_form', $p);
  1360.  
  1361.  
  1362.  
  1363. ///////////////////////////////////////////////////////////////////////
  1364.         if(isset($_POST['account_entry_id'])) {
  1365.             $order = wc_get_order($post_id);
  1366.             $order_id = $order->get_id();
  1367.             $post=json_decode(get_post_meta($order->get_id(), 'post_form',true), true);
  1368.             $url = "http://suitecrm.straformation.fr/service/v4_1/rest.php";
  1369.             $username = "admin";
  1370.             $password = "waRR10RP0wa";
  1371.             function call($method, $parameters, $url) {
  1372.                 ob_start();
  1373.                 $curl_request = curl_init();
  1374.                 curl_setopt($curl_request, CURLOPT_URL, $url);
  1375.                 curl_setopt($curl_request, CURLOPT_POST, 1);
  1376.                 curl_setopt($curl_request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
  1377.                 curl_setopt($curl_request, CURLOPT_HEADER, 1);
  1378.                 curl_setopt($curl_request, CURLOPT_SSL_VERIFYPEER, 0);
  1379.                 curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1);
  1380.                 curl_setopt($curl_request, CURLOPT_FOLLOWLOCATION, 0);
  1381.                 $jsonEncodedData = json_encode($parameters);
  1382.                 $post = array(
  1383.                     "method" => $method,
  1384.                     "input_type" => "JSON",
  1385.                     "response_type" => "JSON",
  1386.                     "rest_data" => $jsonEncodedData
  1387.                 );
  1388.                 curl_setopt($curl_request, CURLOPT_POSTFIELDS, $post);
  1389.                 $result = curl_exec($curl_request);
  1390.                 curl_close($curl_request);
  1391.                 $result = explode("\r\n\r\n", $result, 2);
  1392.                 $response = json_decode($result[1]);
  1393.                 ob_end_flush();
  1394.                 return $response;
  1395.             }
  1396.             //////////////////////////////////////////////////////
  1397.             //login ---------------------------------------------
  1398.             $login_parameters = array(
  1399.                 "user_auth" => array(
  1400.                     "user_name" => $username,
  1401.                     "password" => md5($password),
  1402.                     "version" => "1"
  1403.                 ),
  1404.                 "application_name" => "Woocommerce",
  1405.                 "name_value_list" => array(),
  1406.             );
  1407.             $login_result = call("login", $login_parameters, $url); $session_id = $login_result->id; //echo "<pre>".print_r($login_result,true)."</pre>";
  1408.             ///
  1409.             $current_user = get_user_by('id',$order->get_user_id());
  1410.             for($z=0;$z<count($post['nom']);$z++) {
  1411.                 //////////////////////////////////////////////////////
  1412.                 //get WOO account -------------------------------------
  1413.                 $user=woo_get_customer_by_billing_email($post['email'][$z]);
  1414.                 $username=sanitize_title($post['prenom'][$z].'.'.$post['nom'][$z].'.'.$post['cp'][$z]);
  1415.                 //CREER WOO ACCOUNT STARGIAIRE
  1416.                 if($user===false && $post['email'][$z]!='' && $current_user->user_email!=$post['email'][$z] && username_exists($username)==false && email_exists($post['email'][$z])==false) {
  1417.                     $password=wp_generate_password();
  1418.                     ///
  1419.                     add_filter( 'woocommerce_new_customer_data', 'my_new_customer_data');
  1420.                     ///
  1421.                     $user_id = wc_create_new_customer($post['email'][$z], $username, $password);
  1422.                     update_user_meta( $user_id, "billing_last_name",$post['nom'][$z]);
  1423.                     update_user_meta( $user_id, "billing_first_name",$post['prenom'][$z]);
  1424.                     update_user_meta( $user_id, "billing_company",'');
  1425.                     update_user_meta( $user_id, "billing_address_1",$post['addr1'][$z]);
  1426.                     update_user_meta( $user_id, "billing_address_2",$post['addr2'][$z]);
  1427.                     update_user_meta( $user_id, "billing_postcode",$post['cp'][$z]);
  1428.                     update_user_meta( $user_id, "billing_city",$post['ville'][$z]);
  1429.                     update_user_meta( $user_id, "billing_country",'FR');
  1430.                     update_user_meta( $user_id, "billing_phone",$post['tel'][$z]);
  1431.                     update_user_meta( $user_id, "billing_email",$post['email'][$z]);
  1432.                     $user=woo_get_customer_by_billing_email($post['email'][$z]);
  1433.                     wp_update_user( array ('ID' => $user->get_id(), 'display_name' => $post['prenom'][$z].' '.$post['nom'][$z]) );
  1434.                     wp_update_user( array ('ID' => $user->get_id(), 'first_name' => $post['prenom'][$z]) );
  1435.                     wp_update_user( array ('ID' => $user->get_id(), 'last_name' => $post['nom'][$z]) );
  1436.                     ///
  1437.                     //TODO : dechinter !
  1438.                     //send_email($post['email'][$z],
  1439.                     send_email('serbastien.ferrand@gmail.com',
  1440.                         'Votre formation chez StraFormation',
  1441.                         'Bienvenue '.$post['prenom'][$z].' '.$post['nom'][$z].' chez StraFormation,<br>Un compte à été ouvert à votre attention pour votre formation chez StraFormation. Rendez-vous sur straformation.fr et connectez vous.',
  1442.                         'Identifiant : <b>'.$username.'</b> &nbsp;&nbsp;&nbsp; Mot de passe : <b>'.$password.'</b>',
  1443.                         'Nous vous conseillons de changer votre mot de passe sur StraFormation dans la section <a href="http://straformation.test/mon-compte/edit-account/" target="_blank">Mon compte > Détails du compte</a>.<br>'
  1444.                     );
  1445.                 }
  1446.                 //UPDATE WOO ACCOUNT STAGIAIRE
  1447.                 if($user!==false && $post['email'][$z]!='') {
  1448.                     if($current_user->user_email!=$post['email'][$z]) {
  1449.                         update_user_meta( $user->get_id(), "billing_last_name",$post['nom'][$z]);
  1450.                         update_user_meta( $user->get_id(), "billing_first_name",$post['prenom'][$z]);
  1451.                         update_user_meta( $user->get_id(), "billing_company",'');
  1452.                         update_user_meta( $user->get_id(), "billing_address_1",$post['addr1'][$z]);
  1453.                         update_user_meta( $user->get_id(), "billing_address_2",$post['addr2'][$z]);
  1454.                         update_user_meta( $user->get_id(), "billing_postcode",$post['cp'][$z]);
  1455.                         update_user_meta( $user->get_id(), "billing_city",$post['ville'][$z]);
  1456.                         update_user_meta( $user->get_id(), "billing_country",'FR');
  1457.                         update_user_meta( $user->get_id(), "billing_phone",$post['tel'][$z]);
  1458.                         update_user_meta( $user->get_id(), "billing_email",$post['email'][$z]);
  1459.                         wp_update_user( array ('ID' => $user->get_id(), 'display_name' => $post['prenom'][$z].' '.$post['nom'][$z]) );
  1460.                         wp_update_user( array ('ID' => $user->get_id(), 'first_name' => $post['prenom'][$z]) );
  1461.                         wp_update_user( array ('ID' => $user->get_id(), 'last_name' => $post['nom'][$z]) );
  1462.                     }
  1463.                     ///
  1464.                     $contact_entry_id=get_user_meta($user->get_id(), 'contact_entry_id', true ).'';
  1465.                     //////////////////////////////////////////////////////
  1466.                     //get contact -------------------------------------
  1467.                     $get_entry_list_parameters = array(
  1468.                         'session' => $session_id,
  1469.                         'module_name' => 'Contacts',
  1470.                         'query' => "contacts.id='".$contact_entry_id."'",     //The SQL WHERE clause without the word "where".
  1471.                         'order_by' => "",                              //The SQL ORDER BY clause without the phrase "order by".
  1472.                         'offset' => '0',                               //The record offset from which to start.
  1473.                         'select_fields' => array(                      //Optional. A list of fields to include in the results.
  1474.                             'id',
  1475.                             'name',
  1476.                         ),
  1477.                         'link_name_to_fields_array' => array(),
  1478.                         'max_results' => '1',      //The maximum number of results to return.
  1479.                         'deleted' => '0',          //To exclude deleted records
  1480.                         'Favorites' => false,      //If only records marked as favorites should be returned.
  1481.                     );
  1482.                     $get_entry_list_result_contact = call('get_entry_list', $get_entry_list_parameters, $url);
  1483.                     $contact_entry_id = $get_entry_list_result_contact->entry_list[0]->id;
  1484.                     //echo "<pre>Contacts LOAD : ".print_r($contact_entry_id,true)."</pre>"; //echo "<pre style='font-size:9px;'>".print_r($get_entry_list_result_contact,true)."</pre>";
  1485.                     //////////////////////////////////////////////////////
  1486.                     //create or update contact -------------------------------------
  1487.                     $account_entry_id=get_user_meta($order->get_user_id(), 'account_entry_id', true ).'';
  1488.                     $contact_fname=$post['prenom'][$z];
  1489.                     $contact_lname=$post['nom'][$z];
  1490.                     $addr1=$post['addr1'][$z];
  1491.                     $addr2=$post['addr2'][$z];
  1492.                     $cp=$post['cp'][$z];
  1493.                     $ville=$post['ville'][$z];
  1494.                     $pays=$post['pays'][$z];
  1495.                     $tel=$post['tel'][$z];
  1496.                     $email=$post['email'][$z];
  1497.                     ///
  1498.                     $set_entry_parameters = array(
  1499.                         "session" => $session_id,
  1500.                         "module_name" => "Contacts",
  1501.                         "name_value_list" => array(
  1502.                             array("name" => "assigned_user_name", "value" => 'Administrator'),
  1503.                             array("name" => "assigned_user_id", "value" => '1'),
  1504.                             array("name" => "id", "value" => $contact_entry_id.""), // TO UPDATE
  1505.                             array("name" => "account_id", "value" => $account_entry_id),
  1506.                             array("name" => "name", "value" => $contact_fname.' '.$contact_lname),
  1507.                             array("name" => "full_name", "value" => $contact_fname.' '.$contact_lname),
  1508.                             array("name" => "salutation", "value" => ""),
  1509.                             array("name" => "first_name", "value" => $contact_fname),
  1510.                             array("name" => "last_name", "value" => $contact_lname),
  1511.                             //array("name" => "description", "value" => ""),
  1512.                             array("name" => "primary_address_street", "value" => $addr1),
  1513.                             array("name" => "primary_address_street_2", "value" => $addr2),
  1514.                             array("name" => "primary_address_city", "value" => $ville),
  1515.                             array("name" => "primary_address_postalcode", "value" => $cp),
  1516.                             array("name" => "primary_address_country", "value" => $pays),
  1517.                             array("name" => "phone_home", "value" => $tel),
  1518.                             array("name" => "phone_mobile", "value" => $tel),
  1519.                             array("name" => "phone_work", "value" => $tel),
  1520.                             array("name" => "phone_other", "value" => $tel),
  1521.                             //array("name" => "website", "value" => ""),
  1522.                             array("name" => "email1", "value" => $email),
  1523.                             array("name" => "lead_source", "value" => 'Web Site'),
  1524.                             //array("name" => "birthdate", "value" => ''),
  1525.                         ),
  1526.                     );
  1527.                     $set_entry_result_contact = call("set_entry", $set_entry_parameters, $url);
  1528.                     $contact_entry_id = $set_entry_result_contact->id;
  1529.                     update_post_meta($order->get_id(), 'contact_entry_id_'.$z, $contact_entry_id);
  1530.                     update_post_meta($order->get_id(), 'woo_user_id_'.$z, $user->get_id());
  1531.                     update_user_meta($user->get_id(),'contact_entry_id',$contact_entry_id);
  1532.                     update_user_meta($user->get_id(),'woo_order_id',$order->get_id());
  1533.                     //echo "<pre>Contacts CREATE OR UPDATE contact_entry_id_".$z." : ".print_r($contact_entry_id,true)."</pre>"; //echo "<pre style='font-size:9px;'>".print_r($set_entry_result_contact,true)."</pre>";
  1534.                 }
  1535.             }
  1536.             //////////////////////////////////////////////////////////////////////////////////
  1537.             // RECALCULE DEVIS
  1538.             //////////////////////////////////////////////////////
  1539.             //get AOS_Quotes -------------------------------------
  1540.             $aos_quote_entry_id=get_post_meta($order->get_id(), 'aos_quote_entry_id', true ).'';
  1541.             $get_entry_list_parameters = array(
  1542.                 'session' => $session_id,
  1543.                 'module_name' => 'AOS_Quotes',
  1544.                 'query' => "aos_quotes.id='".$aos_quote_entry_id."'",     //The SQL WHERE clause without the word "where".
  1545.                 'order_by' => "",                              //The SQL ORDER BY clause without the phrase "order by".
  1546.                 'offset' => '0',                               //The record offset from which to start.
  1547.                 'select_fields' => array(                      //Optional. A list of fields to include in the results.
  1548.                     'id',
  1549.                     'name',
  1550.                     'stage',
  1551.                 ),
  1552.                 'link_name_to_fields_array' => array(),
  1553.                 'max_results' => '1',      //The maximum number of results to return.
  1554.                 'deleted' => '0',          //To exclude deleted records
  1555.                 'Favorites' => false,      //If only records marked as favorites should be returned.
  1556.             );
  1557.             $get_entry_list_result_aos_quote = call('get_entry_list', $get_entry_list_parameters, $url); //echo "<pre>".print_r($get_entry_list_result_aos_quote,true)."</pre>";
  1558.             $aos_quote_entry_id = $get_entry_list_result_aos_quote->entry_list[0]->id;
  1559.             ///
  1560.             if($get_entry_list_result_aos_quote->entry_list[0]->name_value_list->stage=='Draft') {
  1561.                 foreach ($order->get_items() as $key => $lineItem) {
  1562.                     $the_product = new WC_Product($lineItem->get_product_id());
  1563.                     $post_id=$lineItem->get_product_id();
  1564.                     $the_sku=$the_product->get_sku();
  1565.                     ///
  1566.                     $nbr_hours=0;
  1567.                     foreach($post['nbr_h'] as $k=>$v) {
  1568.                         if($post['aos_products_entry_id'][$k]==get_post_meta($post_id, 'aos_products_entry_id', true )) {
  1569.                             $nbr_hours+=($v+0);
  1570.                         }
  1571.                     }
  1572.                     ///
  1573.                     $quantity=$nbr_hours;
  1574.                     $prix=$the_product->get_price();
  1575.                     $product_qty=$nbr_hours;
  1576.                     $product_price=round(($lineItem->get_subtotal()+$lineItem->get_subtotal_tax())/($lineItem->get_quantity())+0,3);
  1577.                     $tot_ht+=($product_qty*$product_price);
  1578.                 }
  1579.                 $account_entry_id=get_post_meta($order->get_id(), 'account_entry_id', true ).'';
  1580.                 $aos_quote_entry_id=get_post_meta($order->get_id(), 'aos_quote_entry_id', true ).'';
  1581.                 $aos_line_item_groups_entry_id=get_post_meta($order->get_id(), 'aos_line_item_groups_entry_id', true ).'';
  1582.                 //////////////////////////////////////////////////////
  1583.                 // update aos_quotes -------------------------------------
  1584.                 $set_entry_parameters = array(
  1585.                     "session" => $session_id,
  1586.                     "module_name" => "AOS_Quotes",
  1587.                     "name_value_list" => array(
  1588.                         array("name" => "id", "value" => $aos_quote_entry_id), // TO UPDATE
  1589.                         array("name" => "total_amt", "value" => $tot_ht),
  1590.                         array("name" => "total_amt_usdollar", "value" => $tot_ht),
  1591.                         array("name" => "subtotal_amount", "value" => $tot_ht),
  1592.                         array("name" => "subtotal_amount_usdollar", "value" => $tot_ht),
  1593.                         array("name" => "discount_amount", "value" => "0"),
  1594.                         array("name" => "discount_amount_usdollar", "value" => "0"),
  1595.                         array("name" => "tax_amount", "value" => round($tot_ht*0.2,3)),
  1596.                         array("name" => "tax_amount_usdollar", "value" => round($tot_ht*0.2,3)),
  1597.                         array("name" => "shipping_amount", "value" => "0"),
  1598.                         array("name" => "shipping_amount_usdollar", "value" => "0"),
  1599.                         array("name" => "shipping_tax", "value" => "0"),
  1600.                         array("name" => "shipping_tax_amt", "value" => "0"),
  1601.                         array("name" => "shipping_tax_amt_usdollar", "value" => "0"),
  1602.                         array("name" => "total_amount", "value" => round($tot_ht*1.2,3)),
  1603.                         array("name" => "total_amount_usdollar", "value" => round($tot_ht*1.2,3)),
  1604.                     ),
  1605.                 );
  1606.                 $set_entry_result_aos_quote = call("set_entry", $set_entry_parameters, $url);
  1607.                 //////////////////////////////////////////////////////
  1608.                 // update AOS_Line_Item_Groups -------------------------------------
  1609.                 $set_entry_parameters = array(
  1610.                     "session" => $session_id,
  1611.                     "module_name" => "AOS_Line_Item_Groups",
  1612.                     "name_value_list" => array(
  1613.                         array("name" => "id", "value" => $aos_line_item_groups_entry_id.''), // TO UPDATE
  1614.                         array("name" => "total_amt", "value" => $tot_ht),
  1615.                         array("name" => "total_amt_usdollar", "value" => $tot_ht),
  1616.                         array("name" => "discount_amount", "value" => 0),
  1617.                         array("name" => "discount_amount_usdollar", "value" => 0),
  1618.                         array("name" => "subtotal_amount", "value" => $tot_ht),
  1619.                         array("name" => "subtotal_amount_usdollar", "value" => $tot_ht),
  1620.                         array("name" => "tax_amount", "value" => ($tot_ht*0.2)),
  1621.                         array("name" => "tax_amount_usdollar", "value" => ($tot_ht*0.2)),
  1622.                         array("name" => "subtotal_tax_amount", "value" => 0),
  1623.                         array("name" => "subtotal_tax_amount_usdollar", "value" => 0),
  1624.                         array("name" => "total_amount", "value" => ($tot_ht+($tot_ht*0.2))),
  1625.                         array("name" => "total_amount_usdollar", "value" => ($tot_ht+($tot_ht*0.2))),
  1626.                     ),
  1627.                 );
  1628.                 $set_entry_result_aos_line_item_groups = call("set_entry", $set_entry_parameters, $url);
  1629.                 //////////////////////////////////////////////////////
  1630.                 ///// FOR EACH ITEM CART
  1631.                 $tot_ht=0;
  1632.                 foreach ($order->get_items() as $key => $lineItem) { $zz++;
  1633.                     $aos_products_entry_id=get_post_meta($order->get_id(), 'aos_products_entry_id_'.($zz-1), true).'';
  1634.                     $post_id=$lineItem->get_product_id();
  1635.                     $the_product = new WC_Product($lineItem->get_product_id());
  1636.                     //////////////////////////////////////////////////////
  1637.                     //get WOO
  1638.                     $product_title=str_replace("'","`",$the_product->get_title());
  1639.                     $product_sku=str_replace("'","`",$the_product->get_sku());
  1640.                     $product_url=get_permalink($post_id);
  1641.                     $cost=get_field('cout',$post_id);
  1642.                     ///
  1643.                     $nbr_hours=0;
  1644.                     foreach($post['nbr_h'] as $k=>$v) {
  1645.                         if($post['aos_products_entry_id'][$k]==get_post_meta($post_id, 'aos_products_entry_id', true )) {
  1646.                             $nbr_hours+=($v+0);
  1647.                         }
  1648.                     }
  1649.                     ///
  1650.                     $product_qty=$nbr_hours;
  1651.                     $product_price=round(($lineItem->get_subtotal()+$lineItem->get_subtotal_tax())/($lineItem->get_quantity())+0,3);
  1652.                     $tot_ht+=($product_qty*$product_price);
  1653.                     //////////////////////////////////////////////////////
  1654.                     // update AOS_Products_Quotes -------------------------------------
  1655.                     $aos_products_quotes_entry_id=get_post_meta($order->get_id(), 'aos_products_quotes_entry_id_'.($zz-1), true).'';
  1656.                     $set_entry_parameters = array(
  1657.                         "session" => $session_id,
  1658.                         "module_name" => "AOS_Products_Quotes",
  1659.                         "name_value_list" => array(
  1660.                             array("name" => "assigned_user_name", "value" => 'Administrator'),
  1661.                             array("name" => "assigned_user_id", "value" => '1'),
  1662.                             array("name" => "id", "value" => $aos_products_quotes_entry_id.''), // TO UPDATE
  1663.                             array("name" => "currency_id", "value" => "-99"),
  1664.                             array("name" => "name", "value" => $product_title),
  1665.                             array("name" => "product_cost_price", "value" => $cost),
  1666.                             array("name" => "product_cost_price_usdollar", "value" => $cost),
  1667.                             array("name" => "description", "value" => ''),
  1668.                             array("name" => "item_description", "value" => $product_title),
  1669.                             array("name" => "maincode", "value" => "XXXX"),
  1670.                             array("name" => "part_number", "value" => $product_sku),
  1671.                             array("name" => "currency_id", "value" => "-99"),
  1672.                             array("name" => "product_list_price", "value" => $product_price),
  1673.                             array("name" => "product_list_price_usdollar", "value" => $product_price),
  1674.                             array("name" => "product_discount", "value" => 0),
  1675.                             array("name" => "product_discount_usdollar", "value" => 0),
  1676.                             array("name" => "product_discount_amount", "value" => 0),
  1677.                             array("name" => "product_discount_amount_usdollar", "value" => 0),
  1678.                             array("name" => "discount", "value" => "Percentage"),
  1679.                             array("name" => "product_unit_price", "value" => $product_price),
  1680.                             array("name" => "product_unit_price_usdollar", "value" => $product_price),
  1681.                             array("name" => "vat_amt", "value" => ($product_price*0.2)),
  1682.                             array("name" => "vat_amt_usdollar", "value" => ($product_price*0.2)),
  1683.                             array("name" => "product_total_price", "value" => ($product_qty*$product_price)),
  1684.                             array("name" => "product_total_price_usdollar", "value" => ($product_qty*$product_price)),
  1685.                             array("name" => "vat", "value" => '20.0'),
  1686.                             array("name" => "parent_name", "value" => 'AOS_Quotes'),
  1687.                             array("name" => "parent_type", "value" => 'AOS_Quotes'),
  1688.                             array("name" => "parent_id", "value" => $aos_quote_entry_id),
  1689.                             array("name" => "product_id", "value" => $aos_products_entry_id),
  1690.                             array("name" => "group_id", "value" => $aos_line_item_groups_entry_id),
  1691.                             array("name" => "product_qty", "value" => $product_qty),
  1692.                             array("name" => "number", "value" => ++$aaa),
  1693.                         ),
  1694.                     );
  1695.                     $set_entry_result_aos_products_quotes = call("set_entry", $set_entry_parameters, $url);
  1696.                 }
  1697.             }
  1698.  
  1699.  
  1700.  
  1701.  
  1702.  
  1703.  
  1704.  
  1705.  
  1706.  
  1707.  
  1708.  
  1709.             //////////////////////////////////////////////////////////////////////////////////
  1710.             // CREER LES SOUS CONTACTS
  1711.             foreach($post['contact_entry_id'] as $z=>$v) {
  1712.                 $user=woo_get_customer_by_billing_email($post['email'][$z]);
  1713.                 //////////////////////////////////////////////////////
  1714.                 //get AOS_Products -------------------------------------
  1715.                 ///
  1716.                 $get_entry_list_parameters = array(
  1717.                     'session' => $session_id,
  1718.                     'module_name' => 'AOS_Products',
  1719.                     'query' => "aos_products.id='".$post['aos_products_entry_id'][$z]."'",     //The SQL WHERE clause without the word "where".
  1720.                     'order_by' => "",                              //The SQL ORDER BY clause without the phrase "order by".
  1721.                     'offset' => '0',                               //The record offset from which to start.
  1722.                     'select_fields' => array(                      //Optional. A list of fields to include in the results.
  1723.                         //'id',
  1724.                         //'name',
  1725.                     ),
  1726.                     'link_name_to_fields_array' => array(),
  1727.                     'max_results' => '1',      //The maximum number of results to return.
  1728.                     'deleted' => '0',          //To exclude deleted records
  1729.                     'Favorites' => false,      //If only records marked as favorites should be returned.
  1730.                 );
  1731.                 $get_entry_list_result_aos_products = call('get_entry_list', $get_entry_list_parameters, $url);
  1732.                 $aos_products_entry_id = $get_entry_list_result_aos_products->entry_list[0]->id;
  1733.                 $product_title=$get_entry_list_result_aos_products->entry_list[0]->name_value_list->name->value;
  1734.                 $cost=$get_entry_list_result_aos_products->entry_list[0]->name_value_list->cost->value;
  1735.                 $product_sku=$get_entry_list_result_aos_products->entry_list[0]->name_value_list->part_number->value;
  1736.                 $product_price=$get_entry_list_result_aos_products->entry_list[0]->name_value_list->price->value;;
  1737.                 $product_qty=$post['nbr_h'][$z];
  1738.                 //echo '<pre>'.print_r($get_entry_list_result_aos_products->entry_list[0],true).'</pre>'; exit;
  1739.                 //////////////////////////////////////////////////////
  1740.                 //get AOS_Contracts -------------------------------------
  1741.                 $aos_contract_entry_id=get_user_meta($user->ID, 'aos_contract_entry_id_'.$z, true ).'';
  1742.                 $get_entry_list_parameters = array(
  1743.                     'session' => $session_id,
  1744.                     'module_name' => 'AOS_Contracts',
  1745.                     'query' => "aos_contracts.id='".$aos_contract_entry_id."'",     //The SQL WHERE clause without the word "where".
  1746.                     'order_by' => "",                              //The SQL ORDER BY clause without the phrase "order by".
  1747.                     'offset' => '0',                               //The record offset from which to start.
  1748.                     'select_fields' => array(                      //Optional. A list of fields to include in the results.
  1749.                         'id',
  1750.                         'name',
  1751.                     ),
  1752.                     'link_name_to_fields_array' => array(),
  1753.                     'max_results' => '1',      //The maximum number of results to return.
  1754.                     'deleted' => '0',          //To exclude deleted records
  1755.                     'Favorites' => false,      //If only records marked as favorites should be returned.
  1756.                 );
  1757.                 $get_entry_list_result_aos_contract = call('get_entry_list', $get_entry_list_parameters, $url); //echo "<pre>".print_r($get_entry_list_result_aos_contract,true)."</pre>";
  1758.                 $aos_contract_entry_id = $get_entry_list_result_aos_contract->entry_list[0]->id;
  1759. //echo "<pre>AOS_Contracts LOAD : ".print_r($aos_contract_entry_id,true)."</pre>";
  1760.                 //////////////////////////////////////////////////////
  1761.                 //create AOS_Contracts -------------------------------------
  1762.                 $d=explode('-',$post['date'][$z]);
  1763.                 $date=date('Y-m-d',mktime(0,0,0,$d[1],$d[2],$d[0]));
  1764.                 $date_m=date('Y-m-d',mktime(0,0,0,$d[1]+1,$d[2],$d[0]));
  1765.                 $tot_ht=$product_price*$product_qty;
  1766.                 $set_entry_parameters = array(
  1767.                     "session" => $session_id,
  1768.                     "module_name" => "AOS_Contracts",
  1769.                     "name_value_list" => array(
  1770.                         array("name" => "assigned_user_name", "value" => 'Administrator'),
  1771.                         array("name" => "assigned_user_id", "value" => '1'),
  1772.                         array("name" => "id", "value" => $aos_contract_entry_id), // TO UPDATE
  1773.                         array("name" => "name", "value" => "Contract ".$order->get_id().'-'.($z+1)." pour ".$post['prenom'][$z].' '.$post['nom'][$z]),
  1774.                         array("name" => "description", "value" => ""),
  1775.                         array("name" => "reference_code", "value" => $order->get_id().'-'.($z+1)),
  1776.                         array("name" => "start_date", "value" => $date),
  1777.                         array("name" => "end_date", "value" => $date_m),
  1778.                         array("name" => "total_contract_value", "value" => round($tot_ht*1.2,3)),
  1779.                         array("name" => "total_contract_value_usdollar", "value" => round($tot_ht*1.2,3)),
  1780.                         array("name" => "currency_id", "value" => "-99"),
  1781.                         array("name" => "status", "value" => "Not Started"),
  1782.                         //array("name" => "customer_signed_date", "value" => ""),
  1783.                         //array("name" => "company_signed_date", "value" => ""),
  1784.                         array("name" => "contract_account_id", "value" => $post['account_entry_id'][$z]),
  1785.                         array("name" => "contract_account", "value" => $post['account_entry_id'][$z]),
  1786.                         array("name" => "contact_id", "value" => $post['contact_entry_id'][$z]),
  1787.                         array("name" => "contact", "value" => $post['contact_entry_id'][$z]),
  1788.  
  1789.                         array("name" => "total_amt", "value" => $tot_ht),
  1790.                         array("name" => "total_amt_usdollar", "value" => $tot_ht),
  1791.                         array("name" => "subtotal_amount", "value" => $tot_ht),
  1792.                         array("name" => "subtotal_amount_usdollar", "value" => $tot_ht),
  1793.                         array("name" => "discount_amount", "value" => 0),
  1794.                         array("name" => "discount_amount_usdollar", "value" => 0),
  1795.                         array("name" => "tax_amount", "value" => ($tot_ht*0.2)),
  1796.                         array("name" => "tax_amount_usdollar", "value" => ($tot_ht*0.2)),
  1797.                         array("name" => "shipping_amount", "value" => 0),
  1798.                         array("name" => "shipping_amount_usdollar", "value" => 0),
  1799.                         array("name" => "shipping_tax", "value" => "20.0"),
  1800.                         array("name" => "shipping_tax_amt", "value" => 0),
  1801.                         array("name" => "shipping_tax_amt_usdollar", "value" => 0),
  1802.                         array("name" => "total_amount", "value" => round($tot_ht*1.2,3)),
  1803.                         array("name" => "total_amount_usdollar", "value" => round($tot_ht*1.2,3)),
  1804.                         array("name" => "formateur_c", "value" => ""),
  1805.                         array("name" => "sess_formateurs_id_c", "value" => ""),
  1806.                         array("name" => "lieu_c", "value" => 'StraFormation'), //Lieu StraFormation
  1807.                         array("name" => "fp_event_locations_id_c", "value" => '8ead5c4b-33a0-63f8-dcfe-5af7bdf9390f'), //Lieu StraFormation
  1808.                         array("name" => "dates_formation_text_c", "value" => ''),
  1809.                         array("name" => "woo_order_c", "value" => $order->get_id().'-'.($z+1)),
  1810.                     ),
  1811.                 );
  1812.                 $set_entry_result_aos_contract = call("set_entry", $set_entry_parameters, $url);
  1813.                 $aos_contract_entry_id = $set_entry_result_aos_contract->id;
  1814.                 update_user_meta($user->ID, 'aos_contract_entry_id_'.$z, $aos_contract_entry_id);
  1815. //echo "<pre>AOS_Contracts CREATE OR UPDATE : ".print_r($aos_contract_entry_id,true)."</pre>"; //echo "<pre style='font-size:9px;'>".print_r($set_entry_result_aos_contract,true)."</pre>";
  1816.                 //////////////////////////////////////////////////////
  1817.                 //get AOS_Line_Item_Groups -------------------------------------
  1818.                 $aos_line_item_groups_entry_id=get_user_meta($user->ID, 'aos_line_item_groups_entry_id_'.$z, true ).'';
  1819.                 $get_entry_list_parameters = array(
  1820.                     'session' => $session_id,
  1821.                     'module_name' => 'AOS_Line_Item_Groups',
  1822.                     'query' => "aos_line_item_groups.id='".$aos_line_item_groups_entry_id."'",     //The SQL WHERE clause without the word "where".
  1823.                     'order_by' => "",                              //The SQL ORDER BY clause without the phrase "order by".
  1824.                     'offset' => '0',                               //The record offset from which to start.
  1825.                     'select_fields' => array(                      //Optional. A list of fields to include in the results.
  1826.                         'id',
  1827.                         'name',
  1828.                     ),
  1829.                     'link_name_to_fields_array' => array(),
  1830.                     'max_results' => '1',      //The maximum number of results to return.
  1831.                     'deleted' => '0',          //To exclude deleted records
  1832.                     'Favorites' => false,      //If only records marked as favorites should be returned.
  1833.                 );
  1834.                 $get_entry_list_result_aos_line_item_groups = call('get_entry_list', $get_entry_list_parameters, $url);
  1835.                 $aos_line_item_groups_entry_id = $get_entry_list_result_aos_line_item_groups->entry_list[0]->id;
  1836. //echo "<pre>AOS_Line_Item_Groups LOAD : ".print_r($aos_line_item_groups_entry_id,true)."</pre>"; //echo "<pre style='font-size:9px;'>".print_r($get_entry_list_result_aos_line_item_groups,true)."</pre>";
  1837.                 //////////////////////////////////////////////////////
  1838.                 //create or update AOS_Line_Item_Groups -------------------------------------
  1839.                 $set_entry_parameters = array(
  1840.                     "session" => $session_id,
  1841.                     "module_name" => "AOS_Line_Item_Groups",
  1842.                     "name_value_list" => array(
  1843.                         array("name" => "id", "value" => $aos_line_item_groups_entry_id.''), // TO UPDATE
  1844.                         array("name" => "name", "value" => "Formations ".$order->get_id()),
  1845.                         array("name" => "description", "value" => ""),
  1846.                         array("name" => "assigned_user_name", "value" => 'Administrator'),
  1847.                         array("name" => "assigned_user_id", "value" => '1'),
  1848.                         array("name" => "total_amt", "value" => $tot_ht),
  1849.                         array("name" => "total_amt_usdollar", "value" => $tot_ht),
  1850.                         array("name" => "discount_amount", "value" => 0),
  1851.                         array("name" => "discount_amount_usdollar", "value" => 0),
  1852.                         array("name" => "subtotal_amount", "value" => $tot_ht),
  1853.                         array("name" => "subtotal_amount_usdollar", "value" => $tot_ht),
  1854.                         array("name" => "tax_amount", "value" => ($tot_ht*0.2)),
  1855.                         array("name" => "tax_amount_usdollar", "value" => ($tot_ht*0.2)),
  1856.                         array("name" => "subtotal_tax_amount", "value" => 0),
  1857.                         array("name" => "subtotal_tax_amount_usdollar", "value" => 0),
  1858.                         array("name" => "total_amount", "value" => ($tot_ht+($tot_ht*0.2))),
  1859.                         array("name" => "total_amount_usdollar", "value" => ($tot_ht+($tot_ht*0.2))),
  1860.                         array("name" => "parent_name", "value" => 'AOS_Contracts'),
  1861.                         array("name" => "parent_type", "value" => 'AOS_Contracts'),
  1862.                         array("name" => "parent_id", "value" => $aos_contract_entry_id),
  1863.                         array("name" => "currency_id", "value" => "-99"),
  1864.                         array("name" => "number", "value" => 1),
  1865.                     ),
  1866.                 );
  1867.                 $set_entry_result_aos_line_item_groups = call("set_entry", $set_entry_parameters, $url);
  1868.                 $aos_line_item_groups_entry_id = $set_entry_result_aos_line_item_groups->id;
  1869.                 update_user_meta($user->ID, 'aos_line_item_groups_entry_id_'.$z, $aos_line_item_groups_entry_id);
  1870. //echo "<pre>AOS_Line_Item_Groups CREATE OR UPDATE : ".print_r($aos_line_item_groups_entry_id,true)."</pre>"; //echo "<pre style='font-size:9px;'>".print_r($set_entry_result_aos_line_item_groups,true)."</pre>";
  1871.                 //////////////////////////////////////////////////////
  1872.                 //get AOS_Products_Quotes -------------------------------------
  1873.                 $aos_products_quotes_entry_id=get_user_meta($user->ID, 'aos_products_quotes_entry_id_'.$z, true).'';
  1874.                 $get_entry_list_parameters = array(
  1875.                     'session' => $session_id,
  1876.                     'module_name' => 'AOS_Products_Quotes',
  1877.                     'query' => "aos_products_quotes.id='".$aos_products_quotes_entry_id."'",     //The SQL WHERE clause without the word "where".
  1878.                     'order_by' => "",                              //The SQL ORDER BY clause without the phrase "order by".
  1879.                     'offset' => '0',                               //The record offset from which to start.
  1880.                     'select_fields' => array(                      //Optional. A list of fields to include in the results.
  1881.                         'id',
  1882.                         'name',
  1883.                     ),
  1884.                     'link_name_to_fields_array' => array(),
  1885.                     'max_results' => '1',      //The maximum number of results to return.
  1886.                     'deleted' => '0',          //To exclude deleted records
  1887.                     'Favorites' => false,      //If only records marked as favorites should be returned.
  1888.                 );
  1889.                 $get_entry_list_result_aos_products_quotes = call('get_entry_list', $get_entry_list_parameters, $url);
  1890.                 $aos_products_quotes_entry_id = $get_entry_list_result_aos_products_quotes->entry_list[0]->id.'';
  1891. //echo "<pre>AOS_Products_Quotes LOAD : ".print_r($aos_products_quotes_entry_id,true)."</pre>"; //echo "<pre style='font-size:9px;'>".print_r($get_entry_list_result_aos_products_quotes,true)."</pre>";
  1892.                 //////////////////////////////////////////////////////
  1893.                 //create or update AOS_Products_Quotes -------------------------------------
  1894.                 $set_entry_parameters = array(
  1895.                     "session" => $session_id,
  1896.                     "module_name" => "AOS_Products_Quotes",
  1897.                     "name_value_list" => array(
  1898.                         array("name" => "assigned_user_name", "value" => 'Administrator'),
  1899.                         array("name" => "assigned_user_id", "value" => '1'),
  1900.                         array("name" => "id", "value" => $aos_products_quotes_entry_id.''), // TO UPDATE
  1901.                         array("name" => "currency_id", "value" => "-99"),
  1902.                         array("name" => "name", "value" => $product_title),
  1903.                         array("name" => "product_cost_price", "value" => $cost),
  1904.                         array("name" => "product_cost_price_usdollar", "value" => $cost),
  1905.                         array("name" => "description", "value" => ''),
  1906.                         array("name" => "item_description", "value" => $product_title),
  1907.                         array("name" => "maincode", "value" => "XXXX"),
  1908.                         array("name" => "part_number", "value" => $product_sku),
  1909.                         array("name" => "currency_id", "value" => "-99"),
  1910.                         array("name" => "product_list_price", "value" => $product_price),
  1911.                         array("name" => "product_list_price_usdollar", "value" => $product_price),
  1912.                         array("name" => "product_discount", "value" => 0),
  1913.                         array("name" => "product_discount_usdollar", "value" => 0),
  1914.                         array("name" => "product_discount_amount", "value" => 0),
  1915.                         array("name" => "product_discount_amount_usdollar", "value" => 0),
  1916.                         array("name" => "discount", "value" => "Percentage"),
  1917.                         array("name" => "product_unit_price", "value" => $product_price),
  1918.                         array("name" => "product_unit_price_usdollar", "value" => $product_price),
  1919.                         array("name" => "vat_amt", "value" => ($product_price*0.2)),
  1920.                         array("name" => "vat_amt_usdollar", "value" => ($product_price*0.2)),
  1921.                         array("name" => "product_total_price", "value" => ($product_qty*$product_price)),
  1922.                         array("name" => "product_total_price_usdollar", "value" => ($product_qty*$product_price)),
  1923.                         array("name" => "vat", "value" => '20.0'),
  1924.                         array("name" => "parent_name", "value" => 'AOS_Contracts'),
  1925.                         array("name" => "parent_type", "value" => 'AOS_Contracts'),
  1926.                         array("name" => "parent_id", "value" => $aos_contract_entry_id),
  1927.                         array("name" => "product_id", "value" => $post['aos_products_entry_id'][$z]),
  1928.                         array("name" => "group_id", "value" => $aos_line_item_groups_entry_id),
  1929.                         array("name" => "product_qty", "value" => $product_qty),
  1930.                         array("name" => "number", "value" => ++$aaa),
  1931.                     ),
  1932.                 );
  1933.                 $set_entry_result_aos_products_quotes = call("set_entry", $set_entry_parameters, $url);
  1934.                 $aos_products_quotes_entry_id = $set_entry_result_aos_products_quotes->id;
  1935.                 update_user_meta($user->ID, 'aos_products_quotes_entry_id_'.$z, $aos_products_quotes_entry_id);
  1936. //echo "<pre>AOS_Products_Quotes CREATE OR UPDATE : ".print_r($aos_products_quotes_entry_id,true)."</pre>"; echo "<pre style='font-size:9px;'>".print_r($set_entry_result_aos_products_quotes,true)."</pre>";
  1937.             }// FIN : CREER LES SOUS DEVIS DES CONTACTS
  1938.         }
  1939.  
  1940.  
  1941.  
  1942.     }
  1943. }
  1944.  
  1945.  
  1946.  
  1947.  
  1948.  
  1949.  
  1950.  
  1951.  
  1952.  
  1953. //se \FPDF;
  1954. use setasign\Fpdi;
  1955. //require_once('/var/www/clients/client0/web1/web/FPDF/fpdf.php');
  1956. //require_once('/var/www/clients/client0/web1/web/FPDF/font/helvetica.php');
  1957. //require_once('/var/www/clients/client0/web1/web/FPDI/autoload.php');
  1958. /////////////////////////////////////////////////////////////////////////////////////
  1959. // Apres la signature électronique : créer le pdf signé
  1960. function action_wpcf7_after_flamingo( $result ) {
  1961.     $user_nom=wp_get_current_user();
  1962.     $args = array(
  1963.         'numberposts' => 10,
  1964.         'offset' => 0,
  1965.         'category' => 0,
  1966.         'orderby' => 'post_date',
  1967.         'order' => 'DESC',
  1968.         'include' => '',
  1969.         'exclude' => '',
  1970.         'meta_key' => '',
  1971.         'meta_value' =>'',
  1972.         'post_type' => 'flamingo_inbound',
  1973.         'post_status' => 'publish',
  1974.         'suppress_filters' => true
  1975.     );
  1976.     $recent_emails = wp_get_recent_posts( $args, ARRAY_A );
  1977.     foreach($recent_emails as $k=>$v) {
  1978.         //////////////////////////////////////////////////////
  1979.         // SIGNER DEVIS COMPTE
  1980.         if($v['post_title']=='Signature devis' && $user_nom->roles[0]=='customer') {
  1981.             $recent_emails[$k]['meta']=get_post_meta($v['ID']);
  1982.             if(strtolower($recent_emails[$k]['meta']['_field_bon-pour-accord'][0])=='bon pour accord') {
  1983.                 $recent_emails[$k]['img']=str_replace('http://straformation.test/','/var/www/clients/client0/web1/web/',$recent_emails[$k]['meta']['_field_signature-233'][0]);
  1984.                 $recent_emails[$k]['email']=$recent_emails[$k]['meta']['_field_your-email'][0];
  1985.                 $recent_emails[$k]['name']=$recent_emails[$k]['meta']['_field_your-name'][0];
  1986.                 $tmp=explode('http://straformation.test/mon-compte/view-order/',$recent_emails[$k]['meta']['_meta'][0]); $tmp2=explode('/',$tmp[1]);
  1987.                 $recent_emails[$k]['order_id']=$tmp2[0];
  1988.                 $aos_quote_entry_id=get_post_meta($recent_emails[$k]['order_id'], 'aos_quote_entry_id',true);
  1989.                 ///
  1990.                 if(is_file('/var/www/clients/client0/web1/web/docs/devis_'.$recent_emails[$k]['order_id'].'_'.$aos_quote_entry_id.'_signed.pdf')) {
  1991.                     wp_delete_post($v['ID'], true);
  1992.                     continue;
  1993.                 }
  1994.                 $pdf = new Fpdi\Fpdi();
  1995.                 $h=get_height_from_png("/var/www/clients/client0/web1/web/docs/devis_".$recent_emails[$k]['order_id']."_".$aos_quote_entry_id.".pdf");
  1996.                 $h=round($h/842*297);
  1997.                 $pageCount=$pdf->setSourceFile("/var/www/clients/client0/web1/web/docs/devis_".$recent_emails[$k]['order_id']."_".$aos_quote_entry_id.".pdf");
  1998.                 for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
  1999.                     $templateId = $pdf->importPage($pageNo);
  2000.                     $pdf->AddPage();
  2001.                     $pdf->useTemplate($templateId, ['adjustPageSize' => true]);
  2002.                     $size=$pdf->getTemplateSize ($templateId);
  2003.                 }
  2004.                 $pdf->Image($recent_emails[$k]['img'],$size['width']-80,$h,50,50);
  2005.                 $pdf->SetFont('Helvetica');
  2006.                 $pdf->SetXY($size['width']-80,$h-15);
  2007.                 $pdf->Write(8, 'BON POUR ACCORD');
  2008.                 $pdf->SetXY($size['width']-80,$h-10);
  2009.                 $pdf->Write(8, utf8_decode($recent_emails[$k]['name']));
  2010.                 $pdf->SetXY($size['width']-80,$h-5);
  2011.                 $pdf->Write(8, 'le '.date('d/m/Y').' a '.date('H:i:s'));
  2012.                 $pdf->Output('/var/www/clients/client0/web1/web/docs/devis_'.$recent_emails[$k]['order_id'].'_'.$aos_quote_entry_id.'_signed.pdf','F');
  2013.                 change_order_statuts_to_devis_signe($recent_emails[$k]['order_id']);
  2014.                 send_email('serbastien.ferrand@gmail.com',
  2015.                     'Le devis '.$recent_emails[$k]['order_id'].' à été signé',
  2016.                     'Le devis '.$recent_emails[$k]['order_id'].' à été signé et est en pièce jointe<br/>
  2017.                 <br/>
  2018.                 Retrouver le devis signé sur le CRM StraFormation <a href="http://straformation.test/SuiteCRM/index.php?action=DetailView&module=AOS_Quotes&record='.$aos_quote_entry_id.'&offset=1&time='.time().'" target="_blank">ici</a>.',
  2019.                     '/var/www/clients/client0/web1/web/docs/devis_'.$recent_emails[$k]['order_id'].'_'.$aos_quote_entry_id.'_signed.pdf',
  2020.                     'Devis-signe-'.$recent_emails[$k]['order_id'].'-chez-StraFormation.pdf'
  2021.                 );
  2022.                 ///
  2023.                 $url = "http://suitecrm.straformation.fr/service/v4_1/rest.php";
  2024.                 $username = "admin";
  2025.                 $password = "waRR10RP0wa";
  2026.                 function call($method, $parameters, $url) {
  2027.                     ob_start();
  2028.                     $curl_request = curl_init();
  2029.                     curl_setopt($curl_request, CURLOPT_URL, $url);
  2030.                     curl_setopt($curl_request, CURLOPT_POST, 1);
  2031.                     curl_setopt($curl_request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
  2032.                     curl_setopt($curl_request, CURLOPT_HEADER, 1);
  2033.                     curl_setopt($curl_request, CURLOPT_SSL_VERIFYPEER, 0);
  2034.                     curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1);
  2035.                     curl_setopt($curl_request, CURLOPT_FOLLOWLOCATION, 0);
  2036.                     $jsonEncodedData = json_encode($parameters);
  2037.                     $post = array(
  2038.                         "method" => $method,
  2039.                         "input_type" => "JSON",
  2040.                         "response_type" => "JSON",
  2041.                         "rest_data" => $jsonEncodedData
  2042.                     );
  2043.                     curl_setopt($curl_request, CURLOPT_POSTFIELDS, $post);
  2044.                     $result = curl_exec($curl_request);
  2045.                     curl_close($curl_request);
  2046.                     $result = explode("\r\n\r\n", $result, 2);
  2047.                     $response = json_decode($result[1]);
  2048.                     ob_end_flush();
  2049.                     return $response;
  2050.                 }
  2051.                 //////////////////////////////////////////////////////
  2052.                 //login ---------------------------------------------
  2053.                 $login_parameters = array(
  2054.                     "user_auth" => array(
  2055.                         "user_name" => $username,
  2056.                         "password" => md5($password),
  2057.                         "version" => "1"
  2058.                     ),
  2059.                     "application_name" => "Woocommerce",
  2060.                     "name_value_list" => array(),
  2061.                 );
  2062.                 $login_result = call("login", $login_parameters, $url); $session_id = $login_result->id; //echo "<pre>".print_r($login_result,true)."</pre>";
  2063.                 //////////////////////////////////////////////////////
  2064.                 // update aos_quotes -------------------------------------
  2065.                 $set_entry_parameters = array(
  2066.                     "session" => $session_id,
  2067.                     "module_name" => "AOS_Quotes",
  2068.                     "name_value_list" => array(
  2069.                         array("name" => "id", "value" => $aos_quote_entry_id), // TO UPDATE
  2070.                         array("name" => "approval_issue", "value" => "Devis signé"),
  2071.                         array("name" => "approval_status", "value" => "Approved"),
  2072.                         array("name" => "stage", "value" => "Confirmed"),
  2073.                     ),
  2074.                 );
  2075.                 $set_entry_result_aos_quote = call("set_entry", $set_entry_parameters, $url);
  2076.             }
  2077.             wp_delete_post($v['ID'], true);
  2078.         }
  2079.         //////////////////////////////////////////////////////
  2080.         // SIGNER CONTRACT FORMATEUR
  2081.         if($v['post_title']=='Signature contrat sous traitance' && $user_nom->roles[0]=='formateur') {
  2082.             $recent_emails[$k]['meta']=get_post_meta($v['ID']);
  2083.             if(strtolower($recent_emails[$k]['meta']['_field_lu-et-approuve'][0])=='lu et approuvé') {
  2084.                 $recent_emails[$k]['img']=str_replace('http://straformation.test/','/var/www/clients/client0/web1/web/',$recent_emails[$k]['meta']['_field_signature-234'][0]);
  2085.                 $recent_emails[$k]['email']=$recent_emails[$k]['meta']['_field_your-email'][0];
  2086.                 $recent_emails[$k]['name']=$recent_emails[$k]['meta']['_field_your-name'][0];
  2087.                 $recent_emails[$k]['order_id']=$recent_emails[$k]['meta']['_field_order_id'][0];
  2088.                 $recent_emails[$k]['formateur_id']=$recent_emails[$k]['meta']['_field_formateur_id'][0];
  2089.                 $recent_emails[$k]['contract_id']=$recent_emails[$k]['meta']['_field_contract_id'][0];
  2090.                 ///
  2091.                 if(is_file('/var/www/clients/client0/web1/web/docs/contract_'.$recent_emails[$k]['order_id'].'_'.$recent_emails[$k]['formateur_id'].'_'.$recent_emails[$k]['contract_id'].'_signed.pdf')) {
  2092.                     wp_delete_post($v['ID'], true);
  2093.                     continue;
  2094.                 }
  2095.                 $pdf = new Fpdi\Fpdi();
  2096.                 $h=get_height_from_png("/var/www/clients/client0/web1/web/docs/contract_".$recent_emails[$k]['order_id']."_".$recent_emails[$k]['formateur_id']."_".$recent_emails[$k]['contract_id'].".pdf");
  2097.                 $h=round($h/842*297);
  2098.                 $pageCount=$pdf->setSourceFile("/var/www/clients/client0/web1/web/docs/contract_".$recent_emails[$k]['order_id']."_".$recent_emails[$k]['formateur_id']."_".$recent_emails[$k]['contract_id'].".pdf");
  2099.                 for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
  2100.                     $templateId = $pdf->importPage($pageNo);
  2101.                     $pdf->AddPage();
  2102.                     $pdf->useTemplate($templateId, ['adjustPageSize' => true]);
  2103.                     $size=$pdf->getTemplateSize ($templateId);
  2104.                 }
  2105.                 $pdf->Image($recent_emails[$k]['img'],$size['width']-80,$h,50,50);
  2106.                 $pdf->SetFont('Helvetica');
  2107.                 $pdf->SetXY($size['width']-80,$h-15);
  2108.                 $pdf->Write(8, utf8_decode('Lu et approuvé'));
  2109.                 $pdf->SetXY($size['width']-80,$h-10);
  2110.                 $pdf->Write(8, utf8_decode($recent_emails[$k]['name']));
  2111.                 $pdf->SetXY($size['width']-80,$h-5);
  2112.                 $pdf->Write(8, 'le '.date('d/m/Y').' a '.date('H:i:s'));
  2113.                 $pdf->Output('/var/www/clients/client0/web1/web/docs/contract_'.$recent_emails[$k]['order_id'].'_'.$recent_emails[$k]['formateur_id'].'_'.$recent_emails[$k]['contract_id'].'_signed.pdf','F');
  2114.                 send_email('serbastien.ferrand@gmail.com',
  2115.                     'Le contract de sous traitance '.$recent_emails[$k]['order_id'].' est signé',
  2116.                     'Le contract de sous traitance '.$recent_emails[$k]['order_id'].' est signé et est en pièce jointe.<br/>
  2117.                 <br/>
  2118.                 Retrouver le contract de sous traitance signé sur le CRM StraFormation <a href="http://straformation.test/SuiteCRM/index.php?module=AOS_Contracts&action=DetailView&record='.$recent_emails[$k]['contract_id'].'&offset=1&time='.time().'" target="_blank">ici</a>.',
  2119.                     '/var/www/clients/client0/web1/web/docs/contract_'.$recent_emails[$k]['order_id'].'_'.$recent_emails[$k]['formateur_id'].'_'.$recent_emails[$k]['contract_id'].'_signed.pdf',
  2120.                     'contract-signe-'.$recent_emails[$k]['order_id'].'-'.$recent_emails[$k]['formateur_id'].'-'.$recent_emails[$k]['contract_id'].'-chez-StraFormation.pdf'
  2121.                 );
  2122.                 $date=date('Y-m-d',mktime(0,0,0,date('m'),date('d'),date('Y')));
  2123.                 //////////////////////////////////////////////////////
  2124.                 $url = "http://suitecrm.straformation.fr/service/v4_1/rest.php";
  2125.                 $username = "admin";
  2126.                 $password = "waRR10RP0wa";
  2127.                 function call($method, $parameters, $url) {
  2128.                     ob_start();
  2129.                     $curl_request = curl_init();
  2130.                     curl_setopt($curl_request, CURLOPT_URL, $url);
  2131.                     curl_setopt($curl_request, CURLOPT_POST, 1);
  2132.                     curl_setopt($curl_request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
  2133.                     curl_setopt($curl_request, CURLOPT_HEADER, 1);
  2134.                     curl_setopt($curl_request, CURLOPT_SSL_VERIFYPEER, 0);
  2135.                     curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1);
  2136.                     curl_setopt($curl_request, CURLOPT_FOLLOWLOCATION, 0);
  2137.                     $jsonEncodedData = json_encode($parameters);
  2138.                     $post = array(
  2139.                         "method" => $method,
  2140.                         "input_type" => "JSON",
  2141.                         "response_type" => "JSON",
  2142.                         "rest_data" => $jsonEncodedData
  2143.                     );
  2144.                     curl_setopt($curl_request, CURLOPT_POSTFIELDS, $post);
  2145.                     $result = curl_exec($curl_request);
  2146.                     curl_close($curl_request);
  2147.                     $result = explode("\r\n\r\n", $result, 2);
  2148.                     $response = json_decode($result[1]);
  2149.                     ob_end_flush();
  2150.                     return $response;
  2151.                 }
  2152.                 //////////////////////////////////////////////////////
  2153.                 //login ---------------------------------------------
  2154.                 $login_parameters = array(
  2155.                     "user_auth" => array(
  2156.                         "user_name" => $username,
  2157.                         "password" => md5($password),
  2158.                         "version" => "1"
  2159.                     ),
  2160.                     "application_name" => "Woocommerce",
  2161.                     "name_value_list" => array(),
  2162.                 );
  2163.                 $login_result = call("login", $login_parameters, $url); $session_id = $login_result->id; //echo "<pre>".print_r($login_result,true)."</pre>";
  2164.                 //////////////////////////////////////////////////////
  2165.                 // update aos_quotes -------------------------------------
  2166.                 $set_entry_parameters = array(
  2167.                     "session" => $session_id,
  2168.                     "module_name" => "AOS_Contracts",
  2169.                     "name_value_list" => array(
  2170.                         array("name" => "id", "value" => $recent_emails[$k]['contract_id']), // TO UPDATE
  2171.                         array("name" => "status", "value" => "Signed"),
  2172.                         array("name" => "customer_signed_date", "value" => $date),
  2173.                     ),
  2174.                 );
  2175.                 $set_entry_result_aos_quote = call("set_entry", $set_entry_parameters, $url);
  2176.                 //////////////////////////////////////////////////////
  2177.             }
  2178.             wp_delete_post($v['ID'], true);
  2179.         }
  2180.         //////////////////////////////////////////////////////
  2181.         // SIGNER CONTRACT DE FORMATION
  2182.         if($v['post_title']=='Signature contrat formation' && ($user_nom->roles[0]=='customer' || $user_nom->roles[0]=='stagiaire')) {
  2183.             $recent_emails[$k]['meta']=get_post_meta($v['ID']);
  2184.             if(strtolower($recent_emails[$k]['meta']['_field_lu-et-approuve'][0])=='lu et approuvé') {
  2185.                 $recent_emails[$k]['img']=str_replace('http://straformation.test/','/var/www/clients/client0/web1/web/',$recent_emails[$k]['meta']['_field_signature-235'][0]);
  2186.                 $recent_emails[$k]['email']=$recent_emails[$k]['meta']['_field_your-email'][0];
  2187.                 $recent_emails[$k]['name']=$recent_emails[$k]['meta']['_field_your-name'][0];
  2188.                 $recent_emails[$k]['order_id']=$recent_emails[$k]['meta']['_field_order_id'][0];
  2189.                 $recent_emails[$k]['contact_id']=$recent_emails[$k]['meta']['_field_contact_id'][0];
  2190.                 $recent_emails[$k]['contract_id']=$recent_emails[$k]['meta']['_field_contract_id'][0];
  2191.                 ///
  2192.                 if($user_nom->roles[0]=='stagiaire') {
  2193.                     if(is_file('/var/www/clients/client0/web1/web/docs/contract_'.$recent_emails[$k]['order_id'].'_'.$recent_emails[$k]['contact_id'].'_'.$recent_emails[$k]['contract_id'].'_signed.pdf') || is_file('/var/www/clients/client0/web1/web/docs/contract_'.$recent_emails[$k]['order_id'].'_'.$recent_emails[$k]['contact_id'].'_'.$recent_emails[$k]['contract_id'].'_ok.pdf')) {
  2194.                         wp_delete_post($v['ID'], true);
  2195.                         continue;
  2196.                     }
  2197.                     $pdf = new Fpdi\Fpdi();
  2198.                     $h=get_height_from_png("/var/www/clients/client0/web1/web/docs/contract_".$recent_emails[$k]['order_id']."_".$recent_emails[$k]['contact_id']."_".$recent_emails[$k]['contract_id'].".pdf");
  2199.                     $h=round($h/842*297);
  2200.                     $pageCount=$pdf->setSourceFile("/var/www/clients/client0/web1/web/docs/contract_".$recent_emails[$k]['order_id']."_".$recent_emails[$k]['contact_id']."_".$recent_emails[$k]['contract_id'].".pdf");
  2201.                     for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
  2202.                         $templateId = $pdf->importPage($pageNo);
  2203.                         $pdf->AddPage();
  2204.                         $pdf->useTemplate($templateId, ['adjustPageSize' => true]);
  2205.                         $size=$pdf->getTemplateSize ($templateId);
  2206.                     }
  2207.                     $pdf->Image($recent_emails[$k]['img'],$size['width']-60,$h-60,50,50);
  2208.                     $pdf->SetFont('Helvetica');
  2209.                     $pdf->SetXY($size['width']-60,$h-20);
  2210.                     $pdf->Write(8, utf8_decode('Le stagiaire'));
  2211.                     $pdf->SetXY($size['width']-60,$h-15);
  2212.                     $pdf->Write(8, utf8_decode('Lu et approuvé'));
  2213.                     $pdf->SetXY($size['width']-60,$h-10);
  2214.                     $pdf->Write(8, utf8_decode($recent_emails[$k]['name']));
  2215.                     $pdf->SetXY($size['width']-60,$h-5);
  2216.                     $pdf->Write(8, 'le '.date('d/m/Y').' a '.date('H:i:s'));
  2217.                     $pdf->Output('/var/www/clients/client0/web1/web/docs/contract_'.$recent_emails[$k]['order_id'].'_'.$recent_emails[$k]['contact_id'].'_'.$recent_emails[$k]['contract_id'].'_ok.pdf','F');
  2218.                     send_email('serbastien.ferrand@gmail.com',
  2219.                         'Le contract de formation '.$recent_emails[$k]['order_id'].' est signé par le stagiaire.',
  2220.                         'Le contract de formation '.$recent_emails[$k]['order_id'].' est signé par le stagiaire et est en pièce jointe.<br/>
  2221.                     <br/>
  2222.                     Retrouver le contract de formation signé sur le CRM StraFormation <a href="http://straformation.test/SuiteCRM/index.php?module=AOS_Contracts&action=DetailView&record='.$recent_emails[$k]['contract_id'].'&offset=1&time='.time().'" target="_blank">ici</a>.',
  2223.                         '/var/www/clients/client0/web1/web/docs/contract_'.$recent_emails[$k]['order_id'].'_'.$recent_emails[$k]['contact_id'].'_'.$recent_emails[$k]['contract_id'].'_ok.pdf',
  2224.                         'contract-signe-'.$recent_emails[$k]['order_id'].'-'.$recent_emails[$k]['contact_id'].'-'.$recent_emails[$k]['contract_id'].'-chez-StraFormation.pdf'
  2225.                     );
  2226.                     //TODO : prevenir le client ! si tous ont signé !
  2227.                 }
  2228.                 if($user_nom->roles[0]=='customer') {
  2229.                     if(is_file('/var/www/clients/client0/web1/web/docs/contract_'.$recent_emails[$k]['order_id'].'_'.$recent_emails[$k]['contact_id'].'_'.$recent_emails[$k]['contract_id'].'_signed.pdf')) {
  2230.                         wp_delete_post($v['ID'], true);
  2231.                         continue;
  2232.                     }
  2233.                     $pdf = new Fpdi\Fpdi();
  2234.                     $h=get_height_from_png("/var/www/clients/client0/web1/web/docs/contract_".$recent_emails[$k]['order_id']."_".$recent_emails[$k]['contact_id']."_".$recent_emails[$k]['contract_id']."_ok.pdf");
  2235.                     $h=round($h/842*297);
  2236.                     $pageCount=$pdf->setSourceFile("/var/www/clients/client0/web1/web/docs/contract_".$recent_emails[$k]['order_id']."_".$recent_emails[$k]['contact_id']."_".$recent_emails[$k]['contract_id']."_ok.pdf");
  2237.                     for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
  2238.                         $templateId = $pdf->importPage($pageNo);
  2239.                         $pdf->AddPage();
  2240.                         $pdf->useTemplate($templateId, ['adjustPageSize' => true]);
  2241.                         $size=$pdf->getTemplateSize ($templateId);
  2242.                     }
  2243.                     $pdf->Image($recent_emails[$k]['img'],$size['width']-140,$h,50,50);
  2244.                     $pdf->SetFont('Helvetica');
  2245.                     $pdf->SetXY($size['width']-140,$h-20);
  2246.                     $pdf->Write(8, utf8_decode('L\'entreprise / l\'organisme financeur'));
  2247.                     $pdf->SetXY($size['width']-140,$h-15);
  2248.                     $pdf->Write(8, utf8_decode('Lu et approuvé'));
  2249.                     $pdf->SetXY($size['width']-140,$h-10);
  2250.                     $pdf->Write(8, utf8_decode($recent_emails[$k]['name']));
  2251.                     $pdf->SetXY($size['width']-140,$h-5);
  2252.                     $pdf->Write(8, 'le '.date('d/m/Y').' a '.date('H:i:s'));
  2253.                     $pdf->Output('/var/www/clients/client0/web1/web/docs/contract_'.$recent_emails[$k]['order_id'].'_'.$recent_emails[$k]['contact_id'].'_'.$recent_emails[$k]['contract_id'].'_signed.pdf','F');
  2254.                     send_email('serbastien.ferrand@gmail.com',
  2255.                         'Le contract de formation '.$recent_emails[$k]['order_id'].' est signé par le stagiaire et le client',
  2256.                         'Le contract de formation '.$recent_emails[$k]['order_id'].' est signé par le stagiaire et le client et est en pièce jointe.<br/>
  2257.                     <br/>
  2258.                     retrouver le contract de formation signé sur le CRM StraFormation <a href="http://straformation.test/SuiteCRM/index.php?module=AOS_Contracts&action=DetailView&record='.$recent_emails[$k]['contract_id'].'&offset=1&time='.time().'" target="_blank">ici</a>.',
  2259.                         '/var/www/clients/client0/web1/web/docs/contract_'.$recent_emails[$k]['order_id'].'_'.$recent_emails[$k]['contact_id'].'_'.$recent_emails[$k]['contract_id'].'_signed.pdf',
  2260.                         'contract-signe-'.$recent_emails[$k]['order_id'].'-'.$recent_emails[$k]['contact_id'].'-'.$recent_emails[$k]['contract_id'].'-chez-StraFormation.pdf'
  2261.                     );
  2262.                     $date=date('Y-m-d',mktime(0,0,0,date('m'),date('d'),date('Y')));
  2263.                     //////////////////////////////////////////////////////
  2264.                     $url = "http://suitecrm.straformation.fr/service/v4_1/rest.php";
  2265.                     $username = "admin";
  2266.                     $password = "waRR10RP0wa";
  2267.                     if(!function_exists('call')) { function call($method, $parameters, $url) {
  2268.                         ob_start();
  2269.                         $curl_request = curl_init();
  2270.                         curl_setopt($curl_request, CURLOPT_URL, $url);
  2271.                         curl_setopt($curl_request, CURLOPT_POST, 1);
  2272.                         curl_setopt($curl_request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
  2273.                         curl_setopt($curl_request, CURLOPT_HEADER, 1);
  2274.                         curl_setopt($curl_request, CURLOPT_SSL_VERIFYPEER, 0);
  2275.                         curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1);
  2276.                         curl_setopt($curl_request, CURLOPT_FOLLOWLOCATION, 0);
  2277.                         $jsonEncodedData = json_encode($parameters);
  2278.                         $post = array(
  2279.                             "method" => $method,
  2280.                             "input_type" => "JSON",
  2281.                             "response_type" => "JSON",
  2282.                             "rest_data" => $jsonEncodedData
  2283.                         );
  2284.                         curl_setopt($curl_request, CURLOPT_POSTFIELDS, $post);
  2285.                         $result = curl_exec($curl_request);
  2286.                         curl_close($curl_request);
  2287.                         $result = explode("\r\n\r\n", $result, 2);
  2288.                         $response = json_decode($result[1]);
  2289.                         ob_end_flush();
  2290.                         return $response;
  2291.                     } }
  2292.                     //////////////////////////////////////////////////////
  2293.                     //login ---------------------------------------------
  2294.                     $login_parameters = array(
  2295.                         "user_auth" => array(
  2296.                             "user_name" => $username,
  2297.                             "password" => md5($password),
  2298.                             "version" => "1"
  2299.                         ),
  2300.                         "application_name" => "Woocommerce",
  2301.                         "name_value_list" => array(),
  2302.                     );
  2303.                     $login_result = call("login", $login_parameters, $url); $session_id = $login_result->id; //echo "<pre>".print_r($login_result,true)."</pre>";
  2304.                     //////////////////////////////////////////////////////
  2305.                     // update aos_quotes -------------------------------------
  2306.                     $set_entry_parameters = array(
  2307.                         "session" => $session_id,
  2308.                         "module_name" => "AOS_Contracts",
  2309.                         "name_value_list" => array(
  2310.                             array("name" => "id", "value" => $recent_emails[$k]['contract_id']), // TO UPDATE
  2311.                             array("name" => "status", "value" => "Signed"),
  2312.                             array("name" => "customer_signed_date", "value" => $date),
  2313.                         ),
  2314.                     );
  2315.                     $set_entry_result_aos_quote = call("set_entry", $set_entry_parameters, $url);
  2316.                     //////////////////////////////////////////////////////
  2317.                 }
  2318.             }
  2319.             wp_delete_post($v['ID'], true);
  2320.         }
  2321.         //////////////////////////////////////////////////////
  2322.         // SIGNER ATTESTATION DEBUT DE FORMATION
  2323.         if($v['post_title']=='Signer attestation entree en formation' && $user_nom->roles[0]=='stagiaire') {
  2324.             $recent_emails[$k]['meta']=get_post_meta($v['ID']);
  2325.             $recent_emails[$k]['img']=str_replace('http://straformation.test/','/var/www/clients/client0/web1/web/',$recent_emails[$k]['meta']['_field_signature-240'][0]);
  2326.             $recent_emails[$k]['email']=$recent_emails[$k]['meta']['_field_your-email'][0];
  2327.             $recent_emails[$k]['name']=$recent_emails[$k]['meta']['_field_your-name'][0];
  2328.             $recent_emails[$k]['order_id']=$recent_emails[$k]['meta']['_field_order_id'][0];
  2329.             $recent_emails[$k]['contact_id']=$recent_emails[$k]['meta']['_field_contact_id'][0];
  2330.             $recent_emails[$k]['contract_id']=$recent_emails[$k]['meta']['_field_contract_id'][0];
  2331.             ///
  2332.             if(is_file('/var/www/clients/client0/web1/web/docs/attestation_entree_'.$recent_emails[$k]['order_id'].'_'.$recent_emails[$k]['contact_id'].'_'.$recent_emails[$k]['contract_id'].'_signed.pdf')) {
  2333.                 wp_delete_post($v['ID'], true);
  2334.                 continue;
  2335.             }
  2336.             $pdf = new Fpdi\Fpdi();
  2337.             $h=get_height_from_png("/var/www/clients/client0/web1/web/docs/attestation_entree_".$recent_emails[$k]['order_id']."_".$recent_emails[$k]['contact_id']."_".$recent_emails[$k]['contract_id'].".pdf");
  2338.             $h=round($h/842*297);
  2339.             $pageCount=$pdf->setSourceFile("/var/www/clients/client0/web1/web/docs/attestation_entree_".$recent_emails[$k]['order_id']."_".$recent_emails[$k]['contact_id']."_".$recent_emails[$k]['contract_id'].".pdf");
  2340.             for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
  2341.                 $templateId = $pdf->importPage($pageNo);
  2342.                 $pdf->AddPage();
  2343.                 $pdf->useTemplate($templateId, ['adjustPageSize' => true]);
  2344.                 $size=$pdf->getTemplateSize ($templateId);
  2345.             }
  2346.             $pdf->Image($recent_emails[$k]['img'],10,$h,50,50);
  2347.             $pdf->SetFont('Helvetica');
  2348.             $pdf->SetXY(10,$h-15);
  2349.             $pdf->Write(8, utf8_decode('Le stagiaire'));
  2350.             $pdf->SetXY(10,$h-10);
  2351.             $pdf->Write(8, utf8_decode($recent_emails[$k]['name']));
  2352.             $pdf->SetXY(10,$h-5);
  2353.             $pdf->Write(8, 'le '.date('d/m/Y').' a '.date('H:i:s'));
  2354.             $pdf->Output('/var/www/clients/client0/web1/web/docs/attestation_entree_'.$recent_emails[$k]['order_id'].'_'.$recent_emails[$k]['contact_id'].'_'.$recent_emails[$k]['contract_id'].'_signed.pdf','F');
  2355.             send_email('serbastien.ferrand@gmail.com',
  2356.                 'L\'attestation d\'entrée en formation '.$recent_emails[$k]['order_id'].' est signé par le stagiaire.',
  2357.                 'L\'attestation d\'entrée en formation '.$recent_emails[$k]['order_id'].' est signé par le stagiaire et est en pièce jointe.<br/>
  2358.             <br/>
  2359.             Retrouver l\'attestation d\'entrée en formation signé sur le CRM StraFormation <a href="http://straformation.test/SuiteCRM/index.php?module=AOS_Contracts&action=DetailView&record='.$recent_emails[$k]['contract_id'].'&offset=1&time='.time().'" target="_blank">ici</a>.',
  2360.                 '/var/www/clients/client0/web1/web/docs/attestation_entree_'.$recent_emails[$k]['order_id'].'_'.$recent_emails[$k]['contact_id'].'_'.$recent_emails[$k]['contract_id'].'_signed.pdf',
  2361.                 'attestation-entree-signe-'.$recent_emails[$k]['order_id'].'-'.$recent_emails[$k]['contact_id'].'-'.$recent_emails[$k]['contract_id'].'-chez-StraFormation.pdf'
  2362.             );
  2363.             wp_delete_post($v['ID'], true);
  2364.         }
  2365.         //////////////////////////////////////////////////////
  2366.         // SIGNER ATTESTATION DEBUT DE FORMATION
  2367.         if($v['post_title']=='Signer attestation fin en formation' && $user_nom->roles[0]=='stagiaire') {
  2368.             $recent_emails[$k]['meta']=get_post_meta($v['ID']);
  2369.             $recent_emails[$k]['img']=str_replace('http://straformation.test/','/var/www/clients/client0/web1/web/',$recent_emails[$k]['meta']['_field_signature-241'][0]);
  2370.             $recent_emails[$k]['email']=$recent_emails[$k]['meta']['_field_your-email'][0];
  2371.             $recent_emails[$k]['name']=$recent_emails[$k]['meta']['_field_your-name'][0];
  2372.             $recent_emails[$k]['order_id']=$recent_emails[$k]['meta']['_field_order_id'][0];
  2373.             $recent_emails[$k]['contact_id']=$recent_emails[$k]['meta']['_field_contact_id'][0];
  2374.             $recent_emails[$k]['contract_id']=$recent_emails[$k]['meta']['_field_contract_id'][0];
  2375.             ///
  2376.             if(is_file('/var/www/clients/client0/web1/web/docs/attestation_fin_'.$recent_emails[$k]['order_id'].'_'.$recent_emails[$k]['contact_id'].'_'.$recent_emails[$k]['contract_id'].'_signed.pdf')) {
  2377.                 wp_delete_post($v['ID'], true);
  2378.                 continue;
  2379.             }
  2380.             $pdf = new Fpdi\Fpdi();
  2381.             $h=get_height_from_png("/var/www/clients/client0/web1/web/docs/attestation_fin_".$recent_emails[$k]['order_id']."_".$recent_emails[$k]['contact_id']."_".$recent_emails[$k]['contract_id'].".pdf");
  2382.             $h=round($h/842*297);
  2383.             $pageCount=$pdf->setSourceFile("/var/www/clients/client0/web1/web/docs/attestation_fin_".$recent_emails[$k]['order_id']."_".$recent_emails[$k]['contact_id']."_".$recent_emails[$k]['contract_id'].".pdf");
  2384.             for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
  2385.                 $templateId = $pdf->importPage($pageNo);
  2386.                 $pdf->AddPage();
  2387.                 $pdf->useTemplate($templateId, ['adjustPageSize' => true]);
  2388.                 $size=$pdf->getTemplateSize ($templateId);
  2389.             }
  2390.             $pdf->Image($recent_emails[$k]['img'],10,$h,50,50);
  2391.             $pdf->SetFont('Helvetica');
  2392.             $pdf->SetXY(10,$h-15);
  2393.             $pdf->Write(8, utf8_decode('Le stagiaire'));
  2394.             $pdf->SetXY(10,$h-10);
  2395.             $pdf->Write(8, utf8_decode($recent_emails[$k]['name']));
  2396.             $pdf->SetXY(10,$h-5);
  2397.             $pdf->Write(8, 'le '.date('d/m/Y').' a '.date('H:i:s'));
  2398.             $pdf->Output('/var/www/clients/client0/web1/web/docs/attestation_fin_'.$recent_emails[$k]['order_id'].'_'.$recent_emails[$k]['contact_id'].'_'.$recent_emails[$k]['contract_id'].'_signed.pdf','F');
  2399.             send_email('serbastien.ferrand@gmail.com',
  2400.                 'L\'attestation de fin de formation '.$recent_emails[$k]['order_id'].' est signé par le stagiaire.',
  2401.                 'L\'attestation de fin de formation '.$recent_emails[$k]['order_id'].' est signé par le stagiaire et est en pièce jointe.<br/>
  2402.             <br/>
  2403.             Retrouver l\'attestation de fin de formation signé sur le CRM StraFormation <a href="http://straformation.test/SuiteCRM/index.php?module=AOS_Contracts&action=DetailView&record='.$recent_emails[$k]['contract_id'].'&offset=1&time='.time().'" target="_blank">ici</a>.',
  2404.                 '/var/www/clients/client0/web1/web/docs/attestation_fin_'.$recent_emails[$k]['order_id'].'_'.$recent_emails[$k]['contact_id'].'_'.$recent_emails[$k]['contract_id'].'_signed.pdf',
  2405.                 'attestation-fin-signe-'.$recent_emails[$k]['order_id'].'-'.$recent_emails[$k]['contact_id'].'-'.$recent_emails[$k]['contract_id'].'-chez-StraFormation.pdf'
  2406.             );
  2407.             wp_delete_post($v['ID'], true);
  2408.         }
  2409.         //////////////////////////////////////////////////////////////////////
  2410.         if(is_file($recent_emails[$k]['img'])) { unlink($recent_emails[$k]['img']); }
  2411.     }
  2412. }
  2413. //add_action( 'wpcf7_after_flamingo', 'action_wpcf7_after_flamingo', 10, 1 );
  2414. add_action( 'init', 'action_wpcf7_after_flamingo', 10, 1 );
  2415.  
  2416.  
  2417.  
  2418.  
  2419.  
  2420.  
  2421.  
  2422.  
  2423.  
  2424.  
  2425.  
  2426.  
  2427.  
  2428.  
  2429.  
  2430.  
  2431.  
  2432.  
  2433. /////////////////////////////////////////////////////////////////////////////////////
  2434. //TODO : ADD MY ACCOUNT EXTRAS ACCUEIL TEXT
  2435. function wc_get_customer_orders() {
  2436.     // Get all customer orders
  2437.     $customer_orders = get_posts( array(
  2438.         'numberposts' => -1,
  2439.         'meta_key'    => '_customer_user',
  2440.         'meta_value'  => get_current_user_id(),
  2441.         'post_type'   => wc_get_order_types(),
  2442.         'post_status' => array_keys( wc_get_order_statuses() ),
  2443.     ) );
  2444.     $customer = wp_get_current_user();
  2445.     $loyal_count = 5;
  2446.     $notice_text = sprintf( 'Hey %1$s &#x1f600; We noticed you\'ve placed more than %2$s orders with us – thanks for being a loyal customer!', $customer->display_name, $loyal_count );
  2447.     if ( count( $customer_orders ) >= $loyal_count ) {
  2448.         wc_print_notice( $notice_text, 'notice' );
  2449.     }
  2450. }
  2451. //add_action( 'woocommerce_before_my_account', 'wc_get_customer_orders' );
  2452.  
  2453.  
  2454.  
  2455.  
  2456.  
  2457.  
  2458.  
  2459.  
  2460.  
  2461.  
  2462.  
  2463.  
  2464.  
  2465.  
  2466.  
  2467.  
  2468.  
  2469.  
  2470.  
  2471.  
  2472.  
  2473.  
  2474.  
  2475.  
  2476.  
  2477.  
  2478.  
  2479.  
  2480.  
  2481.  
  2482.  
  2483.  
  2484.  
  2485. /**
  2486. add_filter ( 'woocommerce_account_menu_items', 'misha_remove_my_account_links' );
  2487. function misha_remove_my_account_links( $menu_links ){
  2488. $user=wp_get_current_user();
  2489. if($user->roles[0]=='formateur') {
  2490. unset( $menu_links['orders'] ); // Orders
  2491. return $menu_links;
  2492. }
  2493. //unset( $menu_links['edit-address'] ); // Addresses
  2494. //unset( $menu_links['dashboard'] ); // Dashboard
  2495. //unset( $menu_links['payment-methods'] ); // Payment Methods
  2496. //unset( $menu_links['downloads'] ); // Downloads
  2497. //unset( $menu_links['edit-account'] ); // Account details
  2498. //unset( $menu_links['customer-logout'] ); // Logout
  2499. return $menu_links;
  2500. }
  2501. /////////////////////////////////////////////////////////////////////////////////////
  2502. //ADD MY ACCOUNT EXTRAS LINKS
  2503. add_filter ( 'woocommerce_account_menu_items', 'misha_more_link' );
  2504. function misha_more_link( $menu_links ){
  2505. //if($user->roles[0]!='formateur') { return $menu_links; }
  2506. $new = array( 'stagiaires' => 'Mes stagiaires' );
  2507. $new2 = array( 'formations' => 'Mes Formations' );
  2508. $menu_links = array_slice( $menu_links, 0, 1, true )
  2509. + $new
  2510. + $new2
  2511. + array_slice( $menu_links, 1, NULL, true );
  2512. return $menu_links;
  2513. }
  2514. //
  2515. add_filter( 'woocommerce_get_endpoint_url', 'misha_hook_endpoint', 10, 4 );
  2516. function misha_hook_endpoint( $url, $endpoint, $value, $permalink ){
  2517. if( $endpoint === 'client' ) { $url = '/mes-clients-straformation/'; }
  2518. if( $endpoint === 'formations' ) { $url = '/mes-formations-straformation/'; }
  2519. return $url;
  2520. }
  2521.  **/
  2522.  
  2523. ////////////////////////////////////////////////////////////////////////
  2524. //TWEAK MENU CUSTOMER / FORMATEUR USER TYPE
  2525. function my_custom_endpoints() {
  2526.     add_rewrite_endpoint( 'formations', EP_ROOT | EP_PAGES );
  2527.     add_rewrite_endpoint( 'stagiaires', EP_ROOT | EP_PAGES );
  2528.     add_rewrite_endpoint( 'sessions', EP_ROOT | EP_PAGES );
  2529.     add_rewrite_endpoint( 'contracts', EP_ROOT | EP_PAGES );
  2530. }
  2531. add_action( 'init', 'my_custom_endpoints' );
  2532. ///
  2533. function my_custom_query_vars( $vars ) {
  2534.     $vars[] = 'stagiaires';
  2535.     $vars[] = 'formations';
  2536.     $vars[] = 'sessions';
  2537.     $vars[] = 'contracts';
  2538.     return $vars;
  2539. }
  2540. add_filter( 'query_vars', 'my_custom_query_vars', 0 );
  2541. ///
  2542. function my_custom_flush_rewrite_rules() { flush_rewrite_rules(); }
  2543. add_action( 'wp_loaded', 'my_custom_flush_rewrite_rules' );
  2544. ///
  2545. function my_custom_my_account_menu_items( $items ) {
  2546.     $user=wp_get_current_user();
  2547.     if($user->roles[0]=='formateur') {
  2548.         $items = array(
  2549.             'dashboard'         => __( 'Dashboard', 'woocommerce' ),
  2550.             //'orders'            => __( 'Orders', 'woocommerce' ),
  2551.             //'downloads'       => __( 'Downloads', 'woocommerce' ),
  2552.             'formations'      => 'Capacités de formations',
  2553.             'contracts'      => 'Contracts de formations',
  2554.             'sessions'      => 'Sessions de formations',
  2555.             'stagiaires'      => 'Stagiaires en formation',
  2556.             'edit-address'    => 'Mes informations',
  2557.             'edit-account'      => 'Mon compte',
  2558.             'customer-logout'   => __( 'Logout', 'woocommerce' ),
  2559.             //'payment-methods' => __( 'Payment Methods', 'woocommerce' ),
  2560.         );
  2561.     } elseif($user->roles[0]=='customer') {
  2562.         $items = array(
  2563.             'dashboard'         => __( 'Dashboard', 'woocommerce' ),
  2564.             'orders'            => __( 'Orders', 'woocommerce' ),
  2565.             //'downloads'       => __( 'Downloads', 'woocommerce' ),
  2566.             'contracts'      => 'Contracts de formations',
  2567.             'edit-address'    => 'Mes informations',
  2568.             'edit-account'      => 'Mon compte',
  2569.             'customer-logout'   => __( 'Logout', 'woocommerce' ),
  2570.             //'payment-methods' => __( 'Payment Methods', 'woocommerce' ),
  2571.         );
  2572.     } elseif($user->roles[0]=='stagiaire') {
  2573.         $items = array(
  2574.             'dashboard'         => __( 'Dashboard', 'woocommerce' ),
  2575.             'contracts'      => 'Contracts de formations',
  2576.             'sessions'      => 'Sessions Formations',
  2577.             //'orders'            => __( 'Orders', 'woocommerce' ),
  2578.             //'downloads'       => __( 'Downloads', 'woocommerce' ),
  2579.             'edit-address'    => 'Mes informations',
  2580.             'edit-account'      => 'Mon compte',
  2581.             'customer-logout'   => __( 'Logout', 'woocommerce' ),
  2582.             //'payment-methods' => __( 'Payment Methods', 'woocommerce' ),
  2583.         );
  2584.     }
  2585.     return $items;
  2586. }
  2587. add_filter( 'woocommerce_account_menu_items', 'my_custom_my_account_menu_items' );
  2588. ///
  2589. function my_formations_endpoint_content() { include 'woocommerce/myaccount/formations.php'; }
  2590. add_action( 'woocommerce_account_formations_endpoint', 'my_formations_endpoint_content' );
  2591. ///
  2592. function my_stagiaires_endpoint_content() { include 'woocommerce/myaccount/stagiaires.php'; }
  2593. add_action( 'woocommerce_account_stagiaires_endpoint', 'my_stagiaires_endpoint_content' );
  2594. ///
  2595. function my_sessions_endpoint_content() { include 'woocommerce/myaccount/sessions.php'; }
  2596. add_action( 'woocommerce_account_sessions_endpoint', 'my_sessions_endpoint_content' );
  2597. ///
  2598. function my_contracts_endpoint_content() { include 'woocommerce/myaccount/contracts.php'; }
  2599. add_action( 'woocommerce_account_contracts_endpoint', 'my_contracts_endpoint_content' );
  2600.  
  2601.  
  2602.  
  2603.  
  2604.  
  2605.  
  2606.  
  2607.  
  2608.  
  2609.  
  2610.  
  2611.  
  2612.  
  2613.  
  2614.  
  2615.  
  2616.  
  2617. /////////////////////////////////////////////////////////////////////////////////////
  2618. //BULK IMPORT FROM BDD
  2619. //add_action( 'wp_loaded', 'import_all' );
  2620. function import_all() {
  2621.     global $wpdb;
  2622. /////////////////////////////////////////////////////////////////////////////////////
  2623. //CONNECT TO SUITE CRM BDD
  2624.     $db_host="127.0.0.1"; $db_user="root"; $db_pass="root"; $db_db="suitecrm";
  2625.     $con=mysqli_connect($db_host, $db_user,$db_pass,$db_db) or die("erreur de connexion au serveur");
  2626.     if (mysqli_connect_errno()) { printf("Échec de la connexion : %s\n", mysqli_connect_error()); exit(); }
  2627.     mysqli_query($con,"SET NAMES UTF8");
  2628. /////////////////////////////////////////////////////////////////////////////////////
  2629. //RAZ SUITE CRM
  2630.     $raz="TRUNCATE `accounts`;
  2631. TRUNCATE `accounts_audit`;
  2632. TRUNCATE `accounts_cstm`;
  2633. TRUNCATE `accounts_opportunities`;
  2634. TRUNCATE `alerts`;
  2635. TRUNCATE `aos_line_item_groups`;
  2636. TRUNCATE `aos_line_item_groups_audit`;
  2637. TRUNCATE `aos_products`;
  2638. TRUNCATE `aos_products_audit`;
  2639. TRUNCATE `aos_products_quotes`;
  2640. TRUNCATE `aos_products_quotes_audit`;
  2641. TRUNCATE `aos_product_categories`;
  2642. TRUNCATE `aos_quotes`;
  2643. TRUNCATE `aos_quotes_audit`;
  2644. TRUNCATE `calls`;
  2645. TRUNCATE `calls_users`;
  2646. TRUNCATE `opportunities`;
  2647. TRUNCATE `opportunities_audit`;
  2648. TRUNCATE `reminders`;
  2649. TRUNCATE `reminders_invitees`;
  2650. TRUNCATE `sugarfeed`;
  2651. TRUNCATE `aos_invoices`;
  2652. TRUNCATE `aod_index`;
  2653. TRUNCATE `aod_indexevent`;
  2654. TRUNCATE `aos_quotes_aos_invoices_c`;
  2655. TRUNCATE `opportunities_cstm`;
  2656. TRUNCATE `acl_actions`;
  2657. TRUNCATE `aod_index`;
  2658. TRUNCATE `job_queue`;
  2659. TRUNCATE `aos_products_cstm`;
  2660. TRUNCATE `relationships`;
  2661. TRUNCATE `aos_contracts_audit`;
  2662. TRUNCATE `emails_text`;
  2663. TRUNCATE `emails`;
  2664. TRUNCATE `notes`;
  2665. TRUNCATE `contacts_audit`;
  2666. TRUNCATE `sess_formateurs`;
  2667. TRUNCATE `sess_formateurs_cstm`;
  2668. TRUNCATE `contacts`;
  2669. TRUNCATE `accounts_contacts`;
  2670. TRUNCATE `contacts_cstm`;
  2671. TRUNCATE `sess_formateurs_meetings_c`;
  2672. TRUNCATE `aos_invoices_audit`;
  2673. TRUNCATE `opportunities_contacts`;
  2674. TRUNCATE `emails_beans`;
  2675. TRUNCATE `aos_invoices_cstm`;
  2676. TRUNCATE `meetings_contacts`;
  2677. TRUNCATE `opportunities_meetings_1_c`;
  2678. TRUNCATE `sess_formateurs_aos_products_c`;
  2679. TRUNCATE `opportunities_aos_products_1_c`;
  2680. TRUNCATE `aos_quotes_cstm`;
  2681. TRUNCATE `sess_formateurs_audit`;
  2682. TRUNCATE `meetings_cstm`;
  2683. TRUNCATE `meetings_users`;
  2684. TRUNCATE `meetings`;
  2685. TRUNCATE `documents_accounts`;
  2686. TRUNCATE `aos_quotes_os_contracts_c`;
  2687. TRUNCATE `opportunities_sess_formateurs_1_c`;
  2688. TRUNCATE `sess_formateurs_aos_contracts_c`;
  2689. TRUNCATE `opportunities_accounts_1_c`;
  2690. TRUNCATE `sess_formateurs_aos_products_1_c`;
  2691. TRUNCATE `opportunities_aos_invoices_1_c`;
  2692. TRUNCATE `aos_contracts`;
  2693. TRUNCATE `document_revisions`;
  2694. TRUNCATE `opportunities_fp_event_locations_1_c`;
  2695. TRUNCATE `documents`;
  2696. TRUNCATE `sess_formateurs_audit`;
  2697. TRUNCATE `aos_quotes_cstm`;
  2698. TRUNCATE `meetings_cstm`;
  2699. TRUNCATE `meetings_users`;
  2700. TRUNCATE `meetings`;
  2701.  
  2702.  
  2703.  
  2704. ";
  2705.     foreach(explode("\n",$raz) as $ras) { mysqli_query($con,$ras); }
  2706.     /////////////////////////////////////////////////////////////////////////////////////
  2707.     //CONNEXION SUITE CRM
  2708.     $url = "http://suitecrm.straformation.fr/service/v4_1/rest.php";
  2709.     $username = "admin";
  2710.     $password = "waRR10RP0wa";
  2711.     function call($method, $parameters, $url) {
  2712.         ob_start();
  2713.         $curl_request = curl_init();
  2714.         curl_setopt($curl_request, CURLOPT_URL, $url);
  2715.         curl_setopt($curl_request, CURLOPT_POST, 1);
  2716.         curl_setopt($curl_request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
  2717.         curl_setopt($curl_request, CURLOPT_HEADER, 1);
  2718.         curl_setopt($curl_request, CURLOPT_SSL_VERIFYPEER, 0);
  2719.         curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1);
  2720.         curl_setopt($curl_request, CURLOPT_FOLLOWLOCATION, 0);
  2721.         $jsonEncodedData = json_encode($parameters);
  2722.         $post = array(
  2723.             "method" => $method,
  2724.             "input_type" => "JSON",
  2725.             "response_type" => "JSON",
  2726.             "rest_data" => $jsonEncodedData
  2727.         );
  2728.         curl_setopt($curl_request, CURLOPT_POSTFIELDS, $post);
  2729.         $result = curl_exec($curl_request);
  2730.         curl_close($curl_request);
  2731.         $result = explode("\r\n\r\n", $result, 2);
  2732.         $response = json_decode($result[1]);
  2733.         ob_end_flush();
  2734.         return $response;
  2735.     }
  2736.     //////////////////////////////////////////////////////
  2737.     //login ---------------------------------------------
  2738.     $login_parameters = array("user_auth" => array("user_name" => $username,"password" => md5($password),"version" => "1"),"application_name" => "Woocommerce","name_value_list" => array(),);
  2739.     $login_result = call("login", $login_parameters, $url); $session_id = $login_result->id; //echo "<pre>".print_r($login_result,true)."</pre>";
  2740. /////////////////////////////////////////////////////////////////////////////////////
  2741.     $result=mysqli_query($con,"SELECT * FROM  llx_agefodd_formation_catalogue"); $z=0;
  2742.     while($obj=mysqli_fetch_assoc($result)) {
  2743.         $products[$z]['sku']=$obj['ref'];
  2744.         $products[$z]['titre']=$obj['intitule'];
  2745.         $products[$z]['duree']=strip_tags($obj['duree'])+0;//field_5aec6f798e4e1
  2746.         $products[$z]['public_concerne']=$obj['public'];//field_5aec70868e4e2
  2747.         $products[$z]['pre-requis']=$obj['prerequis'];//field_5aec70bd8e4e3
  2748.         $products[$z]['but_de_la_formation']=$obj['but'];//field_5aec6ef78e4e0
  2749.         $products[$z]['programme']=$obj['programme'];//field_5aec70ed8e4e4
  2750.         $products[$z]['methodes_pedagogiques']=$obj['methode'];//field_5af1b8e5735e9
  2751.         $products[$z]['moyens_pedagogiques']=$obj['pedago_usage'];//field_5af1befce4c32
  2752.         $products[$z]['equipements_necessaires']=$obj['note2'];//field_5af1bba6735ea
  2753.         $products[$z]['documents_necessaires']=$obj['note1'];//field_5af1bdfae4c31
  2754.         $products[$z]['fin_de_la_formation']=$obj['sanction'];//field_5af1bc65735eb
  2755.         $result2=mysqli_query($con,"SELECT * FROM  llx_product WHERE rowid='".$obj['fk_product']."'"); $obj2=mysqli_fetch_assoc($result2);
  2756.         $products[$z]['description']=$obj2['description'].'';
  2757.         $products[$z]['price_ht']=$obj2['price']+0;
  2758.         $products[$z]['price_ttc']=$obj2['price_ttc']+0;
  2759.         $result4=mysqli_query($con,"SELECT * FROM  llx_categorie_product WHERE fk_product='".$obj['fk_product']."'");
  2760.         while($obj4=mysqli_fetch_assoc($result4)) {
  2761.             $result3=mysqli_query($con,"SELECT * FROM  llx_categorie WHERE rowid='".$obj4['fk_categorie']."'"); $obj3=mysqli_fetch_assoc($result3);
  2762.             $item['cat_name']=$obj3['label'].'';
  2763.             if($item['cat_name']=='Marketing') { $item['cat_name']='Marketing - Web marketing'; }
  2764.             if($item['cat_name']=='Fiscalité - Droit des Affaires') { $item['cat_name']='Droit social - Fiscalité - Droit des Affaires'; }
  2765.             if($item['cat_name']!='') { $products[$z]['cat_name'][$item['cat_name']]=$item['cat_name']; }
  2766.         }
  2767.         $result3=mysqli_query($con,"SELECT * FROM  llx_agefodd_formation_catalogue_type WHERE rowid='".$obj['fk_c_category']."'"); $obj3=mysqli_fetch_assoc($result3);
  2768.         $item['cat_name']=$obj3['intitule'].'';
  2769.         if($item['cat_name']=='Marketing') { $item['cat_name']='Marketing - Web marketing'; }
  2770.         if($item['cat_name']=='Fiscalité - Droit des Affaires') { $item['cat_name']='Droit social - Fiscalité - Droit des Affaires'; }
  2771.         if($item['cat_name']!='') { $products[$z]['cat_name'][$item['cat_name']]=$item['cat_name']; }
  2772.         ///
  2773.         $argsCat = array(
  2774.             'taxonomy'     => 'product_cat',
  2775.             'orderby'      => 'id',
  2776.             'show_count'   => 0,
  2777.             'pad_counts'   => 0,
  2778.             'hierarchical' => 1,
  2779.             'title_li'     => '',
  2780.             'hide_empty'   => 0
  2781.         ); $cat=0;
  2782.         $terms = get_categories($argsCat); unset($cat_array);
  2783.         foreach($products[$z]['cat_name'] as $cat_name) {
  2784.             $cat=0;
  2785.             foreach($terms as $term) { if(sanitize_title($term->name)==sanitize_title($cat_name)) { $cat=$term->term_id; $products[$z]['cat_id'][$cat]=$cat; } }
  2786.         }
  2787.         if(count($products[$z]['cat_id'])>0) { sort($products[$z]['cat_id']); }
  2788.         ///
  2789.         $z++;
  2790.     }
  2791.     //echo '<pre>'.print_r($products,true).'</pre>'; exit;
  2792.     foreach( $products as $item) {
  2793.         //echo '<pre>'.print_r($item,true).'</pre>'; exit;
  2794.         $product_id = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key='_sku' AND meta_value='%s' LIMIT 1", $item['sku'] ) );
  2795.         if($product_id>0) { continue; }
  2796.  
  2797.         $post_id = wp_insert_post( array(
  2798.             'post_title' => $item['titre'],
  2799.             'post_content' => $item['description'],
  2800.             'post_status' => 'publish',
  2801.             'post_type' => "product",
  2802.         ) );
  2803.         wp_set_object_terms( $post_id, 'simple', 'product_type' );
  2804.         update_post_meta( $post_id, '_visibility', 'visible' );
  2805.         update_post_meta( $post_id, '_stock_status', 'instock');
  2806.         update_post_meta( $post_id, 'total_sales', '0' );
  2807.         update_post_meta( $post_id, '_downloadable', 'no' );
  2808.         update_post_meta( $post_id, '_virtual', 'yes' );
  2809.         update_post_meta( $post_id, '_regular_price', $item['price_ht'] );
  2810.         update_post_meta( $post_id, '_sale_price', '' );
  2811.         update_post_meta( $post_id, '_purchase_note', '' );
  2812.         update_post_meta( $post_id, '_featured', 'no' );
  2813.         update_post_meta( $post_id, '_weight', '' );
  2814.         update_post_meta( $post_id, '_length', '' );
  2815.         update_post_meta( $post_id, '_width', '' );
  2816.         update_post_meta( $post_id, '_height', '' );
  2817.         update_post_meta( $post_id, '_sku', $item['sku'] );
  2818.         update_post_meta( $post_id, '_product_attributes', array() );
  2819.         update_post_meta( $post_id, '_sale_price_dates_from', '' );
  2820.         update_post_meta( $post_id, '_sale_price_dates_to', '' );
  2821.         update_post_meta( $post_id, '_price', '' );
  2822.         update_post_meta( $post_id, '_sold_individually', '' );
  2823.         update_post_meta( $post_id, '_manage_stock', 'no' );
  2824.         update_post_meta( $post_id, '_backorders', 'no' );
  2825.         update_post_meta( $post_id, '_stock', '' );
  2826.         /////////////////////////////////////////////////////////////////////////////////////
  2827.         $the_product = new WC_Product($post_id);
  2828.         $the_product->set_tax_class('standard');
  2829.         $the_product->set_reviews_allowed(false);
  2830.         $the_product->set_price($item['price_ht']);
  2831.         $the_product->set_regular_price($item['price_ht']);
  2832.         /////////////////////////////////////////////////////////////////////////////////////
  2833.         update_field( 'field_5aec6f798e4e1', strip_tags($item['duree'])+0, $post_id); // duree
  2834.         update_field( 'field_5aec70868e4e2', $item['public_concerne'], $post_id); // public_concerne
  2835.         update_field( 'field_5aec70bd8e4e3', $item['pre-requis'], $post_id); // pre-requis
  2836.         update_field( 'field_5aec6ef78e4e0', $item['but_de_la_formation'], $post_id); // but_de_la_formation
  2837.         update_field( 'field_5aec70ed8e4e4', $item['programme'], $post_id); // programme
  2838.         update_field( 'field_5af1b8e5735e9', $item['methodes_pedagogiques'], $post_id); // methodes_pedagogiques
  2839.         update_field( 'field_5af1befce4c32', $item['moyens_pedagogiques'], $post_id); // moyens_pedagogiques
  2840.         update_field( 'field_5af1bba6735ea', $item['equipements_necessaires'], $post_id); // equipements_necessaires
  2841.         update_field( 'field_5af1bdfae4c31', $item['documents_necessaires'], $post_id); // documents_necessaires
  2842.         update_field( 'field_5af1bc65735eb', $item['fin_de_la_formation'], $post_id); // fin_de_la_formation
  2843.         update_field( 'field_5aed8268825f9', 0, $post_id); // cout
  2844.         update_field( 'field_5aecddb101dbc', '<table class="explication_des_prix"><tbody><tr>
  2845.     <td style="text-align: center;">Nombre de stagiaires&nbsp;</td>
  2846.     <td style="text-align: center;">De 1 à 2</td>
  2847.     <td style="text-align: center;">De 3 à 9</td>
  2848. </tr><tr>
  2849.     <td style="text-align: center;">Tarif horaire HT&nbsp;</td>
  2850.     <td style="text-align: center;">Nous contacter</td>
  2851.     <td style="text-align: center;">29 € HT / h</td>
  2852. </tr></tbody></table>', $post_id); // explication_des_prix
  2853.         /////////////////////////////////////////////////////////////////////////////////////
  2854.         //ASSIGNER LA CATEGORIE
  2855.         if(count($item['cat_id'])>0) { wp_set_object_terms($post_id, $item['cat_id'], 'product_cat'); }
  2856.         if(isset($item['cat_id'][0]) && $item['cat_id'][0]>0) { if(!add_post_meta($post_id, '_yoast_wpseo_primary_product_cat', $item['cat_id'][0], true)) { update_post_meta($post_id, '_yoast_wpseo_primary_product_cat', $item['cat_id'][0]); } }
  2857.         //echo '<pre>'.print_r($item,true).'</pre>'; exit;
  2858.         $the_product->save();
  2859.         //////////////////////////////////////////////////////
  2860.         //get WOO Categorie -------------------------------------
  2861.         $terms = get_the_terms($post_id, 'product_cat');
  2862.         foreach ($terms as $term) { if($term->term_id==$item['cat_id'][0]) { $product_cat=$term; break; } }
  2863.         if ($product_cat->name!='') { $product_cat_title=str_replace("'","`",$product_cat->name); }
  2864.         //////////////////////////////////////////////////////
  2865.         //get AOS_Product_Categories -------------------------------------
  2866.         $get_entry_list_parameters = array(
  2867.             'session' => $session_id,
  2868.             'module_name' => 'AOS_Product_Categories',
  2869.             'query' => "aos_product_categories.name='".$product_cat_title."'",     //The SQL WHERE clause without the word "where".
  2870.             'order_by' => "",                              //The SQL ORDER BY clause without the phrase "order by".
  2871.             'offset' => '0',                               //The record offset from which to start.
  2872.             'select_fields' => array(                      //Optional. A list of fields to include in the results.
  2873.                 'id',
  2874.                 'name',
  2875.             ),
  2876.             /*
  2877.             A list of link names and the fields to be returned for each link name.
  2878.             Example: 'link_name_to_fields_array' => array(array('name' => 'email_addresses', 'value' => array('id', 'email_address', 'opt_out', 'primary_address')))
  2879.             */
  2880.             'link_name_to_fields_array' => array(),
  2881.             'max_results' => '1',      //The maximum number of results to return.
  2882.             'deleted' => '0',          //To exclude deleted records
  2883.             'Favorites' => false,      //If only records marked as favorites should be returned.
  2884.         );
  2885.         $get_entry_list_result = call('get_entry_list', $get_entry_list_parameters, $url); //echo "<pre>".print_r($get_entry_list_result,true)."</pre>";
  2886.         $aos_product_categories_entry_id = $get_entry_list_result->entry_list[0]->id; //echo "<pre>AOS_Product_Categories LOAD : ".print_r($aos_product_categories_entry_id,true)."</pre>";
  2887.         //////////////////////////////////////////////////////
  2888.         //create or update AOS_Product_Categories -------------------------------------
  2889.         $set_entry_parameters = array(
  2890.             "session" => $session_id,
  2891.             "module_name" => "AOS_Product_Categories",
  2892.             "name_value_list" => array(
  2893.                 array("name" => "assigned_user_name", "value" => 'Administrator'),
  2894.                 array("name" => "assigned_user_id", "value" => '1'),
  2895.                 array("name" => "id", "value" => $aos_product_categories_entry_id), // TO UPDATE
  2896.                 array("name" => "name", "value" => $product_cat_title),
  2897.                 array("name" => "is_parent", "value" => "1"),
  2898.             ),
  2899.         );
  2900.         $set_entry_result = call("set_entry", $set_entry_parameters, $url); //echo "<pre>".print_r($set_entry_result,true)."</pre>";
  2901.         $aos_product_categories_entry_id = $set_entry_result->id; //echo "<pre>AOS_Product_Categories CREATE OR UPDATE : ".print_r($aos_product_categories_entry_id,true)."</pre>";
  2902.         update_term_meta($cat, 'aos_product_categories_entry_id', $aos_product_categories_entry_id);
  2903.         //////////////////////////////////////////////////////
  2904.         //get WOO Title -------------------------------------
  2905.         $product_title=str_replace("'","`",$the_product->get_title());
  2906.         $product_sku=str_replace("'","`",$the_product->get_sku());
  2907.         $product_price=str_replace("'","`",$the_product->get_price());
  2908.         $product_url=get_permalink($post_id);
  2909.         //////////////////////////////////////////////////////
  2910.         //get AOS_Products -------------------------------------
  2911.         $get_entry_list_parameters = array(
  2912.             'session' => $session_id,
  2913.             'module_name' => 'AOS_Products',
  2914.             'query' => "aos_products.part_number='".$product_sku."'",     //The SQL WHERE clause without the word "where".
  2915.             'order_by' => "",                              //The SQL ORDER BY clause without the phrase "order by".
  2916.             'offset' => '0',                               //The record offset from which to start.
  2917.             'select_fields' => array(                      //Optional. A list of fields to include in the results.
  2918.                 'id',
  2919.                 'name',
  2920.             ),
  2921.             /*
  2922.             A list of link names and the fields to be returned for each link name.
  2923.             Example: 'link_name_to_fields_array' => array(array('name' => 'email_addresses', 'value' => array('id', 'email_address', 'opt_out', 'primary_address')))
  2924.             */
  2925.             'link_name_to_fields_array' => array(),
  2926.             'max_results' => '1',      //The maximum number of results to return.
  2927.             'deleted' => '0',          //To exclude deleted records
  2928.             'Favorites' => false,      //If only records marked as favorites should be returned.
  2929.         );
  2930.         $get_entry_list_result = call('get_entry_list', $get_entry_list_parameters, $url); //echo "<pre>".print_r($get_entry_list_result,true)."</pre>";
  2931.         $aos_products_entry_id = $get_entry_list_result->entry_list[0]->id; //echo "<pre>AOS_Products LOAD : ".print_r($aos_products_entry_id,true)."</pre>";
  2932.         //////////////////////////////////////////////////////
  2933.         //create or update AOS_Products -------------------------------------
  2934.         $set_entry_parameters = array(
  2935.             "session" => $session_id,
  2936.             "module_name" => "AOS_Products",
  2937.             "name_value_list" => array(
  2938.                 array("name" => "assigned_user_name", "value" => 'Administrator'),
  2939.                 array("name" => "assigned_user_id", "value" => '1'),
  2940.                 array("name" => "id", "value" => $aos_products_entry_id), // TO UPDATE
  2941.                 array("name" => "currency_id", "value" => "-99"),
  2942.                 array("name" => "name", "value" => $product_title),
  2943.                 array("name" => "description", "value" => $product_title),
  2944.                 array("name" => "maincode", "value" => "XXXX"),
  2945.                 array("name" => "part_number", "value" => $product_sku),
  2946.                 array("name" => "type", "value" => "Service"),
  2947.                 array("name" => "cost", "value" => "0"),
  2948.                 array("name" => "cost_usdollar", "value" => "0"),
  2949.                 array("name" => "currency_id", "value" => "-99"),
  2950.                 array("name" => "price", "value" => $product_price),
  2951.                 array("name" => "price_usdollar", "value" => $product_price),
  2952.                 array("name" => "url", "value" => $product_url),
  2953.                 array("name" => "aos_product_category_id", "value" => $aos_product_categories_entry_id),
  2954.             ),
  2955.         );
  2956.         $set_entry_result = call("set_entry", $set_entry_parameters, $url); //echo "<pre>".print_r($set_entry_result,true)."</pre>";
  2957.         $aos_products_entry_id = $set_entry_result->id; //echo "<pre>AOS_Products CREATE OR UPDATE : ".print_r($aos_products_entry_id,true)."</pre>";
  2958.         if(!add_post_meta($post_id, 'aos_products_entry_id',$aos_products_entry_id, true)) { update_post_meta($post_id, 'aos_products_entry_id',$aos_products_entry_id); }
  2959.     }
  2960.     reorder_products();
  2961. }
  2962. /////////////////////////////////////////////////////////////////////////////////////
  2963. //BULK IMPORT FROM BDD
  2964. //add_action( 'wp_loaded', 'import_all2' );
  2965. function import_all2() {
  2966.     $args = array('post_type' => array('product'), 'posts_per_page' => '-1',);
  2967.     $query=new WP_Query($args);
  2968.     if($query->posts) {
  2969.         foreach($query->posts as $post) {
  2970.             $post_id=$post->ID;
  2971.  
  2972.             $the_product = new WC_Product($post_id);
  2973.             /**
  2974.             $the_product->set_price(1);
  2975.             $the_product->set_regular_price(1);
  2976.             $the_product->set_tax_status('taxable');
  2977.             $the_product->set_tax_class('standard');//20%
  2978.             $the_product->set_manage_stock(false);
  2979.             $the_product->set_backorders('no');
  2980.  
  2981.  
  2982.             $the_product->set_reviews_allowed(false);
  2983.              **/
  2984.  
  2985.             /**
  2986.             $the_product->set_description('<p style="line-height:13px;font-size:11px;">A partir du 3ème membre inscrit de votre entreprise à cette formation !*<br/>
  2987.              *Offre non cumulable avec d’autres offres promotionnelles.<br/>
  2988.             Pour plus d’informations, contactez notre centre au 09 81 22 79 07.</p>');
  2989.              **/
  2990.  
  2991.             update_field( 'field_5aecddb101dbc', '<table class="explication_des_prix"><tbody><tr><td style="text-align: center;">Nombre de stagiaires</td><td style="text-align: center;">De 1 à 2</td><td style="text-align: center;">De 3 à 5</td><td style="text-align: center;">De 6 à 9</td></tr><tr><td style="text-align: center;">Tarif horaire HT</td><td style="background-color:#efefef;text-align: center;">Sur demande</td><td style="background-color:#efefef;text-align: center;">29 € HT / h</td><td style="background-color:#efefef;text-align: center;">20 € HT / h</td></tr></tbody></table>', $post_id);
  2992.  
  2993.             $the_product->save();
  2994.  
  2995.  
  2996.             /**
  2997.  
  2998.             $the_product->set_short_description('');
  2999.  
  3000.  
  3001.             $tags[]='Formation à Strasbourg (67) en Alsace';
  3002.             if(count($tags)>=1) { wp_set_object_terms($post_id, $tags, 'product_tag',false); }
  3003.  
  3004.              **/
  3005.  
  3006.             /**
  3007.             update_field( 'field_5aec6f798e4e1', $item['duree'], $post_id); // duree
  3008.             update_field( 'field_5aec70868e4e2', $item['public_concerne'], $post_id); // public_concerne
  3009.             update_field( 'field_5aec70bd8e4e3', $item['pre-requis'], $post_id); // pre-requis
  3010.             update_field( 'field_5aec6ef78e4e0', $item['but_de_la_formation'], $post_id); // but_de_la_formation
  3011.             update_field( 'field_5aec70ed8e4e4', $item['programme'], $post_id); // programme
  3012.             update_field( 'field_5af1b8e5735e9', $item['methodes_pedagogiques'], $post_id); // methodes_pedagogiques
  3013.             update_field( 'field_5af1befce4c32', $item['moyens_pedagogiques'], $post_id); // moyens_pedagogiques
  3014.             update_field( 'field_5af1bba6735ea', $item['equipements_necessaires'], $post_id); // equipements_necessaires
  3015.             update_field( 'field_5af1bdfae4c31', $item['documents_necessaires'], $post_id); // documents_necessaires
  3016.             update_field( 'field_5af1bc65735eb', $item['fin_de_la_formation'], $post_id); // fin_de_la_formation
  3017.             update_field( 'field_5aed8268825f9', 0, $post_id); // cout
  3018.  
  3019.              **/
  3020.  
  3021.             /**
  3022.             $aos_products_entry_id=get_post_meta($post_id, 'aos_products_entry_id', true ).'';
  3023.             if($aos_products_entry_id!='') {
  3024.             ////////////////////////////////////////////////////
  3025.             // BUG AVEC LES CHAMPS HTML
  3026.             $db_host="127.0.0.1"; $db_user="root"; $db_pass="root"; $db_db="suitecrm";
  3027.             $con=mysqli_connect($db_host, $db_user,$db_pass,$db_db) or die("erreur de connexion au serveur");
  3028.             if (mysqli_connect_errno()) { printf("Échec de la connexion : %s\n", mysqli_connect_error()); exit(); }
  3029.             mysqli_query($con,"SET NAMES UTF8");
  3030.             mysqli_query($con,"UPDATE aos_products_cstm SET public_concerne_c='".str_replace("'","\\'",get_field('public_concerne',$post_id))."' WHERE id_c='".$aos_products_entry_id."'");
  3031.             mysqli_query($con,"UPDATE aos_products_cstm SET pre_requis_c='".str_replace("'","\\'",get_field('pre-requis',$post_id))."' WHERE id_c='".$aos_products_entry_id."'");
  3032.             mysqli_query($con,"UPDATE aos_products_cstm SET but_de_la_formation_c='".str_replace("'","\\'",get_field('but_de_la_formation',$post_id))."' WHERE id_c='".$aos_products_entry_id."'");
  3033.             mysqli_query($con,"UPDATE aos_products_cstm SET programme_c='".str_replace("'","\\'",get_field('programme',$post_id))."' WHERE id_c='".$aos_products_entry_id."'");
  3034.             mysqli_query($con,"UPDATE aos_products_cstm SET methodes_pedagogiques_c='".str_replace("'","\\'",get_field('methodes_pedagogiques',$post_id))."' WHERE id_c='".$aos_products_entry_id."'");
  3035.             mysqli_query($con,"UPDATE aos_products_cstm SET moyens_pedagogiques_c='".str_replace("'","\\'",get_field('methodes_pedagogiques',$post_id))."' WHERE id_c='".$aos_products_entry_id."'");
  3036.             mysqli_query($con,"UPDATE aos_products_cstm SET equipements_necessaires_c='".str_replace("'","\\'",get_field('equipements_necessaires',$post_id))."' WHERE id_c='".$aos_products_entry_id."'");
  3037.             mysqli_query($con,"UPDATE aos_products_cstm SET documents_necessaires_c='".str_replace("'","\\'",get_field('documents_necessaires',$post_id))."' WHERE id_c='".$aos_products_entry_id."'");
  3038.             mysqli_query($con,"UPDATE aos_products_cstm SET fin_de_la_formation_c='".str_replace("'","\\'",get_field('fin_de_la_formation',$post_id))."' WHERE id_c='".$aos_products_entry_id."'");
  3039.             mysqli_query($con,"UPDATE aos_products_cstm SET duree_c='".(strip_tags(str_replace("'","\\'",get_field('duree',$post_id)))+0)."' WHERE id_c='".$aos_products_entry_id."'");
  3040.             ////////////////////////////////////////////////////
  3041.             }
  3042.              **/
  3043.  
  3044.         }
  3045.     }
  3046. }
  3047.  
  3048.  
  3049.  
  3050.  
  3051.  
  3052.  
  3053.  
  3054.  
  3055.  
  3056.  
  3057.  
  3058.  
  3059. /////////////////////////////////////////////////////////////////////////////////////
  3060. //PUSH PRODUCTS AND CATEGORIES TO CRM
  3061. //add_action( 'wp_loaded', 'Init_All_CRM' );
  3062. function Init_All_CRM() {
  3063. /////////////////////////////////////////////////////////////////////////////////////
  3064. //CONNECT TO SUITE CRM BDD
  3065.     $db_host="127.0.0.1"; $db_user="root"; $db_pass="root"; $db_db="suitecrm";
  3066.     $con=mysqli_connect($db_host, $db_user,$db_pass,$db_db) or die("erreur de connexion au serveur");
  3067.     if (mysqli_connect_errno()) { printf("Échec de la connexion : %s\n", mysqli_connect_error()); exit(); }
  3068.     mysqli_query($con,"SET NAMES UTF8");
  3069. /////////////////////////////////////////////////////////////////////////////////////
  3070. //RAZ SUITE CRM
  3071.     $raz="TRUNCATE `accounts`;
  3072. TRUNCATE `accounts_audit`;
  3073. TRUNCATE `accounts_cstm`;
  3074. TRUNCATE `accounts_opportunities`;
  3075. TRUNCATE `alerts`;
  3076. TRUNCATE `aos_line_item_groups`;
  3077. TRUNCATE `aos_line_item_groups_audit`;
  3078. TRUNCATE `aos_products`;
  3079. TRUNCATE `aos_products_audit`;
  3080. TRUNCATE `aos_products_quotes`;
  3081. TRUNCATE `aos_products_quotes_audit`;
  3082. TRUNCATE `aos_product_categories`;
  3083. TRUNCATE `aos_quotes`;
  3084. TRUNCATE `aos_quotes_audit`;
  3085. TRUNCATE `calls`;
  3086. TRUNCATE `calls_users`;
  3087. TRUNCATE `opportunities`;
  3088. TRUNCATE `opportunities_audit`;
  3089. TRUNCATE `reminders`;
  3090. TRUNCATE `reminders_invitees`;
  3091. TRUNCATE `sugarfeed`;
  3092. TRUNCATE `aos_invoices`;
  3093. TRUNCATE `aod_index`;
  3094. TRUNCATE `aod_indexevent`;
  3095. TRUNCATE `aos_quotes_aos_invoices_c`;
  3096. TRUNCATE `opportunities_cstm`;
  3097. TRUNCATE `acl_actions`;
  3098. TRUNCATE `aod_index`;
  3099. TRUNCATE `job_queue`;
  3100. TRUNCATE `aos_products_cstm`;
  3101. TRUNCATE `relationships`;
  3102. TRUNCATE `aos_contracts_audit`;
  3103. TRUNCATE `emails_text`;
  3104. TRUNCATE `emails`;
  3105. TRUNCATE `notes`;
  3106. TRUNCATE `contacts_audit`;
  3107. TRUNCATE `sess_formateurs`;
  3108. TRUNCATE `sess_formateurs_cstm`;
  3109. TRUNCATE `contacts`;
  3110. TRUNCATE `accounts_contacts`;
  3111. TRUNCATE `contacts_cstm`;
  3112. TRUNCATE `sess_formateurs_meetings_c`;
  3113. TRUNCATE `aos_invoices_audit`;
  3114. TRUNCATE `opportunities_contacts`;
  3115. TRUNCATE `emails_beans`;
  3116. TRUNCATE `aos_invoices_cstm`;
  3117. TRUNCATE `meetings_contacts`;
  3118. TRUNCATE `opportunities_meetings_1_c`;
  3119. TRUNCATE `sess_formateurs_aos_products_c`;
  3120. TRUNCATE `opportunities_aos_products_1_c`;
  3121. TRUNCATE `aos_quotes_cstm`;
  3122. TRUNCATE `sess_formateurs_audit`;
  3123. TRUNCATE `meetings_cstm`;
  3124. TRUNCATE `meetings_users`;
  3125. TRUNCATE `meetings`;
  3126. TRUNCATE `documents_accounts`;
  3127. TRUNCATE `aos_quotes_os_contracts_c`;
  3128. TRUNCATE `opportunities_sess_formateurs_1_c`;
  3129. TRUNCATE `sess_formateurs_aos_contracts_c`;
  3130. TRUNCATE `opportunities_accounts_1_c`;
  3131. TRUNCATE `sess_formateurs_aos_products_1_c`;
  3132. TRUNCATE `opportunities_aos_invoices_1_c`;
  3133. TRUNCATE `aos_contracts`;
  3134. TRUNCATE `document_revisions`;
  3135. TRUNCATE `opportunities_fp_event_locations_1_c`;
  3136. TRUNCATE `documents`;
  3137. TRUNCATE `sess_formateurs_audit`;
  3138. TRUNCATE `aos_quotes_cstm`;
  3139. TRUNCATE `meetings_cstm`;
  3140. TRUNCATE `meetings_users`;
  3141. TRUNCATE `meetings`;";
  3142.     foreach(explode("\n",$raz) as $ras) { mysqli_query($con,$ras); }
  3143.     /////////////////////////////////////////////////////////////////////////////////////
  3144.     $url = "http://suitecrm.straformation.fr/service/v4_1/rest.php";
  3145.     $username = "admin";
  3146.     $password = "waRR10RP0wa";
  3147.     function call($method, $parameters, $url) {
  3148.         ob_start();
  3149.         $curl_request = curl_init();
  3150.         curl_setopt($curl_request, CURLOPT_URL, $url);
  3151.         curl_setopt($curl_request, CURLOPT_POST, 1);
  3152.         curl_setopt($curl_request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
  3153.         curl_setopt($curl_request, CURLOPT_HEADER, 1);
  3154.         curl_setopt($curl_request, CURLOPT_SSL_VERIFYPEER, 0);
  3155.         curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1);
  3156.         curl_setopt($curl_request, CURLOPT_FOLLOWLOCATION, 0);
  3157.         $jsonEncodedData = json_encode($parameters);
  3158.         $post = array(
  3159.             "method" => $method,
  3160.             "input_type" => "JSON",
  3161.             "response_type" => "JSON",
  3162.             "rest_data" => $jsonEncodedData
  3163.         );
  3164.         curl_setopt($curl_request, CURLOPT_POSTFIELDS, $post);
  3165.         $result = curl_exec($curl_request);
  3166.         curl_close($curl_request);
  3167.         $result = explode("\r\n\r\n", $result, 2);
  3168.         $response = json_decode($result[1]);
  3169.         ob_end_flush();
  3170.         return $response;
  3171.     }
  3172.     //////////////////////////////////////////////////////
  3173.     //login ---------------------------------------------
  3174.     $login_parameters = array(
  3175.         "user_auth" => array(
  3176.             "user_name" => $username,
  3177.             "password" => md5($password),
  3178.             "version" => "1"
  3179.         ),
  3180.         "application_name" => "Woocommerce",
  3181.         "name_value_list" => array(),
  3182.     );
  3183.     $login_result = call("login", $login_parameters, $url); $session_id = $login_result->id; //echo "<pre>".print_r($login_result,true)."</pre>";
  3184.     /////////////////////////////////////////////////////////////////////////////////////
  3185.     $args = array('post_type' => array('product'), 'posts_per_page' => '-1',);
  3186.     $query=new WP_Query($args);
  3187.     if($query->posts) {
  3188.         foreach($query->posts as $post) {
  3189.             $post_id=$post->ID;
  3190.             $the_product = new WC_Product($post_id);
  3191.             if($the_product->is_downloadable()) { continue; }
  3192.             //////////////////////////////////////////////////////
  3193.             //get WOO
  3194.             $terms = get_the_terms($post_id, 'product_cat');
  3195.             foreach ($terms as $term) { $product_cat=$term; break; }
  3196.             if ($product_cat->name!='') { $product_cat_title=str_replace("'","`",$product_cat->name); }
  3197.             //////////////////////////////////////////////////////
  3198.             $cost=get_field('cout',$post_id);
  3199.             $product_title=str_replace("'","`",$the_product->get_title());
  3200.             $product_sku=str_replace("'","`",$the_product->get_sku());
  3201.             $product_url=get_permalink($post_id);
  3202.             ///
  3203.             $aos_product_categories_entry_id=get_term_meta($product_cat->term_id, 'aos_product_categories_entry_id', true).'';
  3204.             $query_cat="aos_product_categories.id='".$aos_product_categories_entry_id."'";
  3205.             //////////////////////////////////////////////////////
  3206.             //get AOS_Product_Categories -------------------------------------
  3207.             $get_entry_list_parameters = array(
  3208.                 'session' => $session_id,
  3209.                 'module_name' => 'AOS_Product_Categories',
  3210.                 'query' => $query_cat,     //The SQL WHERE clause without the word "where".
  3211.                 'order_by' => "",                              //The SQL ORDER BY clause without the phrase "order by".
  3212.                 'offset' => '0',                               //The record offset from which to start.
  3213.                 'select_fields' => array(                      //Optional. A list of fields to include in the results.
  3214.                     'id',
  3215.                     'name',
  3216.                 ),
  3217.                 'link_name_to_fields_array' => array(),
  3218.                 'max_results' => '1',      //The maximum number of results to return.
  3219.                 'deleted' => '0',          //To exclude deleted records
  3220.                 'Favorites' => false,      //If only records marked as favorites should be returned.
  3221.             );
  3222.             $get_entry_list_result_aos_product_categories = call('get_entry_list', $get_entry_list_parameters, $url);
  3223.             $aos_product_categories_entry_id = $get_entry_list_result_aos_product_categories->entry_list[0]->id.'';
  3224. //echo "<pre>AOS_Product_Categories LOAD : ".print_r($aos_product_categories_entry_id,true)."</pre>"; //echo "<pre style='font-size:9px;'>".print_r($get_entry_list_result_aos_product_categories,true)."</pre>";
  3225.             //////////////////////////////////////////////////////
  3226.             //create or update AOS_Product_Categories -------------------------------------
  3227.             if($aos_product_categories_entry_id=='' && $product_cat_title!='') {
  3228.                 $set_entry_parameters = array(
  3229.                     "session" => $session_id,
  3230.                     "module_name" => "AOS_Product_Categories",
  3231.                     "name_value_list" => array(
  3232.                         array("name" => "assigned_user_name", "value" => 'Administrator'),
  3233.                         array("name" => "assigned_user_id", "value" => '1'),
  3234.                         array("name" => "id", "value" => $aos_product_categories_entry_id), // TO UPDATE
  3235.                         array("name" => "name", "value" => $product_cat_title),
  3236.                         array("name" => "is_parent", "value" => "1"),
  3237.                     ),
  3238.                 );
  3239.                 $set_entry_result_aos_product_categories = call("set_entry", $set_entry_parameters, $url);
  3240.                 $aos_product_categories_entry_id = $set_entry_result_aos_product_categories->id;
  3241.             }
  3242.             update_term_meta($product_cat->term_id, 'aos_product_categories_entry_id', $aos_product_categories_entry_id);
  3243. //echo "<pre>AOS_Product_Categories CREATE OR UPDATE : ".print_r($aos_product_categories_entry_id,true)."</pre>"; //echo "<pre style='font-size:9px;'>".print_r($set_entry_result_aos_product_categories,true)."</pre>";
  3244.             ///
  3245.             $aos_products_entry_id=get_post_meta($post_id, 'aos_products_entry_id',true).'';
  3246.             $query_produit="aos_products.id='".$aos_products_entry_id."'";
  3247.             //////////////////////////////////////////////////////
  3248.             //get AOS_Products -------------------------------------
  3249.             $get_entry_list_parameters = array(
  3250.                 'session' => $session_id,
  3251.                 'module_name' => 'AOS_Products',
  3252.                 'query' => $query_produit,     //The SQL WHERE clause without the word "where".
  3253.                 'order_by' => "",                              //The SQL ORDER BY clause without the phrase "order by".
  3254.                 'offset' => '0',                               //The record offset from which to start.
  3255.                 'select_fields' => array(                      //Optional. A list of fields to include in the results.
  3256.                     'id',
  3257.                     'name',
  3258.                 ),
  3259.                 'link_name_to_fields_array' => array(),
  3260.                 'max_results' => '1',      //The maximum number of results to return.
  3261.                 'deleted' => '0',          //To exclude deleted records
  3262.                 'Favorites' => false,      //If only records marked as favorites should be returned.
  3263.             );
  3264.             $get_entry_list_result_aos_products = call('get_entry_list', $get_entry_list_parameters, $url);
  3265.             $aos_products_entry_id = $get_entry_list_result_aos_products->entry_list[0]->id.'';
  3266. //echo "<pre>AOS_Products LOAD : ".print_r($aos_products_entry_id,true)."</pre>"; //echo "<pre style='font-size:9px;'>".print_r($get_entry_list_result_aos_products,true)."</pre>";
  3267.             //////////////////////////////////////////////////////
  3268.             //create or update AOS_Products -------------------------------------
  3269.             if(true || $aos_products_entry_id=='') {
  3270.                 $set_entry_parameters = array(
  3271.                     "session" => $session_id,
  3272.                     "module_name" => "AOS_Products",
  3273.                     "name_value_list" => array(
  3274.                         array("name" => "assigned_user_name", "value" => 'Administrator'),
  3275.                         array("name" => "assigned_user_id", "value" => '1'),
  3276.                         array("name" => "id", "value" => $aos_products_entry_id), // TO UPDATE
  3277.                         array("name" => "currency_id", "value" => "-99"),
  3278.                         array("name" => "name", "value" => $product_title),
  3279.                         array("name" => "cost", "value" => $cost),
  3280.                         array("name" => "cost_usdollar", "value" => $cost),
  3281.                         array("name" => "description", "value" => $product_title),
  3282.                         array("name" => "maincode", "value" => "XXXX"),
  3283.                         array("name" => "part_number", "value" => $product_sku),
  3284.                         array("name" => "type", "value" => "Service"),
  3285.                         array("name" => "currency_id", "value" => "-99"),
  3286.                         array("name" => "price", "value" => $product_price),
  3287.                         array("name" => "price_usdollar", "value" => $product_price),
  3288.                         array("name" => "url", "value" => $product_url),
  3289.                         array("name" => "aos_product_category_id", "value" => $aos_product_categories_entry_id),
  3290.                         array("name" => "duree_c", "value" => (strip_tags(get_field('duree',$post_id))+0) ),
  3291.                         array("name" => "public_concerne_c", "value" => get_field('public_concerne',$post_id) ),
  3292.                         array("name" => "pre_requis_c", "value" => get_field('pre-requis',$post_id) ),
  3293.                         array("name" => "but_de_la_formation_c", "value" => get_field('but_de_la_formation',$post_id) ),
  3294.                         array("name" => "programme_c", "value" => get_field('programme',$post_id) ),
  3295.                         array("name" => "methodes_pedagogiques_c", "value" => get_field('methodes_pedagogiques',$post_id) ),
  3296.                         array("name" => "moyens_pedagogiques_c", "value" => get_field('moyens_pedagogiques',$post_id) ),
  3297.                         array("name" => "equipements_necessaires_c", "value" => get_field('equipements_necessaires',$post_id) ),
  3298.                         array("name" => "documents_necessaires_c", "value" => get_field('documents_necessaires',$post_id) ),
  3299.                         array("name" => "fin_de_la_formation_c", "value" => get_field('fin_de_la_formation',$post_id) ),
  3300.                     ),
  3301.                 );
  3302.                 $set_entry_result_aos_products = call("set_entry", $set_entry_parameters, $url);
  3303.                 $aos_products_entry_id = $set_entry_result_aos_products->id;
  3304.             }
  3305.             update_post_meta($post_id, 'aos_products_entry_id',$aos_products_entry_id);
  3306. //echo "<pre>AOS_Products CREATE OR UPDATE : ".print_r($aos_products_entry_id,true)."</pre>"; //echo "<pre style='font-size:9px;'>".print_r($set_entry_result_aos_products,true)."</pre>";
  3307.         }
  3308.     }
  3309. }
  3310.  
  3311.  
  3312.  
  3313.  
  3314.  
  3315.  
  3316.  
  3317.  
  3318.  
  3319.  
  3320.  
  3321.  
  3322.  
  3323.  
  3324.  
  3325.  
  3326.  
  3327.  
  3328.  
  3329.  
  3330.  
  3331.  
  3332.  
  3333.  
  3334.  
  3335.  
  3336. function woo_get_customer_by_billing_email( $email ) {
  3337.     global $wpdb;
  3338.     $customer_id = $wpdb->get_var( $wpdb->prepare( "
  3339.         SELECT user_id
  3340.         FROM $wpdb->usermeta
  3341.         WHERE meta_key = 'billing_email'
  3342.         AND meta_value = '%s'
  3343.     ", $email ) );
  3344.     if ( ! $customer_id ) { return false; }
  3345.     $customer = new WC_Customer( $customer_id );
  3346.     if ( ! $customer ) { return false; }
  3347.     return $customer;
  3348. }
  3349.  
  3350.  
  3351. function iconic_remove_password_strength() {
  3352.     wp_dequeue_script( 'wc-password-strength-meter' );
  3353. }
  3354. add_action( 'wp_print_scripts', 'iconic_remove_password_strength', 10 );
  3355.  
  3356.  
  3357.  
  3358.  
  3359.  
  3360. include('functions_email.php');
  3361. include('functions_crm.php');
  3362. ?>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top