Вставка нескольких строк из Woocommerce во внешнюю базу данных - PullRequest
1 голос
/ 28 марта 2020

Я просто возвращаюсь в php после более чем десятилетия, и да, это немного грубо. Я пытаюсь вставить информацию о заказе из магазина Wordpress WooCommerce в другую систему управления заказами и доставки. То, что у меня есть, вставит один предмет, но если есть два предмета, я получу только первый. Я просто не могу понять, что я oop. Я оставил свои данные подключения вверху, иначе мой код здесь. Я уверен, что я не перебираю пункты правильно. Я сохранил весь контент stati c вверху, а затем попытался узнать подробности c динамика. Ценю любую помощь. Я видел несколько близких примеров, но мои глаза мертвы.

  // get order object and static order details
$order = new WC_Order( $order_id );
    $billingcompany = $order->get_billing_company();
    $shippingcompany = $order->get_shipping_company();
$email = $order->get_billing_email();
$officenumber = $order->get_billing_phone();
$shipping_type = $order->get_shipping_method();
$shipping_cost = $order->get_total_shipping();
    $billing_first_name = $order->get_billing_first_name();
    $billing_last_name = $order->get_billing_last_name();
    $billing_address_1 = $order->get_billing_address_1();
    $billing_address_2 = $order->get_billing_address_2();
    $billing_city = $order->get_billing_city();
    $billing_state = $order->get_billing_state();
    $billing_postcode = $order->get_billing_postcode();
    $shipping_first_name = $order->get_shipping_first_name();
    $shipping_last_name = $order->get_shipping_last_name();
    $shipping_address_1 = $order->get_shipping_address_1();
    $shipping_address_2 = $order->get_shipping_address_2();
    $shipping_city = $order->get_shipping_city();
    $shipping_state = $order->get_shipping_state();
    $shipping_postcode = $order->get_shipping_postcode();
    $orderdate= $order->get_date_created();
    $salesman= "online order";
    $ordertime = date("g:i a");
    $paymenttype = $order->get_payment_method();
    $ordertotal = $order->get_total();
    $specialnotes = $order->get_customer_note();
    $createdby = "website";

    /* for online payments, send across the transaction ID/key. If the payment is handled offline, you could send across the order key instead */
$transaction_key = get_post_meta( $order_id, '_transaction_id', true );
$transaction_key = empty($transaction_key) ? $_GET['key'] : $transaction_key;

// get coupon information (if applicable)
$cps = array();
$cps = $order->get_items( 'coupon' );

$coupon = array();
foreach($cps as $cp){
        // get coupon titles (and additional details if accepted by the API)
        $coupon[] = $cp['name'];
}

// get product details
$items = $order->get_items();

$item_name = array();
$item_qty = array();
$item_price = array();
$item_sku = array();

foreach( $items as $key => $item){
    $item_name[] = $item['name'];
    $item_qty[] = $item['qty'];
    $item_price[] = $item['line_total'];

    $item_id = $item['product_id'];
    $product = new WC_Product($item_id);
    $item_sku[] = $product->get_sku();

            // insert command
            $mydb->insert($tablename,
            array(
                'customer_email' => $email,
                'customer_phone' => $officenumber,
                'bill_firstname' => $billing_first_name,
                'bill_lastname' => $billing_last_name,
                'bill_address1' => $billing_address_1,
                'bill_address2' => $billing_address_2,
                'bill_city' => $billing_city,
                'bill_state' => $billing_state,
                'bill_zip' => $billing_postcode,
                'ship_firstname' => $shipping_first_name,
                'ship_lastname' => $shipping_last_name,
                'ship_address1' => $shipping_address_1,
                'ship_address2' => $shipping_address_2,
                'ship_city' => $shipping_city,
                'ship_state' => $shipping_state,
                'ship_zip' => $shipping_postcode,
                'shipping_type' => $shipping_type,
                'shipping_cost' => $shipping_cost,
                'item_name' => implode(',', $item_name),
                'item_sku' => implode(',', $item_sku),
                'item_price' => implode(',', $item_price),
                'quantity' => implode(',', $item_qty),
                'transaction_key' => $transaction_key,
                'coupon_code' => implode( ",", $coupon )
                /*'orderdate' => $orderdate;
                'salesman' => $salesman;
                'ordertime' =>$ordertime;
                'paymenttype' =>$paymenttype;
                'ordertotal' =>$ordertotal;
                'specialnotes' =>$specialnotes;
                'createdby' => $createdby;*/

                )
            );
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...