Skip to content

Instantly share code, notes, and snippets.

View willybahuaud's full-sized avatar

Willy Bahuaud willybahuaud

View GitHub Profile
#Il faut penser à remplacer key par sa clef (moi je ne la montre pas à tout le monde)
#je savais pas que pour faire un curl sur une url de plus d'un parametre il fallait des guillemets...
#curl -L -v 'http://connect.advancedcustomfields.com/index.php?p=pro&a=download&k=KEY' > acfpro.zip
curl -L -v 'http://connect.advancedcustomfields.com/index.php?p=pro&a=download&k=KEY' | tar -xf- -C /path/to/plugins
@willybahuaud
willybahuaud / default-term.php
Last active October 17, 2022 16:45
Set a default term for custom taxonomy and custom post type
<?php
add_action( 'wp_insert_post', 'willy_set_default_term', 10, 3 );
function willy_set_default_term( $post_id, $post, $update ) {
if ( 'cpt' == $post->post_type ) { // replace `cpt` with your custom post type slug
/**
* Replace `taxo` by the taxonomy slug you want to control/set
* … and replace `default-term` by the default term slug (or name)
* (or you can use a `get_option( 'my-default-term', 'default term' )` option instead, which is much better)
*/
if ( empty( wp_get_post_terms( $post_id, 'taxo' ) ) ) {
@willybahuaud
willybahuaud / auto-apply-coupons.php
Last active February 9, 2022 10:06
Faire des coupons Woocommerce auto-applicables
<?php
add_action( 'woocommerce_coupon_options', 'w_coupons_options', 10, 2 );
function w_coupons_options( $id, $coupon ) {
woocommerce_wp_checkbox(
array(
'id' => 'auto_applied',
'label' => __( 'Activé automatiquement', 'woocommerce' ),
'description' => 'Cocher cette case pour ajouter automatiquement le coupon s’il est disponible pour le panier',
'value' => wc_bool_to_string( get_post_meta( $id, 'auto_applied', true ) ),
@willybahuaud
willybahuaud / script.js
Last active February 1, 2022 08:37
Synchroniser le stock entre plusieurs produits WooCommerce
jQuery( document ).ready(function($){
$sync = $('.sync-stock-select');
$sync.select2({
minimumInputLength:1,
placeholder: 'Chercher un produit…',
ajax: {
url: '/wp-json/wp/v2/product',
dataType: 'json',
method:'GET',
delay: 250,
@willybahuaud
willybahuaud / functions.php
Created January 29, 2022 00:20
Ajouter un produit gratuit dans chaque commande Woocommerce (après paiement)
<?php
add_filter( 'woocommerce_get_settings_products', 'woo_settings_free_products' );
function woo_settings_free_products( $settings ) {
$settings[] = array(
'name' => __( 'Produits gratuits', 'woocommerce' ),
'type' => 'title',
'id' => 'free_products'
);
$settings[] = array(
@willybahuaud
willybahuaud / function.php
Created January 29, 2022 00:15
Des boutons de quantité synchronisés en ajax avec le panier WooCommerce
<?php
add_action( 'woocommerce_after_shop_loop_item', 'custom_add_to_cart' );
function custom_add_to_cart() {
global $product;
vprintf( '<div class="add-to-cart-block">
<div class="add-to-cart-block__price">%s</div>
<hr/>
%s
</div>',
@willybahuaud
willybahuaud / mail-acf-fields.php
Created January 29, 2022 00:03
Constructeur de module d’emails dont les contenus sont administrables dans ACF
<?php
add_action( 'admin_menu', 'register_option_page', 10 );
function register_option_page() {
if( function_exists( 'acf_add_options_page' ) ) {
acf_add_options_page( array(
'page_title' => 'Réglages des emails',
'menu_title' => 'Réglages',
'menu_slug' => 'emails-settings',
'capability' => 'manage_options',
'redirect' => false,
@willybahuaud
willybahuaud / filtre-order.php
Created January 28, 2022 23:38
Order post by categories id
<?php
// On tri par par ordre des termes dans la tax_query
add_action( 'pre_get_posts', 'w_filter_query' );
function w_filter_query( $q ) {
if ( ! is_admin() && 'product' == $q->get('post_type') && is_post_type_archive( 'product' ) ) {
$q->set( 'need_special_order', 'tq_order' );
}
}
add_filter('posts_orderby', 'edit_posts_orderby', 100, 2);
@willybahuaud
willybahuaud / imagick-bg-white-pdf.php
Created January 12, 2022 00:10
avoir des arrières plans blancs sur les previews de PDF générés automatiquement
<?php
require_once ABSPATH . 'wp-includes/class-wp-image-editor.php';
require_once ABSPATH . 'wp-includes/class-wp-image-editor-imagick.php';
final class ExtendedWpImageEditorImagick extends \WP_Image_Editor_Imagick
{
protected function pdf_load_source()
{
$loaded = parent::pdf_load_source();
@willybahuaud
willybahuaud / shipping-warranty-fees.php
Last active January 11, 2022 22:56
add shipping warranty fees
<?php
add_action( 'woocommerce_review_order_after_shipping', 'shipping_warranty_cb' );
function shipping_warranty_cb() {
?><tr class="shipping-fee">
<th><?php _e( 'Assurance Expédition' ); ?></th>
<td><?php
woocommerce_form_field( 'shipping_warranty', array(
'type' => 'checkbox',
'class' => array('checkbox_field'),