Get current order's SKU and keep it in database

Viewed 33

Time to time we change SKU in a product. But we want to keep old SKU in order tables. Woocommerce change old orders' SKU's one you update the product.

So we had below code and was working great. But after new updates it doesn't work anymore. What can we do?

add_action('woocommerce_thankyou', 'add_current_sku', 4);

function add_current_sku($order_id)
{
    $order = wc_get_order($order_id);
    $product = $order->get_items();
    $skus = array();
    global $wpdb;
    foreach ($order->get_items() as $item_id => $item) {
        $product = $item->get_product();
        $product_id = $item->get_product_id();
        if ($product->is_type('composite')) {
            $skus[] = array(
                "sku" => $product->sku,
                "product_id" => $product_id,
            );
        }
    }
    update_post_meta($order_id, "erp_productcode", $skus);
}
0 Answers
Related