| @@ -289,49 +289,9 @@ | ||
| 289 | 289 | throw new \RuntimeException('wc_create_order did not return a valid WC_Order instance.'); |
| 290 | 290 | } |
| 291 | 291 | if (isset($data['items']) && is_array($data['items'])) { |
| 292 | 292 | foreach ($data['items'] as $item) { |
| 293 | - $sync_basalam_product_id = $item['product']['id'] ?? null; | |
| 294 | - $quantity = $item['quantity'] ?? 1; | |
| 295 | - $item_id = $item['id'] ?? null; | |
| 296 | - | |
| 297 | - if ($sync_basalam_product_id) { | |
| 298 | - try { | |
| 299 | - if (!empty($item['variation']['id'])) { | |
| 300 | - $woo_product_id = self::getWooProductVariableId($item['variation']['id']); | |
| 301 | - } else { | |
| 302 | - $woo_product_id = self::getWooProductSimpleId($sync_basalam_product_id); | |
| 303 | - } | |
| 304 | - | |
| 305 | - if ($woo_product_id) { | |
| 306 | - $product = wc_get_product($woo_product_id); | |
| 307 | - if ($product) { | |
| 308 | - $order_item_id = $order->add_product($product, $quantity); | |
| 309 | - if ($item_id && $order_item_id) { | |
| 310 | - $order->update_meta_data('_sync_basalam_item_id_' . $order_item_id, $item_id); | |
| 311 | - } | |
| 312 | - | |
| 313 | - self::set_item_price_from_financial_report($order, $order_item_id, $item, $quantity); | |
| 314 | - } | |
| 315 | - } else { | |
| 316 | - $placeholder_product_id = self::getPlaceholderProductId(); | |
| 317 | - if ($placeholder_product_id) { | |
| 318 | - $placeholder_product = wc_get_product($placeholder_product_id); | |
| 319 | - if ($placeholder_product) { | |
| 320 | - $order_item_id = $order->add_product($placeholder_product, $quantity); | |
| 321 | - | |
| 322 | - if ($item_id && $order_item_id) { | |
| 323 | - $order->update_meta_data('_sync_basalam_item_id_' . $order_item_id, $item_id); | |
| 324 | - } | |
| 325 | - | |
| 326 | - self::set_item_price_from_financial_report($order, $order_item_id, $item, $quantity); | |
| 327 | - } | |
| 328 | - } | |
| 329 | - } | |
| 330 | - } catch (\Exception $e) { | |
| 331 | - Logger::error('خطا در ایجاد سفارش: ' . $e->getMessage()); | |
| 332 | - } | |
| 333 | - } | |
| 293 | + self::addBasalamItemToOrder($order, $item); | |
| 334 | 294 | } |
| 335 | 295 | } |
| 336 | 296 | |
| 337 | 297 | if (isset($data['customer_data']['recipient']) && is_array($data['customer_data']['recipient'])) { |
| @@ -697,8 +657,112 @@ | ||
| 697 | 657 | ) |
| 698 | 658 | ); |
| 699 | 659 | |
| 700 | 660 | return $product_id ? $product_id : false; |
| 661 | + } | |
| 662 | + | |
| 663 | + public const BASALAM_ITEM_NAME_META_KEY = 'نام محصول در باسلام'; | |
| 664 | + public const BASALAM_ITEM_LINK_META_KEY = 'لینک محصول در باسلام'; | |
| 665 | + | |
| 666 | + public static function addBasalamItemToOrder(\WC_Order $order, array $item) | |
| 667 | + { | |
| 668 | + $sync_basalam_product_id = $item['product']['id'] ?? null; | |
| 669 | + $quantity = $item['quantity'] ?? 1; | |
| 670 | + $item_id = $item['id'] ?? null; | |
| 671 | + | |
| 672 | + if (!$sync_basalam_product_id) { | |
| 673 | + return; | |
| 674 | + } | |
| 675 | + | |
| 676 | + try { | |
| 677 | + $basalam_item_name = self::extractBasalamItemName($item); | |
| 678 | + | |
| 679 | + if (!empty($item['variation']['id'])) { | |
| 680 | + $woo_product_id = self::getWooProductVariableId($item['variation']['id']); | |
| 681 | + } else { | |
| 682 | + $woo_product_id = self::getWooProductSimpleId($sync_basalam_product_id); | |
| 683 | + } | |
| 684 | + | |
| 685 | + $product = null; | |
| 686 | + | |
| 687 | + if ($woo_product_id) { | |
| 688 | + $product = wc_get_product($woo_product_id); | |
| 689 | + } | |
| 690 | + | |
| 691 | + | |
| 692 | + | |
| 693 | + if (!$product) { | |
| 694 | + $placeholder_product_id = self::getPlaceholderProductId(); | |
| 695 | + | |
| 696 | + if ($placeholder_product_id) { | |
| 697 | + $product = wc_get_product($placeholder_product_id); | |
| 698 | + } | |
| 699 | + } | |
| 700 | + | |
| 701 | + if (!$product) { | |
| 702 | + return; | |
| 703 | + } | |
| 704 | + | |
| 705 | + $order_item_id = $order->add_product($product, $quantity); | |
| 706 | + | |
| 707 | + if ($item_id && $order_item_id) { | |
| 708 | + $order->update_meta_data('_sync_basalam_item_id_' . $order_item_id, $item_id); | |
| 709 | + } | |
| 710 | + | |
| 711 | + self::setBasalamItemNameOnOrderItem($order, $order_item_id, $basalam_item_name, $sync_basalam_product_id); | |
| 712 | + self::set_item_price_from_financial_report($order, $order_item_id, $item, $quantity); | |
| 713 | + } catch (\Exception $e) { | |
| 714 | + Logger::error('خطا در ایجاد سفارش: ' . $e->getMessage()); | |
| 715 | + } | |
| 716 | + } | |
| 717 | + | |
| 718 | + public static function extractBasalamItemName(array $item): string | |
| 719 | + { | |
| 720 | + $product = is_array($item['product'] ?? null) ? $item['product'] : []; | |
| 721 | + $productTitle = trim((string) ($product['title'] ?? ($product['name'] ?? ''))); | |
| 722 | + | |
| 723 | + if ($productTitle === '') { | |
| 724 | + return ''; | |
| 725 | + } | |
| 726 | + | |
| 727 | + $variationTitle = ''; | |
| 728 | + if (is_array($item['variation'] ?? null)) { | |
| 729 | + $variationTitle = trim((string) ($item['variation']['title'] ?? ($item['variation']['name'] ?? ''))); | |
| 730 | + } | |
| 731 | + | |
| 732 | + if ($variationTitle !== '' && mb_stripos($productTitle, $variationTitle) === false) { | |
| 733 | + return $productTitle . ' - ' . $variationTitle; | |
| 734 | + } | |
| 735 | + | |
| 736 | + return $productTitle; | |
| 737 | + } | |
| 738 | + | |
| 739 | + public static function setBasalamItemNameOnOrderItem($order, $order_item_id, $basalam_item_name, $basalam_product_id = null) | |
| 740 | + { | |
| 741 | + $basalam_item_name = trim((string) $basalam_item_name); | |
| 742 | + | |
| 743 | + if (empty($order_item_id) || $basalam_item_name === '' || !$order instanceof \WC_Order) { | |
| 744 | + return false; | |
| 745 | + } | |
| 746 | + | |
| 747 | + $order_item = $order->get_item($order_item_id); | |
| 748 | + if (!$order_item instanceof \WC_Order_Item_Product) { | |
| 749 | + return false; | |
| 750 | + } | |
| 751 | + | |
| 752 | + $order_item->update_meta_data(self::BASALAM_ITEM_NAME_META_KEY, $basalam_item_name); | |
| 753 | + | |
| 754 | + if ($basalam_product_id) { | |
| 755 | + $link = 'https://basalam.com/p/' . (int) $basalam_product_id; | |
| 756 | + $order_item->update_meta_data( | |
| 757 | + self::BASALAM_ITEM_LINK_META_KEY, | |
| 758 | + '<a href="' . esc_url($link) . '" target="_blank" rel="noopener">' . esc_html($basalam_item_name) . '</a>' | |
| 759 | + ); | |
| 760 | + } | |
| 761 | + | |
| 762 | + $order_item->save(); | |
| 763 | + | |
| 764 | + return true; | |
| 701 | 765 | } |
| 702 | 766 | |
| 703 | 767 | private static function set_item_price_from_financial_report($order, $order_item_id, $item, $quantity) |
| 704 | 768 | { |