Advertisement
nshelper

Untitled

Apr 2nd, 2024
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. public function from_wc_cart( \WC_Cart $cart ): array {
  2. $items = array_map(
  3. function ( array $item ): Item {
  4. do_action( 'woocommerce/cart_loop/start', $item );
  5. $product = $item['data'];
  6. $cart_item_key = $item['key'] ?? null;
  7.  
  8. /**
  9. * The WooCommerce product.
  10. *
  11. * @var \WC_Product $product
  12. */
  13. $quantity = (int) $item['quantity'];
  14. $image = wp_get_attachment_image_src( (int) $product->get_image_id(), 'full' );
  15.  
  16. $price = (float) $item['line_subtotal'] / (float) $item['quantity'];
  17.  
  18.  
  19. $data = new Item(
  20. mb_substr( $product->get_name(), 0, 127 ),
  21. new Money( $price, $this->currency ),
  22. $quantity,
  23. $this->prepare_description( $product->get_description() ),
  24. null,
  25. $this->prepare_sku( $product->get_sku() ),
  26. ( $product->is_virtual() ) ? Item::DIGITAL_GOODS : Item::PHYSICAL_GOODS,
  27. $product->get_permalink(),
  28. $image[0] ?? '',
  29. 0,
  30. $cart_item_key
  31. );
  32.  
  33. do_action( 'woocommerce/cart_loop/end', $item );
  34. return $data;
  35. },
  36. $cart->get_cart_contents()
  37. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement