开发者

Integrating MailChimp into E-Commerce Shopping Cart

I'm about to ask a tough question, and I apologize in advance for the fact that I'm a n00b at javascript!

Basically I am looking to integrate MailChimp list signup into the WP E-Commerce plugin. If I understand correctly, this could be accomplished with Javascript where I send the data to two separate locations.

I found some pretty good info on this process here: http://www.codeproject.com/KB/scripting/multiact.aspx and here POST to a form that POSTS to another form

First off the Requirements from the MailChimp End, pretty simple:

<form action="http://mylist.us1.mailchimp.xxx/xxx/" method="post" id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate" target="_blank">
 <fieldset>
 <legend>join our mailing list</legend>
<div class="indicate-required">* indicates required</div>
<div class="mc-field-group">
<label for="mce-EMAIL">Email Address <strong class="note-required">*</strong>
</label>
<input value="" name="EMAIL" class="required email" id="mce-EMAIL" type="text">
</div>
  <div id="mce-responses">

   <div class="response" id="mce-error-response" style="display: none;"></div>
   <div class="response" id="mce-success-response" style="display: none;"></div>
  </div>
  <div><input value="Subscribe" name="subscribe" id="mc-embedded-subscribe" class="btn" type="submit"></div>
 </fieldset> 
 <a href="#" id="mc_embed_close" class="mc_embed_close">Close</a>
</form>

Now for the tougher part... the WP E-Commerce source code. This is a huge plugin (I think 73 files) but the relevant code is all found in "shopping_cart_page.php"... here is the source for that:

Does anyone know exactly what the final code would look like if I were to integrate the MailChimp code above into the WP E-commerce code below? I'm looking to have a checkbox that gives WP E-commerce store customers the option of opting-in to the MailChimp mailing list so that when they hit "Buy Now" their data is submitted to both the local (WordPress) database as well as the external (MailChimp) database.

<?php
global $wpsc_cart, $wpdb, $wpsc_checkout, $wpsc_gateway, $wpsc_coupons;
$wpsc_checkout = new wpsc_checkout();
$wpsc_gateway = new wpsc_gateways();
$wpsc_coupons = new wpsc_coupons($_SESSION['coupon_numbers']);
 //echo "<pre>".print_r($wpsc_cart,true)."</pre>";
