Last active
May 1, 2017 09:10
-
-
Save psaikali/970c87ed2f67ff4891ee4bfe04191506 to your computer and use it in GitHub Desktop.
Créer un formulaire de proposition de produit WooCommerce
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Shortcode pour afficher le formulaire pour proposer un produit | |
*/ | |
add_shortcode('msk_formulaire_proposer_produit', 'msk_shortcode_product_submission'); | |
function msk_shortcode_product_submission($atts) { | |
// On ajoute un champ caché pour savoir si c'est un utilisateur lambda ou l'admin qui remplit le formulaire | |
$is_admin_hidden_field = (is_user_logged_in() && current_user_can('manage_options')) ? 'on' : 'off'; | |
// Valeurs par défaut des champs | |
$form_values_default = array( | |
'product-title' => '', | |
'product-description' => '' | |
); | |
// On boucle pour nettoyer les valeurs, si elles sont renvoyées par le système en cas d'erreur | |
$form_values = array_map('sanitize_text_field', wp_parse_args($_GET, $form_values_default)); | |
$errors = msk_get_current_errors($_GET); | |
ob_start(); ?> | |
<form id="form-submit-product" class="row" enctype="multipart/form-data" method="post" action="#"> | |
<?php msk_display_errors($errors); ?> | |
<section class="data"> | |
<fieldset class="affiliate-data"> | |
<?php if (!is_user_logged_in()) { ?> | |
<p> | |
<a href="<?php echo get_permalink(get_option('woocommerce_myaccount_page_id')); ?>"><?php _e('Identifiez-vous ou créez un compte afin de gagner des points à chaque vente d\'un de vos produits proposés.'); ?></a> | |
</p> | |
<?php } else { $user_data = get_userdata(get_current_user_id()); ?> | |
<p> | |
<?php printf( | |
__('Vous êtes connecté en tant que %1$s (e-mail %2$s) : vos points seront reversés sur ce compte parrain.', 'mosaika'), | |
'<strong>' . $user_data->user_login . '</strong>', | |
'<strong>' . $user_data->user_email . '</strong>' | |
); ?> | |
</p> | |
<?php } ?> | |
</fieldset> | |
<?php if (is_user_logged_in()) { ?> | |
<fieldset class="product-data"> | |
<label for="product-title"><?php _e('Nom du produit', 'mosaika'); ?><span class="required">*</span></label> | |
<input type="text" required id="product-title" name="product-title" placeholder="<?php esc_attr_e('Nom du produit', 'mosaika'); ?>" value="<?php esc_attr_e($form_values['product-title']); ?>" /> | |
<label for="product-description"><?php _e('Description du produit', 'mosaika'); ?><span class="required">*</span></label> | |
<textarea id="product-description" name="product-description" placeholder="<?php esc_attr_e('Description du produit', 'mosaika'); ?>" rows="7"><?php echo esc_textarea($form_values['product-description']); ?></textarea> | |
<p class="required-text"><span class="required">*</span><?php _e('Champs obligatoires'); ?></p> | |
</fieldset> | |
<fieldset class="photos"> | |
<input type="file" id="product-photo" name="product-photo[]" accept="image/jpeg, image/jpg, image/png" multiple class="jfilestyle" data-buttonText="<i class='fa fa-camera'></i> Ajouter une photo" /> | |
</fieldset> | |
<fieldset class="footer"> | |
<?php wp_nonce_field('msk_submit_product'); ?> | |
<input type="hidden" name="is-admin" value="<?php echo $is_admin_hidden_field; ?>" /> | |
<button class="button" type="submit" name="submit" value="submit-product"><?php _e('Proposer un produit', 'mosaika'); ?></button> | |
</fieldset> | |
<?php } ?> | |
</section> | |
</form> | |
<?php return ob_get_clean(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* On intercepte les données lorsque le formulaire de proposition d'un produit est soumis par un utilisateur | |
*/ | |
function msk_process_product_submission() { | |
if (isset($_POST['submit']) && $_POST['submit'] == 'submit-product') { | |
check_admin_referer('msk_submit_product'); | |
$data = (!empty($_POST)) ? $_POST : array(); | |
$data['errors'] = array(); | |
$data = apply_filters('msk_do_product_submission', $data); | |
} | |
} | |
add_action('template_redirect', 'msk_process_product_submission'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* On valide les données et on prépare un nouveau tableau mieux organisé pour la suite | |
*/ | |
function msk_preprocess_data_for_product_submission($data) { | |
$validation_rules = array( | |
'product-title' => array('required'), | |
'is-admin' => array('required', 'is_admin') | |
); | |
// On vérifie les données selon des règles : si il y a des erreurs, on les aura dans $errors | |
$errors = msk_validate_data($data, $validation_rules); | |
// Si l'utilisateur n'est pas identifié, on ajoute une erreur | |
if (!is_user_logged_in()) $errors[] = 'user:not_logged_in'; | |
if (empty($errors)) { | |
// On prépare un nouveau tableau de données, plus organisé | |
$new_data['product'] = array( | |
'title' => sanitize_text_field($data['product-title']), | |
'content' => sanitize_text_field($data['product-description']), | |
'product_meta' => array() | |
); | |
// On prépare la structure des metadonnées du produit | |
$new_data['product']['product_meta']['user_submitted'] = 'on'; | |
// ... l'ID du parrain | |
$new_data['product']['product_meta']['commission_user_id'] = get_current_user_id(); | |
// ... le taux de commission par défaut | |
$new_data['product']['product_meta']['commission_rate'] = 5; | |
// ... le début de la validité de commission | |
$new_data['product']['product_meta']['commission_date_start'] = date('Y-m-d', strtotime('now')); | |
// ... la fin de validité de commission | |
$new_data['product']['product_meta']['commission_date_end'] = date('Y-m-d', strtotime('+6 months')); | |
$data = $new_data; | |
} | |
$data['errors'] = $errors; | |
return $data; | |
} | |
add_filter('msk_do_product_submission', 'msk_preprocess_data_for_product_submission', 10, 1); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* On créée un produit WooCommerce au statut 'brouillon' | |
*/ | |
function msk_create_product_for_product_submision($data) { | |
if (empty($data['errors']) && array_key_exists('product', $data)) { | |
// On créée le produit | |
$product = new WC_Product; | |
$product->set_name($data['product']['title']); | |
$product->set_description($data['product']['content']); | |
$product->set_status('pending'); | |
$product->save(); | |
// L'ancienne méthode... | |
/*$product_id = wp_insert_post( | |
array( | |
'post_type' => 'product', | |
'post_content' => $data['product']['content'], | |
'post_title' => $data['product']['title'], | |
'post_status' => 'pending', | |
'post_author' => (current_user_can('manage_options')) ? get_current_user_id() : 1, | |
) | |
);*/ | |
$product_id = $product->get_id(); | |
if (0 >= $product_id) { | |
// Erreur dans création du produit | |
$data['errors'][] = 'cant_create_product'; | |
//var_dump($product_id->get_error_message()); | |
} else { | |
// Produit bien créé | |
$data['product']['ID'] = $product_id; | |
// On enregistre les metadonnées du produit | |
if ($product && is_array($data['product']['product_meta'])) { | |
foreach ($data['product']['product_meta'] as $meta_key => $meta_value) { | |
$product->update_meta_data($meta_key, $meta_value); | |
} | |
$product->save(); | |
} | |
} | |
} | |
return $data; | |
} | |
add_filter('msk_do_product_submission', 'msk_create_product_for_product_submision', 20, 1); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* On redirige vers la page du formulaire | |
*/ | |
function msk_redirect_after_product_submission($data) { | |
if (empty($data['errors'])) { | |
// Si pas d'erreur, on redirige vers la page précédente avec ?notice=product_submitted dans l'URL | |
$redirect_url = add_query_arg( | |
array( | |
'notice' => 'product_submitted' | |
), | |
remove_query_arg(array('product-title', 'product-description', 'is-admin', '_wpnonce', 'errors', 'notice'), wp_get_referer()) | |
); | |
} else { | |
// Sinon, on redirige avec ?errors=... dans l'URL | |
unset($data['submit']); | |
unset($data['_wp_http_referer']); | |
$data = array_map('urlencode', array_merge($data, array('errors' => multi_implode(',', $data['errors'])))); | |
$redirect_url = add_query_arg( | |
$data, | |
wp_get_referer() | |
); | |
} | |
wp_redirect($redirect_url); | |
exit; | |
} | |
add_filter('msk_do_product_submission', 'msk_redirect_after_product_submission', 60, 1); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Un peu plus tard : lorsque l'admin publie le produit, on envoie un e-mail au parrain | |
*/ | |
function msk_notify_parrain_when_product_is_published($post_id) { | |
$product = wc_get_product($post_id); | |
$user_id = $product->get_meta('commission_user_id', true); | |
$user_data = get_userdata($user_id); | |
if ($user_data) { | |
$user_login = $user_data->user_login; | |
$user_email = $user_data->user_email; | |
$product_title = $product->get_title(); | |
$product_url = get_permalink($post_id); | |
$subject = 'Votre produit est en vente dans notre boutique !'; | |
$body = sprintf( | |
__('Bonjour %1$s, votre produit %2$s est en vente sur <a href="%3$s">%3$s</a>.', 'mosaika'), | |
$user_login, | |
$product_title, | |
$product_url | |
); | |
wp_mail($user_email, $subject, $body, array('Content-Type: text/html; charset=UTF-8')); | |
} | |
} | |
add_action('publish_product', 'msk_notify_parrain_when_product_is_published', 10, 1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment