如何获取订单数据woocommerce?

时间:2019-02-21 09:01:35

标签: php wordpress woocommerce

我使用此源代码。此代码不起作用,为什么? 电报API和chat_id是正确的。

add_action( 'woocommerce_new_order', 'send_massage_to_telegram',  1, 1  );
function send_massage_to_telegram ($order_id) {

    $order = wc_get_order($order_id);

// Iterating through each WC_Order_Item_Product objects
    foreach ($order->get_items() as $item_key => $item_values) {

        ## Using WC_Order_Item methods ##

        // Item ID is directly accessible from the $item_key in the foreach loop or
        $item_id = $item_values->get_id();


        ## Using WC_Order_Item_Product methods ##
        $item_name = $item_values->get_name(); // Name of the product
        $item_type = $item_values->get_type(); // Type of the order item ("line_item")

        $product_id = $item_values->get_product_id(); // the Product id
        $product = $item_values->get_product(); // the WC_Product object

        ## Access Order Items data properties (in an array of values) ##
        $item_data = $item_values->get_data();

        $product_name = $item_data['name'];
        $product_id = $item_data['product_id'];
        $variation_id = $item_data['variation_id'];
        $quantity = $item_data['quantity'];
        $tax_class = $item_data['tax_class'];
        $line_subtotal = $item_data['subtotal'];
        $line_subtotal_tax = $item_data['subtotal_tax'];
        $line_total = $item_data['total'];
        $line_total_tax = $item_data['total_tax'];
        $item_total = $item_data->get_total();

        // Get data from The WC_product object using methods (examples)
        $product_type = $product->get_type();
        $product_sku = $product->get_sku();
        $product_price = $product->get_price();
        $stock_quantity = $product->get_stock_quantity();


    }

     $sendToTelegram = fopen("https://api.telegram.org/bot:xxxxxxxxxxxxxxxxx/sendMessage?chat_id=00000000000&parse_mode=html&text={$product_price}", "r");
}

0 个答案:

没有答案