// echo "<pre>".print_r($wpsc_cart,true)."</pre>";
if(wpsc_cart_item_count() > 0) :
?>
<p><?php echo TXT_WPSC_REVIEW_YOUR_ORDER; ?></p>
<table class="productcart">
 <tr class="firstrow">
  <td class='firstcol'></td>
  <td><?php echo TXT_WPSC_PRODUCT; ?>:</td>
  <td><?php echo TXT_WPSC_QUANTITY; ?>:</td>
  <td><?php echo TXT_WPSC_PRICE; ?>:</td>
  <td></td>
 </tr>
 <?php while (wpsc_have_cart_items()) : wpsc_the_cart_item(); ?>

 <?php  //this displays the confirm your order html ?>

  <tr class="product_row">
   <td class="firstcol"><img src='<?php echo wpsc_cart_item_image(48,48); ?>' alt='<?php echo wpsc_cart_item_name(); ?>' title='<?php echo wpsc_cart_item_name(); ?>' /></td>
   <td class="firstcol">
   <a href='<?php echo wpsc_cart_item_url();?>'><?php
    echo wpsc_cart_item_name();
    if(wpsc_cart_item_custom_message()) {
    ?> (<?php echo wpsc_cart_item_custom_message() ?>)<?php
    }
    ?></a>
   </td>
   <td>
    <form action="<?php echo get_option('shopping_cart_url'); ?>" method="post" class="adjustform">
     <input type="text" name="quantity" size="2" value="<?php echo wpsc_cart_item_quantity(); ?>" />
     <input type="hidden" name="key" value="<?php echo wpsc_the_cart_item_key(); ?>" />
     <input type="hidden" name="wpsc_update_quantity" value="true" />
     <input type="submit" value="<?php echo TXT_WPSC_APPLY; ?>" name="submit" />
    </form>
   </td>
   <td><span class="pricedisplay"><?php echo wpsc_cart_item_price(); ?></span></td>
   <td>
    <form action="<?php echo get_option('shopping_cart_url'); ?>" method="post" class="adjustform">
     <input type="hidden" name="quantity" value="0" />
     <input type="hidden" name="key" value="<?php echo wpsc_the_cart_item_key(); ?>" />
     <input type="hidden" name="wpsc_update_quantity" value="true" />
     <button class='remove_button' type="submit"><span><?php echo TXT_WPSC_REMOVE; ?></span></button>
    </form>
   </td>
  </tr>
 <?php endwhile; ?>
 <?php //this HTML displays coupons if there are any active coupons to use ?>
 <?php if(wpsc_uses_coupons()): ?>

  <?php if(wpsc_coupons_error()): ?>
   <tr><td><?php echo TXT_WPSC_COUPONSINVALID; ?></td></tr>
  <?php endif; ?>
  <tr>
   <td colspan="2"><?php _e('Enter your coupon number'); ?> :</td>
   <td  colspan="3" align='left'>
    <form  method='post' action="<?php echo get_option('shopping_cart_url'); ?>">
     <input type='text' name='coupon_num' id='coupon_num' value='<?php echo $wpsc_cart->coupons_name; ?>' />
     <input type='submit' value='<?php echo TXT_WPSC_APPLY ?>' />
    </form>
   </td>
  </tr>
 <?php endif; ?> 
 </table>
 <?php  //this HTML dispalys the calculate your order HTML ?>

 <?php if(isset($_SESSION['nocamsg']) && isset($_GET['noca']) && $_GET['noca'] == 'confirm'): ?>
  <p class='validation-error'><?php echo $_SESSION['nocamsg']; ?></p>
 <?php endif; ?>
 <?php if($_SESSION['categoryAndShippingCountryConflict'] != '') : ?>
  <p class='validation-error'><?php echo $_SESSION['categoryAndShippingCountryConflict']; ?></p>
 <?php
 endif;

 if($_SESSION['WpscGatewayErrorMessage'] != '') :
 ?>
  <p class='validation-error'><?php echo $_SESSION['WpscGatewayErrorMessage']; ?></p>
 <?php
 endif;
 ?>
 <?php do_action('wpsc_before_shipping_of_shopping_cart'); ?>
 <div id='wpsc_shopping_cart_container'>
 <?php if(wpsc_uses_shipping()) : ?>
  <h2><?php echo TXT_WPSC_SHIPPING_COUNTRY; ?></h2>
  <table class="productcart">
   <tr>
    <td colspan='5'>
     <?php echo TXT_WPSC_SHIPPING_DETAIL; ?>
    </td>
   </tr>

   <?php if (!wpsc_have_ship开发者_运维技巧ping_quote()) : // No valid shipping quotes ?>
    <?php if (($_SESSION['wpsc_zipcode'] == '') || ($_SESSION['wpsc_zipcode'] == 'Your Zipcode')) : // No valid shipping quotes ?>
     <?php if ($_SESSION['wpsc_update_location'] == true) :?>
      <tr>
       <td colspan='5' class='shipping_error' >
        <?php echo TXT_WPSC_NO_ZIPCODE; ?>
       </td>
      </tr>
     <?php endif; ?>
    <?php else: ?>
     <tr>
      <td colspan='5' class='shipping_error' >
       <?php echo TXT_WPSC_NO_SHIPPING_QUOTES; ?>
      </td>
     </tr>
    <?php endif; ?>
   <?php endif; ?>
   <tr>
    <td colspan='5'>
     <form name='change_country' id='change_country' action='' method='post'>
      <?php echo wpsc_shipping_country_list();?>
      <input type='hidden' name='wpsc_update_location' value='true' />
      <input type='submit' name='wpsc_submit_zipcode' value='Calculate' />
     </form>
    </td>
   </tr>

   <?php if (wpsc_have_morethanone_shipping_quote()) :?>
    <?php while (wpsc_have_shipping_methods()) : wpsc_the_shipping_method(); ?>
      <?php  if (!wpsc_have_shipping_quotes()) { continue; } // Don't display shipping method if it doesn't have at least one quote ?>
      <tr><td class='shipping_header' colspan='5'><?php echo wpsc_shipping_method_name().TXT_WPSC_CHOOSE_A_SHIPPING_RATE; ?> </td></tr>
      <?php while (wpsc_have_shipping_quotes()) : wpsc_the_shipping_quote(); ?>
       <tr>
        <td colspan='3'>
         <label for='<?php echo wpsc_shipping_quote_html_id(); ?>'><?php echo wpsc_shipping_quote_name(); ?></label>
        </td>
        <td style='text-align:center;'>
         <label for='<?php echo wpsc_shipping_quote_html_id(); ?>'><?php echo wpsc_shipping_quote_value(); ?></label>
        </td>
        <td style='text-align:center;'>
         <?php if(wpsc_have_morethanone_shipping_methods_and_quotes()): ?>
          <input type='radio' id='<?php echo wpsc_shipping_quote_html_id(); ?>' <?php echo wpsc_shipping_quote_selected_state(); ?>  onclick='switchmethod("<?php echo wpsc_shipping_quote_name(); ?>", "<?php echo wpsc_shipping_method_internal_name(); ?>")' value='<?php echo wpsc_shipping_quote_value(true); ?>' name='shipping_method' />
         <?php else: ?>
          <input <?php echo wpsc_shipping_quote_selected_state(); ?> disabled='disabled' type='radio' id='<?php echo wpsc_shipping_quote_html_id(); ?>'  value='<?php echo wpsc_shipping_quote_value(true); ?>' name='shipping_method' />
           <?php wpsc_update_shipping_single_method(); ?>
         <?php endif; ?>
        </td>
       </tr>
      <?php endwhile; ?>
    <?php endwhile; ?>
   <?php endif; ?>

   <?php wpsc_update_shipping_multiple_methods(); ?>


   <?php if (!wpsc_have_shipping_quote()) : // No valid shipping quotes ?>
     </table>
     </div>
    <?php return; ?>
   <?php endif; ?>
  </table>
 <?php endif;  ?>

 <table class="productcart">
 <tr class="total_price total_tax">
  <td colspan="3">
   <?php echo TXT_WPSC_TAX; ?>
  </td>
  <td colspan="2">
   <span id="checkout_tax" class="pricedisplay checkout-tax"><?php echo wpsc_cart_tax(); ?></span>
  </td>
 </tr>
   <?php if(wpsc_uses_shipping()) : ?>
 <tr class="total_price total_shipping">
  <td colspan="3">
   <?php echo TXT_WPSC_TOTALSHIPPING; ?>
  </td>
  <td colspan="2">
   <span id="checkout_shipping" class="pricedisplay checkout-shipping"><?php echo wpsc_cart_shipping(); ?></span>
     </td>
 </tr>
    <?php endif; ?>

   <?php if(wpsc_uses_coupons() && (wpsc_coupon_amount(false) > 0)): ?>
 <tr class="total_price">
  <td colspan="3">
   <?php echo TXT_WPSC_COUPONS; ?>
  </td>
  <td colspan="2">
   <span id="coupons_amount" class="pricedisplay"><?php echo wpsc_coupon_amount(); ?></span>
     </td>
    </tr>
   <?php endif ?>



 <tr class='total_price'>
  <td colspan='3'>
  <?php echo TXT_WPSC_TOTALPRICE; ?>
  </td>
  <td colspan='2'>
   <span id='checkout_total' class="pricedisplay checkout-total"><?php echo wpsc_cart_total(); ?></span>
  </td>
 </tr>


 </table>

  <?php do_action('wpsc_before_form_of_shopping_cart'); ?>

 <form class='wpsc_checkout_forms' action='' method='post' enctype="multipart/form-data">

    <?php 
    /**  
     * Both the registration forms and the checkout details forms must be in the same form element as they are submitted together, you cannot have two form elements submit together without the use of JavaScript.
    */
    ?>

  <?php if(!is_user_logged_in() && get_option('users_can_register') && get_option('require_register')) : ?>
  <h2><?php _e('Not yet a member?');?></h2>
  <p><?php _e('In order to buy from us, you\'ll need an account. Joining is free and easy. All you need is a username, password and valid email address.');?></p>
  <?php if(count($_SESSION['wpsc_checkout_user_error_messages']) > 0) : ?>
   <div class="login_error"> 
    <?php    
    foreach($_SESSION['wpsc_checkout_user_error_messages'] as $user_error ) {
      echo $user_error."<br />\n";
    }
    $_SESSION['wpsc_checkout_user_error_messages'] = array();
    ?>   
    </div>
  <?php endif; ?>


   <fieldset class='wpsc_registration_form'>
   <label><?php _e('Username'); ?>:</label><input type="text" name="log" id="log" value="" size="20"/>
   <label><?php _e('Password'); ?>:</label><input type="password" name="pwd" id="pwd" value="" size="20" />
   <label><?php _e('E-mail'); ?>:</label><input type="text" name="user_email" id="user_email" value="<?php echo attribute_escape(stripslashes($user_email)); ?>" size="20" />
  </fieldset>
 <?php endif; ?>

 <h2><?php echo TXT_WPSC_CONTACTDETAILS; ?></h2>
 <?php/* echo TXT_WPSC_CREDITCARDHANDY; <br /> */?>
 <?php echo TXT_WPSC_ASTERISK; ?>
 <?php
   if(count($_SESSION['wpsc_checkout_misc_error_messages']) > 0) {
   echo "<div class='login_error'>\n\r";
   foreach((array)$_SESSION['wpsc_checkout_misc_error_messages'] as $user_error ) {
    echo $user_error."<br />\n";
   }
   echo "</div>\n\r";
  }
  $_SESSION['wpsc_checkout_misc_error_messages'] =array();
 ?>
 <table class='wpsc_checkout_table'>
  <?php while (wpsc_have_checkout_items()) : wpsc_the_checkout_item(); ?>
   <?php if(wpsc_is_shipping_details()) : ?>
     <tr>
      <td colspan ='2'>
       <br />
       <input type='checkbox' value='true' name='shippingSameBilling' id='shippingSameBilling' />
       <label for='shippingSameBilling'>Shipping Address same as Billing Address?</label>

      </td>
     </tr>
   <?php endif; ?>

    <?php if(wpsc_checkout_form_is_header() == true) : ?>
      <tr <?php echo wpsc_the_checkout_item_error_class();?>>
   <td <?php if(wpsc_is_shipping_details()) echo "class='wpsc_shipping_forms'"; ?> colspan='2'>
    <h4>
     <?php echo wpsc_checkout_form_name();?>
    </h4>
   </td>
    </tr>
    <?php else: ?>
    <?php if((!wpsc_uses_shipping()) && $wpsc_checkout->checkout_item->unique_name == 'shippingstate'): ?>
    <?php else : ?>
      <tr <?php echo wpsc_the_checkout_item_error_class();?>>
   <td>
    <label for='<?php echo wpsc_checkout_form_element_id(); ?>'>
    <?php echo wpsc_checkout_form_name();?>:
    </label>
   </td>
   <td>
    <?php echo wpsc_checkout_form_field();?>

      <?php if(wpsc_the_checkout_item_error() != ''): ?>
      <p class='validation-error'><?php echo wpsc_the_checkout_item_error(); ?></p>

   <?php endif; ?>
   </td>
   </tr>
   <?php endif; ?>

   <?php endif; ?>

  <?php endwhile; ?>

  <?php if (get_option('display_find_us') == '1') : ?>
  <tr>
   <td>How did you find us:</td>
   <td>
    <select name='how_find_us'>
     <option value='Word of Mouth'>Word of mouth</option>
     <option value='Advertisement'>Advertising</option>
     <option value='Internet'>Internet</option>
     <option value='Customer'>Existing Customer</option>
    </select>
   </td>
  </tr>
  <?php endif; ?>  
  <tr>
   <td colspan='2'>

   <?php  //this HTML displays activated payment gateways?>

    <?php if(wpsc_gateway_count() > 1): // if we have more than one gateway enabled, offer the user a choice ?>
     <h3><?php echo TXT_WPSC_SELECTGATEWAY;?></h3>
     <?php while (wpsc_have_gateways()) : wpsc_the_gateway(); ?>
      <div class="custom_gateway">
       <?php if(wpsc_gateway_internal_name() == 'noca'){ ?>
        <label><input type="radio" id='noca_gateway' value="<?php echo wpsc_gateway_internal_name();?>" <?php echo wpsc_gateway_is_checked(); ?> name="custom_gateway" class="custom_gateway"/><?php echo wpsc_gateway_name();?></label>
       <?php }else{ ?>
        <label><input type="radio" value="<?php echo wpsc_gateway_internal_name();?>" <?php echo wpsc_gateway_is_checked(); ?> name="custom_gateway" class="custom_gateway"/><?php echo wpsc_gateway_name();?></label>
       <?php } ?>


       <?php if(wpsc_gateway_form_fields()): ?> 
        <table class='<?php echo wpsc_gateway_form_field_style();?>'>
         <?php echo wpsc_gateway_form_fields();?> 
        </table>  
       <?php endif; ?>   
      </div>
     <?php endwhile; ?>
    <?php else: // otherwise, there is no choice, stick in a hidden form ?>
     <?php while (wpsc_have_gateways()) : wpsc_the_gateway(); ?>
      <input name='custom_gateway' value='<?php echo wpsc_gateway_internal_name();?>' type='hidden' />

       <?php if(wpsc_gateway_form_fields()): ?> 
        <table>
         <?php echo wpsc_gateway_form_fields();?> 
        </table>  
       <?php endif; ?> 
     <?php endwhile; ?>    
    <?php endif; ?>    

   </td>
  </tr>
  <?php if(get_option('terms_and_conditions') != '') : ?>
  <tr>
   <td colspan='2'>
         <input type='checkbox' value='yes' name='agree' /> <?php echo TXT_WPSC_TERMS1;?><a class='thickbox' target='_blank' href='<?php
      echo get_option('siteurl')."?termsandconds=true&amp;width=360&amp;height=400'"; ?>' class='termsandconds'><?php echo TXT_WPSC_TERMS2;?></a>
        </td>
     </tr>
  <?php endif; ?> 
  <tr>
   <td colspan='2'>
    <?php if(get_option('terms_and_conditions') == '') : ?>
     <input type='hidden' value='yes' name='agree' />
    <?php endif; ?> 
    <?php //exit('<pre>'.print_r($wpsc_gateway->wpsc_gateways[0]['name'], true).'</pre>');
     if(count($wpsc_gateway->wpsc_gateways) == 1 && $wpsc_gateway->wpsc_gateways[0]['name'] == 'Noca'){}else{?>
     <input type='hidden' value='submit_checkout' name='wpsc_action' />
     <input type='submit' value='<?php echo TXT_WPSC_MAKEPURCHASE;?>' name='submit' class='make_purchase' />
    <?php }/* else: ?>

    <br /><strong><?php echo TXT_WPSC_PLEASE_LOGIN;?></strong><br />
    <?php echo TXT_WPSC_IF_JUST_REGISTERED;?>
    </td>
    <?php endif;  */?>    
   </td>
  </tr>
 </table>
