admin管理员组

文章数量:1296482

I am working on a new website for my company. We have products with variations. each variation have its own SKU number. When we select the product, the correct item is displayed in the cart with the correct SKU (the one assigned to the variation).

I'm using another plugin that creates Hosted Orders for me that then can be downloaded to our ERP system and all of that works fine except for one thing. The SKU that is getting passed to the Hosted Order is the parent item SKU and not the variant.

I need a way to determine if the product has variations and the variation SKU becomes the product SKU which is picked up by the other plugin. I've tried an if/else function in the new plugin but it didn't change the result.

I believe the code that is creating the Hosted Order is this code snippet.

        $items = $this->order->get_items();
        // Line items can be added to a Transaction individually or as an array
        $lineItems = array();
        $itemcount = 0;
        foreach($items as $item_id => $item_data) {
            array_push($lineItems, new ConnectLineItem);
            $product = wc_get_product($item_data['product_id']);
            $lineItems[$itemcount]->ProductCode = $product->get_sku();
            $lineItems[$itemcount]->Category = "";
            $lineItems[$itemcount]->Description = substr($item_data['name'], 0, 50);
            $lineItems[$itemcount]->Quantity = $item_data['quantity'];
            $lineItems[$itemcount]->UnitOfMeasure = "PCE";
            $lineItems[$itemcount]->LineAmount = $item_data['total'];
            $lineItems[$itemcount]->UnitPrice = $item_data['total'] / $item_data['quantity'];
            $lineItems[$itemcount]->LineTaxAmount = $item_data['subtotal_tax'];
            $lineItems[$itemcount]->LineDiscountAmount = "0.00";
            $itemcount = $itemcount + 1;
        }
        $trans->addLineItemArray($lineItems);

I am admittedly a real novice at this so any and all suggestions would be helpful.

I am working on a new website for my company. We have products with variations. each variation have its own SKU number. When we select the product, the correct item is displayed in the cart with the correct SKU (the one assigned to the variation).

I'm using another plugin that creates Hosted Orders for me that then can be downloaded to our ERP system and all of that works fine except for one thing. The SKU that is getting passed to the Hosted Order is the parent item SKU and not the variant.

I need a way to determine if the product has variations and the variation SKU becomes the product SKU which is picked up by the other plugin. I've tried an if/else function in the new plugin but it didn't change the result.

I believe the code that is creating the Hosted Order is this code snippet.

        $items = $this->order->get_items();
        // Line items can be added to a Transaction individually or as an array
        $lineItems = array();
        $itemcount = 0;
        foreach($items as $item_id => $item_data) {
            array_push($lineItems, new ConnectLineItem);
            $product = wc_get_product($item_data['product_id']);
            $lineItems[$itemcount]->ProductCode = $product->get_sku();
            $lineItems[$itemcount]->Category = "";
            $lineItems[$itemcount]->Description = substr($item_data['name'], 0, 50);
            $lineItems[$itemcount]->Quantity = $item_data['quantity'];
            $lineItems[$itemcount]->UnitOfMeasure = "PCE";
            $lineItems[$itemcount]->LineAmount = $item_data['total'];
            $lineItems[$itemcount]->UnitPrice = $item_data['total'] / $item_data['quantity'];
            $lineItems[$itemcount]->LineTaxAmount = $item_data['subtotal_tax'];
            $lineItems[$itemcount]->LineDiscountAmount = "0.00";
            $itemcount = $itemcount + 1;
        }
        $trans->addLineItemArray($lineItems);

I am admittedly a real novice at this so any and all suggestions would be helpful.

Share Improve this question edited Apr 3, 2021 at 20:50 Amirition 3555 silver badges20 bronze badges asked Apr 3, 2021 at 3:32 Chuck BChuck B 12 bronze badges 1
  • How are the products and SKU stored in WordPress? – Matthew Brown aka Lord Matt Commented Apr 3, 2021 at 19:34
Add a comment  | 

1 Answer 1

Reset to default 0

So I was able to get the result I am looking for but in so doing I have an error in the php error log. I added the following code in the section I showed above where I thought needed to be set. The code is as follows.

        $items = $this->order->get_items();
        // Line items can be added to a Transaction individually or as an array
        $lineItems = array();
        $itemcount = 0;
        foreach($items as $item_id => $item_data) {
            array_push($lineItems, new ConnectLineItem);
            ***$product_variation_id = $item_data['variation_id'];
            if ($product_variation_id) {
              $product = wc_get_product($item_data['variation_id']);}
              else {
              $product = wc_get_product($item_data['product_id']); }***  
            $lineItems[$itemcount]->ProductCode = $product->get_sku();
            $lineItems[$itemcount]->Category = "";
            $lineItems[$itemcount]->Description = substr($item_data['name'], 0, 50);
            $lineItems[$itemcount]->Quantity = $item_data['quantity'];
            $lineItems[$itemcount]->UnitOfMeasure = "PCE";
            $lineItems[$itemcount]->LineAmount = $item_data['total'];
            $lineItems[$itemcount]->UnitPrice = $item_data['total'] / $item_data['quantity'];
            $lineItems[$itemcount]->LineTaxAmount = $item_data['subtotal_tax'];
            $lineItems[$itemcount]->LineDiscountAmount = "0.00";
            $itemcount = $itemcount + 1;

The error message I get is this: [06-Apr-2021 21:24:16 UTC] status was called incorrectly. Order properties should not be accessed directly. Backtrace: require('wp-blog-header.php'), require_once('wp-includes/template-loader.php'), do_action('template_redirect'), WP_Hook->do_action, WP_Hook->apply_filters, WC_AJAX::do_wc_ajax, do_action('wc_ajax_checkout'), WP_Hook->do_action, WP_Hook->apply_filters, WC_AJAX::checkout, WC_Checkout->process_checkout, WC_Checkout->process_order_payment, WC_CL_CONNECT_Gateway->process_payment, WC_CL_CONNECT_Gateway->do_order_complete_tasks, WC_Abstract_Legacy_Order->__get, wc_doing_it_wrong. This message was added in version 3.0.

How do I resolve this one.

本文标签: phpVariant Item SKU as Product Code