Last active
February 1, 2022 08:37
-
-
Save willybahuaud/9561d114af4bc1b5ebe9c424f1e91928 to your computer and use it in GitHub Desktop.
Synchroniser le stock entre plusieurs produits 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
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, | |
data: function (params) { | |
var query = { | |
search: params.term, | |
} | |
return query; | |
}, | |
processResults: function (data) { | |
var res = data.map(function(r) { | |
return { | |
id:r.id, | |
text: `${r.title.rendered} (id:${r.id})` | |
}; | |
}); | |
return { | |
results: res | |
}; | |
} | |
} | |
}); | |
}); |
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 | |
add_action( 'init', 'w_load_acf_fields', 9 ); | |
function w_load_acf_fields() { | |
if( function_exists('acf_add_local_field_group') ): | |
acf_add_local_field_group(array( | |
'key' => 'group_5fa29f12476fd', | |
'title' => 'Synchronisation de stock', | |
'fields' => array( | |
array( | |
'key' => 'field_5fa29f25937ba', | |
'label' => 'Le stock de ce produit doit être synchronisé avec celui-ci', | |
'name' => 'synchro_stock_with', | |
'type' => 'post_object', | |
'instructions' => '', | |
'required' => 0, | |
'conditional_logic' => 0, | |
'wrapper' => array( | |
'width' => '', | |
'class' => '', | |
'id' => '', | |
), | |
'acfe_permissions' => array( | |
0 => 'administrator', | |
1 => 'editor', | |
2 => 'shop_manager', | |
), | |
'post_type' => array( | |
0 => 'product', | |
), | |
'taxonomy' => '', | |
'allow_null' => 1, | |
'multiple' => 1, | |
'return_format' => 'id', | |
'save_custom' => 0, | |
'save_post_status' => 'publish', | |
'acfe_bidirectional' => array( | |
'acfe_bidirectional_enabled' => '1', | |
'acfe_bidirectional_related' => array( | |
0 => 'field_5fa29f25937ba', | |
), | |
), | |
'ui' => 1, | |
), | |
), | |
'location' => array( | |
array( | |
array( | |
'param' => 'post_type', | |
'operator' => '==', | |
'value' => 'product', | |
), | |
), | |
), | |
'menu_order' => 0, | |
'position' => 'normal', | |
'style' => 'default', | |
'label_placement' => 'top', | |
'instruction_placement' => 'label', | |
'hide_on_screen' => '', | |
'active' => true, | |
'description' => '', | |
'acfe_display_title' => '', | |
'acfe_autosync' => '', | |
'acfe_permissions' => '', | |
'acfe_form' => 0, | |
'acfe_meta' => '', | |
'acfe_note' => '', | |
)); | |
endif; | |
} |
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 | |
add_action( 'woocommerce_product_set_stock', 'w_sync_stock' ); | |
function w_sync_stock( $product ) { | |
remove_action( 'woocommerce_product_set_stock', 'w_sync_stock' ); | |
$synced_products = get_post_meta( $product->get_id(), 'synchro_stock_with', true ); | |
foreach ( $synced_products as $synced_product ) { | |
$other = wc_get_product( $synced_product ); | |
$other->set_stock_quantity( $product->get_stock_quantity() ); | |
$other->save(); | |
} | |
add_action( 'woocommerce_product_set_stock', 'w_sync_stock' ); | |
} | |
add_action('woocommerce_variation_set_stock', 'w_sync_variation_stock' ); | |
function w_sync_variation_stock( $variation ) { | |
remove_action( 'woocommerce_variation_set_stock', 'w_sync_variation_stock' ); | |
$parent = $variation->get_parent_id(); | |
$synced_products = get_post_meta( $parent, 'synchro_stock_with', true ); | |
foreach ( $synced_products as $synced_product ) { | |
$atts = $variation->get_variation_attributes(); | |
$parent = wc_get_product( $synced_product ); | |
$synced_var = $parent->get_available_variations(); | |
$same = wp_list_filter( $synced_var, array( 'attributes' => $atts ) ); | |
if ( ! empty( $same ) ) { | |
$target = reset( $same ); | |
$target = $target['variation_id']; | |
$other = wc_get_product( $target ); | |
$other->set_stock_quantity( $variation->get_stock_quantity() ); | |
$other->save(); | |
} | |
} | |
add_action( 'woocommerce_variation_set_stock', 'w_sync_variation_stock' ); | |
} |
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 | |
add_action( 'add_meta_boxes_product', 'register_sync_metabox' ); | |
function register_sync_metabox() { | |
add_meta_box( 'sync_metabox', 'Synchronisation du stock', 'sync_metabox', 'product', 'side', 'default'); | |
} | |
add_action( 'admin_enqueue_scripts', 'my_admin_scripts_method' ); | |
function my_admin_scripts_method() { | |
wp_register_script( 'sync-script', plugins_url( 'script.js', __FILE__ ), 'selectwoo' ); | |
} | |
function sync_metabox( $post ) { | |
wp_nonce_field( 'update-synced_p_' . $post->ID, '_wpnonce_update_synced_p' ); | |
wp_enqueue_script( 'sync-script'); | |
?> | |
<p> | |
<label>Sélectionnez les produits avec lesquels vous souhaitez syncroniser le stock :</label><br> | |
<input type="hidden" name="synchro_stock_with" value="" /> | |
<select name="synchro_stock_with[]" class="sync-stock-select" style="width:100%" multiple="multiple"> | |
<?php | |
$products_synced = get_post_meta( $post->ID,'synchro_stock_with', true ); | |
if ( ! empty( $products_synced ) ) { | |
foreach ( $products_synced as $id ) { | |
$titre_produit = get_the_title( $id ) . " (id:{$id})"; | |
printf( '<option value="%1$s" selected>%2$s</option>', $id, $titre_produit ); | |
} | |
} | |
?> | |
</select> | |
</p> | |
<?php | |
} | |
add_action('save_post','sauvegarde_metabox'); | |
function sauvegarde_metabox( $post_id ){ | |
if ( ! wp_doing_ajax() && isset($_POST['synchro_stock_with'] ) ) { | |
check_admin_referer( 'update-synced_p_' . $post_id, '_wpnonce_update_synced_p' ); | |
$meta_key = 'synchro_stock_with'; | |
$previously_synced = (array) get_post_meta( $post_id, $meta_key, true ); | |
$freshly_synced = empty( $_POST['synchro_stock_with'] ) ? array() : array_map( 'intval', (array) $_POST['synchro_stock_with'] ); | |
update_post_meta( $post_id, $meta_key, $freshly_synced ); | |
foreach ( $freshly_synced as $id ) { | |
$v = $freshly_synced; | |
$v[] = $post_id; | |
unset( $v[ array_search( $id, $v ) ] ); | |
update_post_meta( $id, $meta_key, $v ); | |
} | |
$to_del = array_diff( $previously_synced, $freshly_synced ); | |
$erase_these = $to_del == $previously_synced; | |
foreach ( $to_del as $id ) { | |
if ( $erase_these ) { | |
$s = (array) get_post_meta( $id, $meta_key, true ); | |
if ( false !== ( $key = array_search( intval( $post_id ), $s ) ) ) { | |
unset( $s[ $key ] ); | |
update_post_meta( $id, $meta_key, $s ); | |
} | |
} else { | |
update_post_meta( $id, $meta_key, array() ); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment