我一直在寻找解决这个问题的方法,但没有运气。我认为我缺少一些非常基本的东西,因为修复听起来很简单。
我正在尝试将客户帐单详细信息添加到 MailChimp 组。
它适用于销售在线课程的网站。
我想要发生的是:用户在现场购买并根据他们的购买自动注册适当的 MailChimp 组(即用户购买每月视频课程,被添加到“每月视频课程”MailChimp 组。)
我已经编写了一些代码,但它不起作用(我收到“未定义变量”错误)。我不确定变量/语法是否正确。我绝不是编码员。
有人可以帮助我吗?
这是我的代码(我把它放在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 文件?电子商务?谢谢你.php?结帐.php?购物车.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);