Support » Fixing WordPress » Update Post Meta . Please help!

  • So I was following a guide to creating custom fields in checkout depending on the items in cart. and i did it!! woooo.

    but now when an order is placed the meta information does not follow into woocommerce. I think thats because in the guide it takes an array of custom fields and mine has an array of categories wrapping it.

    Long story short the info does not follow into woocommerce and im so stressed out please help my poor soul!!

    // Add a new checkout field
    function kia_filter_checkout_fields($fields){
    
    	global $woocommerce;
    
    //assigns a default negative value
    //  categories targeted 17, 18, 19
    
    $product_in_cart = "false";
     $fields['extra_fields'] = array(); 
    
    // start of the loop that fetches the cart items
    
    foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
        $_product = $values['data'];
        $_product_title = $values['data']->post->post_title;
        $terms = get_the_terms( $_product->id, 'product_cat' );
    
    // second level loop search, in case some items have several categories
                foreach ($terms as $term) {
                    $_categoryid = $term->term_id;
    
        if (( $_categoryid === 14 )) {
            // category is in cart!
       $soundcloud = array('soundcloud' => array(
                    'type' => 'text',
                    'required'      => true,
                    'placeholder' => "SoundCloud URL",
                    'label' => __( 'SoundCloud Link' ).(' : ').$_product_title
                    ));
            array_push($fields['extra_fields'], $soundcloud);
        }
    
         if (( $_categoryid === 12 )) {
            // category is in cart!
      $twitter =    array( 'Twitter' => array(
                    'type' => 'text',
                    'required'      => true,
                    'placeholder' => "Twitter Handle",
                    'label' => __( 'Twitter Handle' ).(' : ').$_product_title
                    ));
            array_push($fields['extra_fields'], $twitter);
        }z
        if (( $_categoryid === 13 )) {
            // category is in cart!
       $youtube =   array(  'YouTube' => array(
                    'type' => 'text',
                    'required'      => true,
                    'placeholder' => "YouTube URL",
                    'label' => __( 'YouTube URL' ).(' : ').$_product_title
                    ));
            array_push($fields['extra_fields'], $youtube);
        }
    
         if (( $_categoryid === 9 )) {
            // category is in cart!
        $instagram = array( 'Instagram' => array(
                    'type' => 'text',
                    'required'      => true,
                    'placeholder' => "Instagram Handle",
                    'label' => __( 'Instagram Handle' ).(' : ').$_product_title
                    ));
            array_push($fields['extra_fields'], $instagram);
        }
        if (( $_categoryid === 8 )) {
            // category is in cart!
        $googleplus = array( 'GooglePlus' => array(
                    'type' => 'text',
                    'required'      => true,
                    'placeholder' => "Google Plus URL",
                    'label' => __( 'Google Plus URL' ).(' : ').$_product_title
                    ));
            array_push($fields['extra_fields'], $googleplus);
        }
    
         if (( $_categoryid === 7 )) {
            // category is in cart!
        $facebook = array(  'Facebook' => array(
                    'type' => 'text',
                    'required'      => true,
                    'placeholder' => "Facebook URL",
                    'label' => __( 'Facebook URL' ).(' : ').$_product_title
                    ));
            array_push($fields['extra_fields'], $facebook);
        }    if (( $_categoryid === 10 )) {
            // category is in cart!
        $pinterest = array( 'Pinterest' => array(
                    'type' => 'text',
                    'required'      => true,
                    'placeholder' => "Pinterest URL",
                    'label' => __( 'Pinterest URL' ).(' : ').$_product_title
                    ));
            array_push($fields['extra_fields'], $pinterest);
        }
    
         if (( $_categoryid === 11 )) {
            // category is in cart!
    
        $tumblr = array(   'Tumblr' => array(
                    'type' => 'text',
                    'required'      => true,
                    'placeholder' => "Tumblr Link",
                    'label' => __( 'Tumblr Link' ).(' : ').$_product_title
                    ));     
    
            array_push($fields['extra_fields'], $tumblr);
        }
        if (( $_categoryid === 15 )) {
            // category is in cart!
        $reverbnation = array('Reverbnation' => array(
                    'type' => 'text',
                    'required'      => true,
                    'placeholder' => "Reverbnation Link",
                    'label' => __( 'Reverbnation Link' ).(' : ').$_product_title
                    ));
            array_push($fields['extra_fields'], $reverbnation);
        }
    
        //  if (( $_categoryid === 14 )) {
        //     // category is in cart!
        //     array_push($fields['extra_fields'], $twitter);
        // }    
    
        // array_push($fields['extra_fields'], $soundcloud);
        // array_push($fields['extra_fields'], $soundcloud);
        // array_push($fields, $soundcloud);
    
    }
    }
        return $fields;
    
    }
    add_filter( 'woocommerce_checkout_fields', 'kia_filter_checkout_fields' );
    
    // display the extra field on the checkout form
    function kia_extra_checkout_fields(){ 
    
        $checkout = WC()->checkout(); ?>
    
        <div class="extra-fields">
        <h3><span>2</span><?php _e( 'Please enter links below' ); ?></h3><p>(Corresponding to Cart Below)</p><br></br>
    
        </div>
    
    <?php }
    add_action( 'woocommerce_checkout_after_customer_details' ,'kia_extra_checkout_fields' );
    
    function new_excerpt_length($length) {
        return 200;
    }
    add_filter('excerpt_length', 'new_excerpt_length');
    
    // display the extra data on order recieved page and my-account order review
    function kia_display_order_data( $order_id ){  ?>
        <h2><?php _e( 'Additional Info' ); ?></h2>
        <table class="shop_table shop_table_responsive additional_info">
            <tbody>
                <tr>
                    <th><?php _e( 'SoundCloud Link:' ); ?></th>
                    <td><?php echo get_post_meta( $order_id, '_soundcloud', true ); ?></td>
                </tr>
                <tr>
                    <th><?php _e( 'Twitter Handle:' ); ?></th>
                    <td><?php echo get_post_meta( $order_id, '_Twitter', true ); ?></td>
                </tr>
                <tr>
                    <th><?php _e( 'Facebook URL:' ); ?></th>
                    <td><?php echo get_post_meta( $order_id, '_Facebook', true ); ?></td>
                </tr>
                <tr>
                    <th><?php _e( 'Reverbnation URL:' ); ?></th>
                    <td><?php echo get_post_meta( $order_id, '_Reverbnation', true ); ?></td>
                </tr>
                <tr>
                    <th><?php _e( 'YouTube URL:' ); ?></th>
                    <td><?php echo get_post_meta( $order_id, '_YouTube', true ); ?></td>
                </tr>
                <tr>
                    <th><?php _e( 'Instagram Handle:' ); ?></th>
                    <td><?php echo get_post_meta( $order_id, '_Instagram', true ); ?></td>
                </tr>
                <tr>
                    <th><?php _e( 'Google Plus Link:' ); ?></th>
                    <td><?php echo get_post_meta( $order_id, '_GooglePlus', true ); ?></td>
                </tr>
                <tr>
                    <th><?php _e( 'Tumblr URL:' ); ?></th>
                    <td><?php echo get_post_meta( $order_id, '_Tumblr', true ); ?></td>
                </tr>
                <tr>
                    <th><?php _e( 'Instagram Link:' ); ?></th>
                    <td><?php echo get_post_meta( $order_id, '_Instagram', true ); ?></td>
                </tr>
            </tbody>
        </table>
    <?php }
    add_action( 'woocommerce_thankyou', 'kia_display_order_data', 20 );
    add_action( 'woocommerce_view_order', 'kia_display_order_data', 20 ); 
    
    // save the extra field when checkout is processed
    function kia_save_extra_checkout_fields( $order_id, $posted ){
    
        if( isset( $posted['soundcloud'] ) && in_array( $posted['soundcloud'] ) ) {
            update_post_meta( $order_id, '_soundcloud', $posted['soundcloud'] );
        }
    }
    add_action( 'woocommerce_checkout_update_order_meta', 'kia_save_extra_checkout_fields', 10, 2 );
  • The topic ‘Update Post Meta . Please help!’ is closed to new replies.