daily pastebin goal
6%
SHARE
TWEET

Untitled

a guest Apr 16th, 2018 47 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Hook for adding html to the product edit page under linked products
  2. add_action( 'woocommerce_product_options_related', 'add_linked_custom_product_field' );
  3. function add_linked_custom_product_field() {
  4.   global $product_object; // UGH globals.
  5.   ?>
  6.     <div class="options_group show_if_variable">
  7.       <p class="form-field">
  8.         <label for="customLinkedProdField"><?php esc_html_e( 'Custom Linked Field Products', 'woocommerce' ); ?></label>
  9.         <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&hellip;', 'woocommerce' ); ?>" data-action="woocommerce_json_search_products" data-exclude="<?php echo intval( $post->ID ); ?>">
  10.           <?php
  11.           $product_ids = !empty( get_post_meta($product_object->get_id(),'customLinkedProdField', true) ) ? get_post_meta($product_object->get_id(),'customLinkedProdField', true) : array();
  12.           foreach ( $product_ids as $product_id ) {
  13.             $product = wc_get_product( $product_id );
  14.             if ( is_object( $product ) ) {
  15.               echo '<option value="' . esc_attr( $product_id ) . '"' . selected( true, true, false ) . '>' . wp_kses_post( $product->get_formatted_name() ) . '</option>';
  16.             }
  17.           }
  18.           ?>
  19.         </select> <?php echo wc_help_tip( __( 'This lets you choose which products are part of this group.', 'woocommerce' ) ); // WPCS: XSS ok. ?>
  20.       </p>
  21.     </div>
  22.   <?php
  23. }
  24.  
  25. // Filter for saving custom product data
  26. add_filter( 'save_post_product', 'save_custom_product_options' );
  27. public function save_custom_product_options( $post_ID, $product, $update ) {
  28.  
  29.   // DO YOUR OWN SANITIZATION HERE!!!! IMPORTANTTT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  30.  
  31.   $customLinkedProdField = isset( $_POST['customLinkedProdField'] ) ? $_POST['customLinkedProdField'] : array();
  32.   update_post_meta( $post_ID, 'customLinkedProdField', $customLinkedProdField );
  33. }
RAW Paste Data
Top