Created
July 20, 2021 08:55
-
-
Save plugin-republic/533daa94a525d91f3b7b01bba4732c78 to your computer and use it in GitHub Desktop.
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 | |
/** | |
* Use this snippet to make a product level field conditional on the value of a global field | |
* On your product level field add a condition based on another product level field | |
* Then enter the field and groups IDs below to swap the product level field with a global level field | |
*/ | |
function prefix_get_field_ids_to_replace() { | |
// You need to update the field and group IDs below | |
$ids['global-group'] = 8812; // This is the ID of the global group | |
$ids['global-field'] = 8813; // This is the ID of the global field | |
$ids['local-group'] = 9209; // This is the ID of the product-level group that will be conditional on the global field | |
$ids['local-field'] = 9209; // This is the ID of the product-level field that will be conditional on the global field | |
return $ids; | |
} | |
function prefix_single_product_classes( $classes, $item ) { | |
$ids = prefix_get_field_ids_to_replace(); | |
// Add the ID of the global field that should trigger the condition | |
if( $item['field_id'] == $ids['global-field'] ) { | |
$classes[] = 'pewc-field-triggers-condition'; | |
} | |
return $classes; | |
} | |
add_filter( 'pewc_filter_single_product_classes', 'prefix_single_product_classes', 10, 2 ); | |
function prefix_item_attributes( $attributes, $item ) { | |
$ids = prefix_get_field_ids_to_replace(); | |
// Add the ID of the global field that should trigger the condition | |
if( $item['field_id'] == $ids['global-field'] ) { | |
$attributes['data-triggers-for'] = '[' . $ids['local-field'] . ']'; // This is the ID of the field that will be dependent on the global condition | |
} | |
return $attributes; | |
} | |
add_filter( 'pewc_filter_item_attributes', 'prefix_item_attributes', 10, 2 ); | |
function prefix_change_condition_field_id( $field, $item, $product_id ) { | |
$ids = prefix_get_field_ids_to_replace(); | |
if( $item['field_id'] == $ids['local-field'] ) { | |
$field = 'pewc_group_' . $ids['global-group'] . '_' . $ids['global-field']; | |
} | |
return $field; | |
} | |
add_filter( 'pewc_field_condition_field', 'prefix_change_condition_field_id', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment