NewbieLootz
NewbieLootz

Reputation: 61

Wordpress Woocommerce Update Meta

ok, so I have an affiliate system. that I am embedding a cookie with. I have gotten the code to work to add a new field to the checkout page and then got it to force the person to enter the cookie's # if they type it into the new field. this is the working code.

   add_action('woocommerce_after_order_notes', 'affid_field');

function affid_field($checkout)
{
    if(isset($_COOKIE['ap_ref_tracking'])) {
        echo '<div style=" width: 335px;clear: both;
    margin-bottom: 75px;" id="affid_field">';
        woocommerce_form_field('affid_field', array(
            'type' => 'text',
            'class' => array(
                'my-field-class form-row-wide'
            ) ,
            'label' => __('Retype Number Shown Below') ,
            'placeholder' => $_COOKIE['ap_ref_tracking'] ,
            'required' => true,
        ) , $checkout->get_value('affid_field'));
        echo '</div>';
    }
}


add_action('woocommerce_checkout_process', 'customise_checkout_field_process');

function customise_checkout_field_process()
{
    if(isset($_COOKIE['ap_ref_tracking'])) {
    // if the field is set, if not then show an error message.
        $field = $_POST['affid_field'];
        $aff = $_COOKIE['ap_ref_tracking'];
        if (!$field or $field != $aff) 
        wc_add_notice(__('Please re-enter '.$aff.' as shown on the Shipping Tab.') , 'error');
    }
}

add_action('woocommerce_checkout_update_order_meta', 'customise_checkout_field_update_order_meta');

function customise_checkout_field_update_order_meta($order_id)
{
    $affid_field = $_POST['affid_field'];
    if ( ! empty( $affid_field ) ) {
        update_post_meta( $order_id, '_affid_field', sanitize_text_field( $affid_field ) );
    }
}

But I was thinking, Can't I just shorten all this up, by just injecting the cookie into the meta update. therefore removing the need for the extra field and the customer having to input the number. So I did this.

add_action('woocommerce_checkout_update_order_meta', 'customise_checkout_field_update_order_meta');

function customise_checkout_field_update_order_meta($order_id)
{
    if(isset($_COOKIE['ap_ref_tracking'])) {
            $aff = $_COOKIE['ap_ref_tracking'];
            update_post_meta( $order_id, '_affid_field', sanitize_text_field( $aff ) );
    )
}

which didn't work at all, it crashed the site. no clue what I am missing here, this should work, right? anyone spots my error and give me a hand it would be greatly appreciated.

Upvotes: 0

Views: 76

Answers (1)

Darsh khakhkhar
Darsh khakhkhar

Reputation: 676

You are using ")" instead of "}" to close the if statement... Try this :-

 add_action('woocommerce_checkout_update_order_meta', 'customise_checkout_field_update_order_meta');

function customise_checkout_field_update_order_meta($order_id)
{
    if(isset($_COOKIE['ap_ref_tracking'])) {
            $aff = $_COOKIE['ap_ref_tracking'];
            update_post_meta( $order_id, '_affid_field', sanitize_text_field( $aff ) );
    }
}

Upvotes: 1

Related Questions