Created
April 8, 2018 18:20
-
-
Save JohnTendik/af4d787d00c8305e424e9880f88ed5b4 to your computer and use it in GitHub Desktop.
Woocommerce Linked Products Custom Product Search/:ink Field
This file contains hidden or 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
// Hook for adding html to the product edit page under linked products | |
add_action( 'woocommerce_product_options_related', 'add_linked_custom_product_field' ); | |
function add_linked_custom_product_field() { | |
global $product_object; // UGH globals. | |
?> | |
<div class="options_group show_if_variable"> | |
<p class="form-field"> | |
<label for="customLinkedProdField"><?php esc_html_e( 'Custom Linked Field Products', 'woocommerce' ); ?></label> | |
<select class="wc-product-search" multiple="multiple" style="width: 50%;" id="customLinkedProdField" name="customLinkedProdField[]" data-sortable="true" data-placeholder="<?php esc_attr_e( 'Search for a product…', 'woocommerce' ); ?>" data-action="woocommerce_json_search_products" data-exclude="<?php echo intval( $post->ID ); ?>"> | |
<?php | |
$product_ids = !empty( get_post_meta($product_object->get_id(),'customLinkedProdField', true) ) ? get_post_meta($product_object->get_id(),'customLinkedProdField', true) : array(); | |
foreach ( $product_ids as $product_id ) { | |
$product = wc_get_product( $product_id ); | |
if ( is_object( $product ) ) { | |
echo '<option value="' . esc_attr( $product_id ) . '"' . selected( true, true, false ) . '>' . wp_kses_post( $product->get_formatted_name() ) . '</option>'; | |
} | |
} | |
?> | |
</select> <?php echo wc_help_tip( __( 'This lets you choose which products are part of this group.', 'woocommerce' ) ); // WPCS: XSS ok. ?> | |
</p> | |
</div> | |
<?php | |
} | |
// Filter for saving custom product data | |
add_filter( 'save_post_product', 'save_custom_product_options' ); | |
public function save_custom_product_options( $post_ID, $product, $update ) { | |
// DO YOUR OWN SANITIZATION HERE!!!! IMPORTANTTT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | |
$customLinkedProdField = isset( $_POST['customLinkedProdField'] ) ? $_POST['customLinkedProdField'] : array(); | |
update_post_meta( $post_ID, 'customLinkedProdField', $customLinkedProdField ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment