• Resolved ddelvin

    (@ddelvin)


    Hey there,

    I created SKU’s for my attribute stock plugin items. I am trying to get all users that have purchased an item by SKU, using $order = wc_get_order($id); however, it is only getting the SKU from the product, or product variation. Is there a different way that I can get all users that purchased a specific SKU?

Viewing 1 replies (of 1 total)
  • Plugin Author Mewz

    (@mewz)

    It’s a bit of a process. You first need to get the order items from the order, then match them to attribute stock items one by one. From there it depends what exactly you need to do with them.

    The code would look something like this:

    $order = wc_get_order($id);
    $stock_items = [];
    
    /** @var \WC_Order_Item_Product $order_item */
    foreach ($order->get_items() as $order_item) {
    	$product = $order_item->get_product();
    	$attributes = \Mewz\WCAS\Util\Orders::get_order_item_attributes($order_item, $product);
    	$matches = mewz_wcas_match_product_stock($product, $attributes);
    
    	foreach ($matches as $match) {
    		$stock_id = $match['stock_id'];
    
    		if (!isset($stock_items[$stock_id])) {
    			$stock_items[$stock_id] = mewz_wcas_get_stock($stock_id);
    		}
    	}
    }
    
    foreach ($stock_items as $stock) {
    	echo $stock->sku();
    }
Viewing 1 replies (of 1 total)
  • The topic ‘getting attribute stock sku’ is closed to new replies.