Advertisement
nshelper

Untitled

May 8th, 2024
454
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.70 KB | None | 0 0
  1.     $orderItems = $order->get_items() + $order->get_items('fee');
  2.     foreach ($orderItems as $itemId => $item) {
  3.         do_action( 'woocommerce/cart_loop/start', $item );
  4.         if ($onWoocommerceV3OrNewer) {
  5.             $product = is_callable(array($item, 'get_product')) ? $item->get_product() : null;
  6.         }
  7.         else {
  8.             $product = $order->get_product_from_item($item);
  9.         }
  10.  
  11.         if ($includeOrdersWithOnlyVirtualItems) {
  12.             $orderHasAtleastOneItem = true;
  13.         }
  14.         // skip items that don't require shipping
  15.         else if ((!$product || !$product->needs_shipping()) && 'fee' !== $item['type']) {
  16.             do_action( 'woocommerce/cart_loop/end', $item );
  17.             continue;
  18.         }
  19.        
  20.         $orderHasAtleastOneItem = true;
  21.  
  22.         $webshipItem = array(
  23.             'lineId' => (string) $itemId,
  24.         );
  25.  
  26.         if ('fee' === $item['type']) {
  27.             $webshipItem = array_merge($webshipItem, array(
  28.                 'title' => $onWoocommerceV3OrNewer ? $item->get_name() : $item['name'],
  29.                 'quantity' => (string) 1,
  30.                 'price' => $order->get_item_total($item, false, true)
  31.             ));
  32.         }
  33.  
  34.         if ($product && $product->needs_shipping()) {
  35.             $imageId = $product->get_image_id();
  36.             $imgUrl = $imageId ? current(wp_get_attachment_image_src($imageId, 'shop_thumbnail')) : '';
  37.            
  38.             if (method_exists($product, 'get_id')) {
  39.                 $productId = $product->get_id();
  40.             }
  41.             else {
  42.                 $productId = $product->id;
  43.             }
  44.  
  45.             $webshipItem = array_merge($webshipItem, array(
  46.                 'productId' => (string) $productId,
  47.                 'sku' => $product->get_sku(),
  48.                 'title' => $product->get_title(),
  49.                 'imgUrl' => $imgUrl,
  50.                 'shippingWeight' => (string) wc_get_weight($product->get_weight(), 'lbs'),
  51.                 'quantity' => (string) $item['qty'],
  52.                 'price' => $order->get_item_subtotal($item, false, true),
  53.                 'productLength' => $product->get_length(),
  54.                 'productWidth' => $product->get_width(),
  55.                 'productHeight' => $product->get_height(),
  56.                 'url' => $product->get_permalink()
  57.             ));
  58.         }
  59.  
  60.         if ($item['item_meta']) {
  61.             if (version_compare(WC_VERSION, '3.0.0', '<')) {
  62.                 $itemMeta = new WC_Order_Item_Meta($item, $product);
  63.                 $formattedMeta = $itemMeta->get_formatted('_');
  64.             }
  65.             else {
  66.                 add_filter('woocommerce_is_attribute_in_product_name', '__return_false');
  67.                 $formattedMeta = $item->get_formatted_meta_data();
  68.             }
  69.  
  70.             if (!empty($formattedMeta)) {
  71.                 $attributes = array();
  72.  
  73.                 foreach ($formattedMeta as $metaKey => $meta) {
  74.                     if (version_compare(WC_VERSION, '3.0.0', '<')) {
  75.                         array_push($attributes, array(
  76.                             'name' => $meta['label'],
  77.                             'value' => $meta['value']
  78.                         ));
  79.                     }
  80.                     else {
  81.                         array_push($attributes, array(
  82.                             'name' => $meta->display_key,
  83.                             'value' => wp_strip_all_tags($meta->display_value)
  84.                         ));
  85.                     }
  86.                 }
  87.  
  88.                 $webshipItem['attributes'] = $attributes;
  89.             }
  90.         }
  91.        
  92.         array_push($webshipOrder['items'], $webshipItem);
  93.         do_action( 'woocommerce/cart_loop/end', $item );
  94.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement