我已经上下寻找解决这个问题,但没有运气。 我想我错过了一些非常基本的东西,因为修复听起来很简单。
我正在尝试将客户帐单详细信息添加到MailChimp Group。
它适用于销售在线课程的网站。
我想发生的是:用户在网站上进行购买,并根据他们的购买自动注册相应的MailChimp Group(即用户购买每月视频课程,被添加到"每月视频课程"MailChimp Group。
我已经编写了一些代码,但它不起作用(我收到"未定义的变量"错误)。我不确定变量/语法是否正确。 我绝不是编码员。
有人可以帮助我吗?
这是我的代码(我放在函数中.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";
}
我的问题是:这段代码是否正确?如果是这样,函数.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);
现在有一个插件 - 看看WooChimp - MailChimp WooCommerce Integration。这里有人不太了解 PHP,所以我认为这可能会有所帮助。
完全披露:我是插件的作者。