• Resolved chelseacarson08

    (@chelseacarson08)


    Hi there
    I am trying to add a filter to the order description so that I can identify in Stripe either a product name, SKU or type for coding. This is so that I can tell the difference between orders for subscription products and orders for regular products.

    I have this so far:

    add_filter('wc_stripe_payment_intent_args', function($args, $order){
    	
    	$args['description'] = sprintf('Order %s %s %s', $order->get_order_number(), $order->get_billing_last_name(), $order->get_items());
        return $args;
    
    },10, 2);

    But all it prints is the Order ID, Last Name and the word ‘Array’.

    I’ve searched and know the $order param is a WC_Order class and that I should be able to get the product and get the sku.

    For example I found this:

    foreach ( $order->get_items() as $item_id => $item ) {
    	$product_type = $item->get_type();
    }

    But can’t work out how to use it. Are you able to help?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter chelseacarson08

    (@chelseacarson08)

    I appear to have solved this with the below. Is this the best approach?

    add_filter('wc_stripe_payment_intent_args', function($args, $order){
    
        $items = $order->get_items();
    
        // Loop through ordered items
        foreach ($items as $item) {
        $product_id = $item['product_id'];
        $product = new WC_Product($item['product_id']);
        $sku = $product->get_sku();
        }
        
        $args['description'] = sprintf('Order %s %s %s', $order->get_order_number(), $order->get_billing_last_name(), $product->get_sku());
        return $args;
    
    },10, 2);
    Plugin Author Payment Plugins

    (@mrclayton)

    Hi @chelseacarson08

    That will work but only if you have one product. Notice you are assigning the SKU variable in your loop but then you are creating the description outside of the loop which means you will only ever see the last SKU assigned in the loop.

    My recommendation is you create a comma separated string that consists of the SKUs and then you can assign that outside of the loop.

    Thread Starter chelseacarson08

    (@chelseacarson08)

    Ah yes I see. I don’t suppose you can advise on how to do that?

    Plugin Author Payment Plugins

    (@mrclayton)

    add_filter('wc_stripe_payment_intent_args', function($args, $order){
    
        $items = $order->get_items();
        $skus = [];
        // Loop through ordered items
        foreach ($items as $item) {
        $product_id = $item['product_id'];
        $product = new WC_Product($item['product_id']);
        $skus[] = $product->get_sku();
        }
        
        $args['description'] = sprintf('Order %s %s %s', $order->get_order_number(), $order->get_billing_last_name(), implode(',', $skus));
        return $args;
    
    },10, 2);
    Thread Starter chelseacarson08

    (@chelseacarson08)

    Thank you!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Product type or sku in order description’ is closed to new replies.