Session cookie and using ID in url

I am using wordpress, and want to give a url to a customer with a specific id on it for him, such as -

hiswebsite.com/trade=groundeffect

If a user lands on our site with that url, and want to use it in a specif way on my site, but can someone give me a heads up, how I start with taking that id for starters in a session cookie and then how do i call it in say jscript to use somewhere else

Hi,

Could this old thread be of any interest?

1 Like

Can’t you use .htaccess redirects for this?

Hi Gandalf458,

Its not a redirect Im needing, basically we want to give this other company a url so they can advertise it on their social media accounts, if a customer clicks that link and comes to our site we want to change the dropdown in the shopping cart to already be set to the compnay where it came from, because that Where did you hear about us drop down is a required field, but if say its come from a compnay called Ground Run, then i want the dropdown to already be set to Ground Run, so they dont have to choose.

When the transaction goes through,it will then show where they hear about is and we can give the other compnay their commission.

Ive added this below to the header, which is on every page through my website, so it sets nicely when the url has ?id=GroundRun, but then when i go to another page the coockie empties, I understand why, but not sure how to resolve that. Basically once the cookie is set i want it to stay there until they leave the site.

<?php
session_start();

$cookie_business = $_GET['id'];

setcookie($cookie_business);
if(!isset($_COOKIE[$cookie_business])) {
    echo "Cookie named '" . $cookie_business . "";
}
1 Like

I’d put it in the session like so:

session_start();

if (!empty($_GET['id']) && empty($_SESSION['partner'])) {
    $_SESSION['partner'] = $_GET['id'];
}

and then when you render the dropdown, for each value check of the value is the one that is in the session and if so pre-select it. Something like this:

$partners = ['ACME', 'GroundRun', 'Babelfish'];

foreach ($partners as $partner) {
    echo sprintf(
        '<option value="%s"%s>%s</option>',
        $partner,
        $_SESSION['partner'] === $partner ? ' selected="selected"': '',
        $partner
    );
}

Also, I’d recommend using a different variable name than id, because that is really generic. Something like partner would be better IMHO.

1 Like

Thanks rpkamp,

Think my logic is good in what I was attempting to do, but my PHP skills doesnt match unfortunatly.

The ‘Where did you hear about us’ drop down is a wordpress plugin, and I thought I coulld get this working, but i just cant get it right, this is theoriginal code that deals with the dropdown, and thought id ask you if my idea would be possible to work into the code below, to get it working.

$source_options = array( '' =>  __( 'Select an option&hellip;', 'woocommerce' ) );
$source_options = array_merge( $source_options, (array) $this->settings['checkout_field_options'] );

if ( ! $this->settings['other_field_disable'] ) {
     $source_options['other'] = __( 'Other', 'woocommerce-admin' );
}
?>
<div id="wc-customer-source">
<?php
    woocommerce_form_field( 'wc_customer_source_checkout_field', array(
    'type'          => 'select',
    'class'         => array('wc-customer-source-select form-row-wide'),
    'label'         => $this->settings['checkout_field_label'],
    'options'		=> $source_options,
    'required'		=> array_key_exists( 'checkout_field_required', $this->settings ) &&    $this->settings['checkout_field_required'] ? true : false,
) );
?>
<div class="wc-customer-source-other-field form-row form-row-wide" style="display: none">
<label><?php echo $this->settings['other_field_label']; ?></label>
<input class="input-text" type="text" name="wc_customer_source_checkout_other_field" value="" disabled>
</div>
</div>

Going by this page you should be able to pass a default to woocommerce_form_field. Something like this:

<?php
    woocommerce_form_field( 'wc_customer_source_checkout_field', array(
    'type'          => 'select',
    'class'         => array('wc-customer-source-select form-row-wide'),
    'label'         => $this->settings['checkout_field_label'],
    'options'		=> $source_options,
    'required'		=> array_key_exists( 'checkout_field_required', $this->settings ) &&    $this->settings['checkout_field_required'] ? true : false,
    'default'       => isset($_SESSION['partner']) ? $_SESSION['partner'] : '',
) );

I’m not sure you should use the value or the label though, but I’m guessing you’d need the value.

1 Like