</form>
</div>
<?php
else:
 echo TXT_WPSC_BUYPRODUCTS;
endif;
do_action('wpsc_bottom_of_shopping_cart');
?>


I am not familiar with WP e-commerce, but I've done something similar. I've added the following function and a call to the function if the 'subscribe' checkbox is checked. I am guessing that this should happen on the page AFTER the page you posted here as this looks like it's confirming the order and not processing it?

I also include a blurb in my email about signing up if they did not check the check box. Hit them again, you know?

if(isset($_REQUEST['subscribe_to_newsletter']) && $_REQUEST['subscribe_to_newsletter'] == 'yes')
{

    echo mailchimp_subscribe($listid);

}

/**
 * Mailchimp Subscribe
 * This function subscribes people to the mailing list.
 * @param string Mailchimp subscription to add users to
 * @return string Success/Fail message
 */
function mailchimp_subscribe($listid)
{


    require_once "{$_SERVER['DOCUMENT_ROOT']}/lib/mailchimp/MCAPI.class.php";
    require_once "{$_SERVER['DOCUMENT_ROOT']}/lib/mailchimp/config.inc"; //contains username & password

    $api = new MCAPI($username, $password);
    if ($api->errorCode){
        // an error occurred while logging in
        echo "code:".$api->errorCode."\n";
        echo "msg :".$api->errorMessage."\n";
        //die(); // don't die, just do nothing
    }

/**
Note that if you are not passing merge_vars, you will still need to pass a "blank" array. That should be either:

$merge_vars = array('');
- or -
$merge_vars = '';

Specifically, this will fail:
$merge_vars = array();

Or pass the proper data as below...
 */

    $merge_vars = array( 'LNAME'=> $_REQUEST['name'], 'INTERESTS'=>'');

    // By default this sends a confirmation email - you will not see new members
    // until the link contained in it is clicked!

    $retval = $api->listSubscribe( $listId, $_REQUEST['email_address'], $merge_vars );

    if ($api->errorCode){

        return "<p>Oops! We were not able to subscribe you to the e-Newsletter mailing list. Maybe you are already subscribed? Please try our <a href=\"http://www.example.com/e-newsletter-signup\">e-Newsletter Subscription page, it should work better.</p>\n<div style='color:white'>\t<span style='color:white;'>code=".$api->errorcode."\n\tmsg=".$api->errormessage."</span></div>\n";
    } else {
        return "<p>Thank you for subscribing to the e-Newsletter. A newsletter confirmation email has been sent to your email address.</p>\n";

    }



}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