Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Adding WYSIJA opt-in to WooCommerce check-out form WordPress

  • SOLVED

I have a regular WooCommerce check-out page with a ready form and everything, and I want the customers to be able to check a box just before paying, that says:

"Want to sign up for our newsletter?"

I have found this page from WYSIJA:
http://support.wysija.com/knowledgebase/plugin-form-integrate/

That lead me to here. I am not sure how I get the check-box to work, but I am thinking it's either in form-billing.php or in review-order.php?

If anyone can help, I would be very happy!

Answers (1)

2013-03-10

Christianto answers:

Hi,

Only the checkbox? have you create function to add user to subscription list?
if you only need to put check box, you could add this on functions.php to show the checkbox..

add_action('woocommerce_checkout_after_customer_details', 'my_subscription_checkbox');
function my_subscription_checkbox(){
echo '<p class="form-row">';
echo '<input class="input-checkbox" id="subscription-box" type="checkbox" name="subscribe_newsletter" value="1"> <label for="subscribe_newsletter" class="checkbox">Want to sign up for our newsletter?</label>';
echo '</p>';
}

above hook will make checkbox appear on checkout page, or you could edit directly but you need to copy file of your woocommerce templates to your theme directory (the file is form-checkout.php).


Kasper comments:

Hi Christianto,

I am not sure I understand your question :) Or that I made myself clear..

I have not added a create function myself..

I just installed WooCommerce and WYSIJA-Newsletter plugin. Now I want a function on the check out page, just before people are pressing "Place your order" that says:

"Sign up for our newsletter - check the box here:"

And if they do, they get put on the mailing list for my newsletter, with the e-mail they used in their check-out (billing fields).


Christianto comments:

ok,

Please try this,
function my_subscription_checkbox(){
// set to true to show your each list ID
$show_list_id = false;

echo '<p class="form-row">';
echo '<input class="input-checkbox" id="subscription-box" type="checkbox" name="subscribe_newsletter" value="1"> <label for="subscribe_newsletter" class="checkbox">Want to sign up for our newsletter?</label>';
echo '</p>';

if($show_list_id){
//this will return an array of results with the name and list_id of each mailing list
$data = array('name','list_id'), array('is_enabled'=>1);
$modelList = &WYSIJA::get('list','model');
$wysijaLists = $modelList->get($data);
//this loop will just echo the information selected for each list
foreach($wysijaLists as $l ist){
echo 'list id : '.$list['list_id'].' , list name : '.$list['name'] ."<br/>";
}
}
}
add_action('woocommerce_checkout_after_customer_details', 'my_subscription_checkbox');

function my_subscription_checkout_processor(){

// change or remove this list ID
$listID = array( 'my_list_ID_1', 'my_list_ID_2' );

$subscribe = $_POST['subscribe_newsletter'] ;
$email = $_POST['billing_email'];
if(!empty($subscrib) && !empty($email)){
$userData=array(
'email' => $email,
'firstname' => $_POST['billing_first_name'],
'lastname' => $_POST['billing_last_name']
);
$data=array(
'user' => $userData,
'user_list' => array( 'list_ids'=> $listID )
);
$userHelper=&WYSIJA::get('user','helper');
$userHelper->addSubscriber($data);
}
}
add_action('init','my_subscription_checkout_processor');


You need to set your list ID for this subscriber
// change or remove this list ID
$listID = array( 'my_list_ID_1', 'my_list_ID_2' );


to see your list id set this to true and you can see each list id on checkout page.
// set to true to show your each list ID
$show_list_id = false;


Kasper comments:

I added the code above to my functions.php on the template, and the following error returns:

Parse error: syntax error, unexpected ',' in /var/www/empiresko.dk/public_html/wp-content/themes/woostore/functions.php on line 111

What am I doing wrong?


Christianto comments:

Sorry, I copy the function from wysija.com without checking it.

remove this line
$data = array('name','list_id'), array('is_enabled'=>1);
and change
$wysijaLists = $modelList->get($data);
//this loop will just echo the information selected for each list
foreach($wysijaLists as $l ist){
echo 'list id : '.$list['list_id'].' , list name : '.$list['name'] ."<br/>";
}


to
$wysijaLists = $modelList->get(array('name','list_id'), array('is_enabled'=>1));
//this loop will just echo the information selected for each list
foreach($wysijaLists as $list){
echo 'list id : '.$list['list_id'].' , list name : '.$list['name'] ."<br/>";
}