获取数据get_posts()并将其传递给函数wordpress

时间:2016-05-03 09:57:37

标签: php wordpress function foreach

所以,我正在弄清楚如何将旧订单发送到mailchimp。因为我真的找不到这方面的信息,所以我会测试一些东西,直到我得到一些合理的结果。

<?php     
// Get all customer orders
$customer_orders = get_posts( array(
    'numberposts' => -1,
    'meta_key'    => '_customer_user',
    'meta_value'  => get_current_user_id(),
    'post_type'   => wc_get_order_types(),
    'post_status' => array_keys( wc_get_order_statuses() ),
) );

var_dump($customer_orders); //to see what I'm working with

echo "test";
require_once('../public_html/wp-content/plugins/woochimp/woochimp.php'); 

foreach($customer_orders as $cuord) {
    $order = $cuord->ID; 
    echo ($cuord->post_date);
    echo "<br>";
    on_completed($order); //I'm trying to run this as it should try to send some data to mailchimp.
}
?>

当我调用on_completed函数时,似乎打破了foreach循环。我做错了什么?

on_completed函数:(来自插件WooChimp)

    /**
     * Subscribe on order completed status and send Ecommerce360 data
     *
     * @access public
     * @param int $order_id
     * @return void
     */
    public function on_completed($order_id)
    {
        // Check if functionality is enabled
        if (!$this->opt['woochimp_enabled']) {
            return;
        }

        // Check if WC order class is available and MailChimp is loaded
        if (class_exists('WC_Order') && $this->load_mailchimp()) {

            // Do we need to subscribe user on completed order or payment?
            $subscribe_on_completed = get_post_meta($order_id, 'woochimp_subscribe_on_completed', true);
            $subscribe_on_payment = get_post_meta($order_id, 'woochimp_subscribe_on_payment', true);

            foreach (array('auto', 'checkbox') as $sets_type) {
                if ($subscribe_on_completed == $sets_type || $subscribe_on_payment == $sets_type) {
                    $this->subscribe_checkout($order_id, $sets_type);
                }
            }

            // Check if we need to send order data or was it already sent
            if (!$this->opt['woochimp_send_order_data'] || self::order_data_sent($order_id)) {
                return;
            }

            // Get args
            $args = $this->prepare_order_data($order_id);

            // Send order data
            try {
                $this->mailchimp->ecomm_order_add($args);
                update_post_meta($order_id, '_woochimp_ecomm_sent', 1);
            }
            catch (Exception $e) {
                $this->woochimp_log_write($e);
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

如果将on_completed($ order)函数调用放在try catch块中,您会看到什么错误消息文本?示例如下:

try {
    on_completed($order);
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

另外,请检查您的Web服务器error_log。在执行代码时,您是否看到任何错误?