/**
* xmoze theme functions and definitions
*/
// File Security Check
if (!defined('ABSPATH')) {
exit;
}
/**
* xmoze functions and definitions
*
* @link https://developer.wordpress.org/themes/basics/theme-functions/
*
* @package xmoze
*/
if (!function_exists('xmoze_setup')) :
/**
* Sets up theme defaults and registers support for various WordPress features.
*
* Note that this function is hooked into the after_setup_theme hook, which
* runs before the init hook. The init hook is too late for some features, such
* as indicating support for post thumbnails.
*/
function xmoze_setup()
{
/*
* Make theme available for translation.
* Translations can be filed in the /languages/ directory.
* If you're building a theme based on xmoze, use a find and replace
* to change 'xmoze' to the name of your theme in all the template files.
*/
load_theme_textdomain('xmoze', get_template_directory() . '/languages');
// Add default posts and comments RSS feed links to head.
add_theme_support('automatic-feed-links');
/*
* Let WordPress manage the document title.
* By adding theme support, we declare that this theme does not use a
* hard-coded
tag in the document head, and expect WordPress to
* provide it for us.
*/
add_theme_support('title-tag');
/*
* Enable support for Post Thumbnails on posts and pages.
*
* @link https://developer.wordpress.org/themes/functionality/featured-images-post-thumbnails/
*/
add_theme_support('post-thumbnails');
// This theme uses wp_nav_menu() in one location.
register_nav_menus(
array(
'main-menu' => esc_html__('Main Menu', 'xmoze'),
)
);
/*
* Switch default core markup for search form, comment form, and comments
* to output valid HTML5.
*/
add_theme_support(
'html5',
array(
'search-form',
'comment-form',
'comment-list',
'gallery',
'caption',
'style',
'script',
)
);
// Set up the WordPress core custom background feature.
$args = apply_filters(
'xmoze_custom_background_args',
array(
'default-color' => 'ffffff',
'default-image' => '',
)
);
add_theme_support('custom-background', $args);
// Add theme support for selective refresh for widgets.
add_theme_support('customize-selective-refresh-widgets');
/**
* Add support for core custom logo.
*
* @link https://codex.wordpress.org/Theme_Logo
*/
add_theme_support(
'custom-logo',
array(
'height' => 250,
'width' => 250,
'flex-width' => true,
'flex-height' => true,
)
);
}
endif;
add_action('after_setup_theme', 'xmoze_setup');
/**
* Set the content width in pixels, based on the theme's design and stylesheet.
*
* Priority 0 to make it available to lower priority callbacks.
*
* @global int $content_width
*/
function xmoze_content_width()
{
// This variable is intended to be overruled from themes.
// Open WPCS issue: {@link https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/issues/1043}.
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
$GLOBALS['content_width'] = apply_filters('xmoze_content_width', 1200);
}
add_action('after_setup_theme', 'xmoze_content_width', 0);
/**
*
* Intialize xmoze
*
*/
require get_parent_theme_file_path('/inc/init.php');
function mon_traitement_apres_commande($order_id)
{
// Récupérer l'objet de la commande
$order = wc_get_order($order_id);
$create = get_field('cannards_crees', $order_id);
if (!$create) {
$first_name = $order->get_billing_first_name();
$last_name = $order->get_billing_last_name();
$email = $order->get_billing_email();
// Récupérer les articles de la commande
$items = $order->get_items();
// Obtenir le numéro de canard actuel
$duck_number = get_option('last_duck_number', 0) + 1;
// Parcourir chaque article de la commande
foreach ($items as $item_id => $item_data) {
// Récupérer l'ID du produit
$product_id = $item_data->get_product_id();
// Vérifier si l'ID du produit est 11366
if ($product_id == 11366) {
// Récupérer la quantité commandée du produit
$product_quantity = $item_data->get_quantity();
// Créer des posts de type "canard"
for ($i = 0; $i < $product_quantity; $i++) {
// Générer un nom de post unique avec le numéro de canard
if($duck_number > 20000 && $duck_number < 25001){
$duck_number = 25001;
}
$post_name = $duck_number;
// Créer le post de type "canard" en tant que post privé
$post_id = wp_insert_post(array(
'post_title' => $post_name,
'post_name' => $post_name,
'post_type' => 'canard',
'post_status' => 'private' // Définir le statut comme "privé"
));
// Attribuer la commande liée au champ ACF "commande_liee"
update_field('commande_liee', $order_id, $post_id);
update_field('prenom_client_canard', $first_name, $post_id);
update_field('nom_client_canard', $last_name, $post_id);
update_field('email_client_canard', $email, $post_id);
// Incrémenter le numéro de canard pour le prochain
$duck_number++;
}
//verrou pour dire les canards sont créés
update_field('cannards_crees', 1, $order_id);
}
}
// Mettre à jour le numéro de canard dans les options
update_option('last_duck_number', $duck_number - 1);
}
}
add_action('woocommerce_checkout_order_processed', 'mon_traitement_apres_commande');
function mon_traitement_apres_commande_TEST() {
global $wpdb;
$duck_number = get_option('last_duck_number', 0) + 1;
var_dump($duck_number);
// Tableau des statuts de commande à récupérer
$statuses = array('wc-completed', 'wc-processing', 'wc-on-hold', 'wc-pending');
// Requête SQL pour récupérer les ID des commandes
$requete_orders = $wpdb->prepare("
SELECT ID
FROM {$wpdb->posts}
WHERE post_type = 'shop_order'
AND post_status IN ('" . implode("', '", $statuses) . "')
");
// Exécution de la requête pour récupérer les ID des commandes
$order_ids = $wpdb->get_col($requete_orders);
// Vérifier si des commandes ont été trouvées
if ($order_ids) {
// Parcourir chaque ID de commande
foreach ($order_ids as $order_id) {
//canards créés
$table_postmeta = $wpdb->prefix . 'postmeta';
$meta_value = $order_id;
$requete = $wpdb->prepare("
SELECT COUNT(*)
FROM $table_postmeta
WHERE meta_key LIKE 'commande_liee'
AND meta_value LIKE %s
ORDER BY meta_id DESC
", $meta_value);
$countCanards = (int)($wpdb->get_var($requete));
//nb de canards dans la commande
$requeteOrder = $wpdb->prepare("SELECT SUM(product_qty) FROM `wpji_wc_order_product_lookup` WHERE `order_id` = %d", $meta_value);
$countItems = (int)($wpdb->get_var($requeteOrder));
if ($countCanards != $countItems) {
echo('la commande ' . $order_id . ' contient ' . $countItems . ' canards dans la commande mais ' . $countCanards . ' ont été créés. ');
}
if($countCanards == 0){
}
}
} else {
echo "Aucune commande trouvée.";
}
}
add_shortcode('mon_shortcode_test', 'mon_traitement_apres_commande_TEST');
/************************** colonnes admin pour canards *********************************/
// Add the custom columns to the commande post type:
add_filter('manage_canard_posts_columns', 'set_custom_edit_canard_columns');
function set_custom_edit_canard_columns($columns)
{
$columns['commande'] = 'Commande';
return $columns;
}
// Add the data to the custom columns for the annonce post type:
add_action('manage_canard_posts_custom_column', 'custom_canard_column', 10, 2);
function custom_canard_column($column, $post_id)
{
$commandeliee = get_field('commande_liee', $post_id);
switch ($column) {
case 'commande' :
echo($commandeliee);
break;
}
}
add_action('pre_get_posts', 'extend_admin_search_canard');
function extend_admin_search_canard($query)
{
$post_type = 'canard';
$custom_fields = array("commande_liee");
if (!is_admin())
return;
if ($query->query['post_type'] != $post_type)
return;
$search_term = $query->query_vars['s'];
$query->query_vars['s'] = '';
if ($search_term != '') {
$meta_query = array('relation' => 'OR');
foreach ($custom_fields as $custom_field) {
array_push($meta_query, array(
'key' => $custom_field,
'value' => $search_term,
'compare' => 'LIKE'
));
}
$query->set('meta_query', $meta_query);
};
}