如何在使用WooCommerce进行购买时向MailChimp Group添加客户结算信息

时间:2013-01-26 18:55:15

标签: php wordpress mailchimp woocommerce

我看起来很高,很难解决这个问题,但没有运气。我想我错过了一些非常基本的东西,因为修复听起来非常简单。

我正在尝试将客户结算明细添加到MailChimp Group。

这是一个销售在线课程的网站。

我想要发生的是: 用户在网站上进行购买,并根据购买情况自动注册相应的MailChimp Group(即用户购买每月视频课程,添加到'每月视频课程'MailChimp Group。)

我已经编写了一些代码,但它无法正常工作(我收到了'未定义的变量'错误)。我不确定变量/语法是否正确。我绝不是一个程序员。

有人可以帮助我吗?

这是我的代码(我把它放在functions.php中):

function pass_wp_to_mc() {
require_once 'inc/MCAPI.class.php';
require_once 'inc/config.inc.php'; //contains apikey
require_once 'wp-content/plugins/woocommerce/classes/class-wc-checkout.php';


$api = new MCAPI($apikey);

// Grabs the WooCommerce Product IDs and associates them with the Mailchimp Group IDs - users are put into Groups based on product purchase.

if ($product_id == 42) {
$mailchimpGroupingId = 1;
$mailchimpGroup = 'Monthly';

} elseif ($product_id == 142) {
$mailchimpGroupingId = 1;
$mailchimpGroup = 'Weekly';

} else ($product_id == 144); 
$mailchimpGroupingId = 1;
$mailchimpGroup = 'Audio';

}

$merge_vars = array(
'FNAME' => $billing_first_name,
'LNAME'=> $billing_last_name,
'EMAIL'=> $billing_email,
'GROUPINGS'=>array(
array('id'=>$mailchimpGroupingId, 'groups'=>$mailchimpGroup),
)
);
$listId = 33833; //List ID found inside MailChimp on the page for your List
$my_email = '$email'; 
$double_optin = false; // People are automatically added in to List
$update_existing = true; // Will update users if they are already on the list


$retval = $api->listSubscribe( $listId, $my_email, $merge_vars, $double_optin, $update_existing);

if ($api->errorCode){
echo "Unable to load listSubscribe()!\n";
echo "\tCode=".$api->errorCode."\n";
echo "\tMsg=".$api->errorMessage."\n";
} else {
echo "Subscribed - look for the confirmation email!\n";
}

我的问题是: 这段代码是否正确? 如果是这样,functions.php是放置它的地方吗? 如果是这样,我如何“呼叫”它以及我将把呼叫放在哪里 - WordPress文件? WooCommerce? thankyou.php? checkout.php? cart.php?

非常感谢任何帮助 - 我一直试图解决这个问题几周!

更新:我明白了!首先,代码不正确。这是有用的:

require_once dirname(__FILE__).'/inc/MCAPI.class.php';
require_once dirname(__FILE__).'/inc/config.inc.php';

add_action('woocommerce_checkout_order_processed', 'get_info');

function get_info($order_id) { 
global $woocommerce;

$order = new WC_Order( $order_id );
$firstname = $order->billing_first_name;
$lastname = $order->billing_last_name;
$email = $order->billing_email;
$product_id=unserialize($order->order_custom_fields["_order_items"][0]);
$product_id=$product_id[0]['id'];

    global $apikey;
$api = new MCAPI($apikey);

if ($product_id == *GET THIS ID AT THE EDITING SCREEN OF YOUR PARTICULAR WOOCOMMERCE PRODUCT*) {
$mailchimpGroup = '*ENTER THE NAME OF YOUR MAILCHIMP GROUP (NOT THE TITLE)*';

} elseif ($product_id == *GET THIS ID AT THE EDITING SCREEN OF YOUR PARTICULAR WOOCOMMERCE PRODUCT*) {
$mailchimpGroup = '*ENTER THE NAME OF YOUR MAILCHIMP GROUP (NOT THE TITLE)*';

} else ($product_id == *GET THIS ID AT THE EDITING SCREEN OF YOUR PARTICULAR WOOCOMMERCE PRODUCT*); 
$mailchimpGroup = '*ENTER THE *NAME* OF YOUR MAILCHIMP GROUP (NOT THE TITLE)*';

$merge_vars = array(
                    'FNAME' => $firstname,
                    'LNAME'=> $lastname,
                    //'EMAIL'=> $email,
                    'GROUPINGS'=>array(
                        array('name'=>'*ENTER THE TITLE OF YOUR MAICHIMP GROUP (NOT THE NAME)', 'groups'=>$mailchimpGroup),
                        )
                    );
$listId = 'YOUR LIST ID HERE'; //List ID found inside MailChimp on the page for your List
$my_email = $email; 
$double_optin = false; // People are automatically added in to List
$update_existing = true;    // Will update users if they are already on the list               


$retval = $api->listSubscribe( $listId, $my_email, $merge_vars, $double_optin, $update_existing);

1 个答案:

答案 0 :(得分:1)

现在有一个插件 - 看看WooChimp - MailChimp WooCommerce Integration。这里有些人不熟悉PHP,所以我认为这可能会有所帮助。

完全披露:我是该插件的作者。