class-studiou-wc-product-manage-product-export.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. /**
  3. * Product export class
  4. *
  5. * Handles product export to CSV files
  6. *
  7. * @since 1.4.0
  8. * @package Studiou_WC_Product_Cat_Manage
  9. * @subpackage Studiou_WC_Product_Cat_Manage/includes
  10. */
  11. // If this file is called directly, abort.
  12. if (!defined('WPINC')) {
  13. die;
  14. }
  15. class Studiou_WC_Product_Manage_Product_Export {
  16. /**
  17. * @var Studiou_WC_Product_Manage_Product_DB
  18. */
  19. private $db;
  20. /**
  21. * Constructor
  22. *
  23. * @param Studiou_WC_Product_Manage_Product_DB $db Database instance
  24. */
  25. public function __construct($db) {
  26. $this->db = $db;
  27. }
  28. /**
  29. * Export products by category IDs
  30. *
  31. * @param array $category_ids Array of category IDs
  32. * @return string CSV content
  33. */
  34. public function export_products($category_ids) {
  35. $product_ids = $this->db->get_products_by_categories($category_ids);
  36. if (empty($product_ids)) {
  37. return '';
  38. }
  39. // Define CSV header
  40. $header = array(
  41. 'ID', 'Typ', 'Katalogové číslo', 'Jméno', 'Nadřazené', 'Krátký popis', 'Popis',
  42. 'Název 1 vlastnosti', 'Hodnota(y) 1 vlastnosti', 'Kategorie', 'Obrázky', 'Běžná cena',
  43. 'Vlastnost 1 viditelnost', 'Vlastnost 1 globální', 'Vlastnost 1 varianta',
  44. 'Povolit zákaznické recenze?', 'Minimum Quantity', 'Maximum Quantity'
  45. );
  46. // Start CSV output
  47. $csv = '';
  48. $csv .= '"' . implode('","', $header) . '"' . "\n";
  49. // Process each product
  50. foreach ($product_ids as $product_id) {
  51. $product = wc_get_product($product_id);
  52. if (!$product) {
  53. continue;
  54. }
  55. // Only export variable products and variations
  56. if (!$product->is_type('variable') && !$product->is_type('variation')) {
  57. continue;
  58. }
  59. $csv .= $this->format_product_row($product);
  60. }
  61. return $csv;
  62. }
  63. /**
  64. * Format product data as CSV row
  65. *
  66. * @param WC_Product $product Product object
  67. * @return string CSV row
  68. */
  69. private function format_product_row($product) {
  70. $row = array();
  71. // ID
  72. $row[] = $product->get_id();
  73. // Typ (Type)
  74. $row[] = $product->is_type('variation') ? 'variation' : 'variable';
  75. // Katalogové číslo (SKU)
  76. $row[] = $product->get_sku();
  77. // Jméno (Name)
  78. $row[] = $product->get_name();
  79. // Nadřazené (Parent) - only for variations
  80. if ($product->is_type('variation')) {
  81. $parent_id = $product->get_parent_id();
  82. if ($parent_id) {
  83. $parent = wc_get_product($parent_id);
  84. $row[] = $parent ? $parent->get_sku() : '';
  85. } else {
  86. $row[] = '';
  87. }
  88. } else {
  89. $row[] = '';
  90. }
  91. // Krátký popis (Short description)
  92. $row[] = $product->get_short_description();
  93. // Popis (Description)
  94. $row[] = $product->get_description();
  95. // Název 1 vlastnosti (Attribute 1 name)
  96. // Hodnota(y) 1 vlastnosti (Attribute 1 value(s))
  97. $attributes = $product->get_attributes();
  98. $attribute_name = '';
  99. $attribute_values = '';
  100. $attribute_visible = '0';
  101. $attribute_global = '0';
  102. $attribute_variation = '0';
  103. if (!empty($attributes)) {
  104. $first_attribute = reset($attributes);
  105. if ($first_attribute instanceof WC_Product_Attribute) {
  106. $attribute_name = $first_attribute->get_name();
  107. // Remove 'pa_' prefix if present
  108. if (strpos($attribute_name, 'pa_') === 0) {
  109. $attribute_name = substr($attribute_name, 3);
  110. }
  111. // Get attribute values
  112. if ($product->is_type('variation')) {
  113. // For variations, get the specific value
  114. $variation_attributes = $product->get_variation_attributes();
  115. if (!empty($variation_attributes)) {
  116. $attribute_values = reset($variation_attributes);
  117. }
  118. } else {
  119. // For variable products, get all options
  120. $options = $first_attribute->get_options();
  121. if (is_array($options)) {
  122. $attribute_values = implode('|', $options);
  123. }
  124. }
  125. $attribute_visible = $first_attribute->get_visible() ? '1' : '0';
  126. $attribute_global = $first_attribute->is_taxonomy() ? '1' : '0';
  127. $attribute_variation = $first_attribute->get_variation() ? '1' : '0';
  128. }
  129. }
  130. $row[] = $attribute_name;
  131. $row[] = $attribute_values;
  132. // Kategorie (Categories) - with full hierarchy for subcategories
  133. $category_ids = $product->get_category_ids();
  134. $category_names = array();
  135. foreach ($category_ids as $cat_id) {
  136. $category_path = $this->get_category_path($cat_id);
  137. if ($category_path) {
  138. $category_names[] = $category_path;
  139. }
  140. }
  141. $row[] = implode('|', $category_names);
  142. // Obrázky (Images)
  143. $image_id = $product->get_image_id();
  144. $row[] = $image_id ? wp_get_attachment_url($image_id) : '';
  145. // Běžná cena (Regular price)
  146. $row[] = $product->get_regular_price();
  147. // Vlastnost 1 viditelnost (Attribute 1 visibility)
  148. $row[] = $attribute_visible;
  149. // Vlastnost 1 globální (Attribute 1 global)
  150. $row[] = $attribute_global;
  151. // Vlastnost 1 varianta (Attribute 1 variation)
  152. $row[] = $attribute_variation;
  153. // Povolit zákaznické recenze? (Enable reviews)
  154. $row[] = $product->get_reviews_allowed() ? '1' : '0';
  155. // Minimum Quantity
  156. $min_qty = $product->get_meta('_minimum_quantity');
  157. $row[] = $min_qty ? $min_qty : '';
  158. // Maximum Quantity
  159. $max_qty = $product->get_meta('_maximum_quantity');
  160. $row[] = $max_qty ? $max_qty : '';
  161. // Format as CSV row
  162. $formatted_row = array();
  163. foreach ($row as $value) {
  164. // Escape double quotes
  165. $value = str_replace('"', '""', $value);
  166. $formatted_row[] = '"' . $value . '"';
  167. }
  168. return implode(',', $formatted_row) . "\n";
  169. }
  170. /**
  171. * Get full category path (Parent > Child format for subcategories)
  172. *
  173. * @param int $category_id Category term ID
  174. * @return string Category path
  175. */
  176. private function get_category_path($category_id) {
  177. $term = get_term($category_id, 'product_cat');
  178. if (!$term || is_wp_error($term)) {
  179. return '';
  180. }
  181. // Build path from child to root
  182. $path = array($term->name);
  183. $current_term = $term;
  184. while ($current_term->parent != 0) {
  185. $parent_term = get_term($current_term->parent, 'product_cat');
  186. if (!$parent_term || is_wp_error($parent_term)) {
  187. break;
  188. }
  189. array_unshift($path, $parent_term->name);
  190. $current_term = $parent_term;
  191. }
  192. // Return full path with ">" separator if subcategory, otherwise just the name
  193. return count($path) > 1 ? implode(' > ', $path) : $path[0];
  194. }
  195. /**
  196. * Generate CSV file and return download URL
  197. *
  198. * @param array $category_ids Array of category IDs
  199. * @return array Result with download URL or error message
  200. */
  201. public function generate_export_file($category_ids) {
  202. $csv_content = $this->export_products($category_ids);
  203. if (empty($csv_content)) {
  204. return array(
  205. 'success' => false,
  206. 'message' => __('No products found in selected categories', 'studiou-wc-product-cat-manage')
  207. );
  208. }
  209. // Create uploads directory if it doesn't exist
  210. $upload_dir = wp_upload_dir();
  211. $export_dir = $upload_dir['basedir'] . '/studiou-wc-product-exports';
  212. if (!file_exists($export_dir)) {
  213. wp_mkdir_p($export_dir);
  214. }
  215. // Generate filename with timestamp
  216. $filename = 'product-export-' . date('Y-m-d-H-i-s') . '.csv';
  217. $file_path = $export_dir . '/' . $filename;
  218. // Write CSV content to file
  219. $result = file_put_contents($file_path, $csv_content);
  220. if ($result === false) {
  221. return array(
  222. 'success' => false,
  223. 'message' => __('Failed to create export file', 'studiou-wc-product-cat-manage')
  224. );
  225. }
  226. // Generate download URL
  227. $download_url = $upload_dir['baseurl'] . '/studiou-wc-product-exports/' . $filename;
  228. return array(
  229. 'success' => true,
  230. 'message' => sprintf(
  231. __('Export successful! %d products exported.', 'studiou-wc-product-cat-manage'),
  232. substr_count($csv_content, "\n") - 1 // Subtract header row
  233. ),
  234. 'download_url' => $download_url
  235. );
  236. }
  237. }