| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- <?php
- /**
- * Product export class
- *
- * Handles product export to CSV files
- *
- * @since 1.4.0
- * @package Studiou_WC_Product_Cat_Manage
- * @subpackage Studiou_WC_Product_Cat_Manage/includes
- */
- // If this file is called directly, abort.
- if (!defined('WPINC')) {
- die;
- }
- class Studiou_WC_Product_Manage_Product_Export {
- /**
- * @var Studiou_WC_Product_Manage_Product_DB
- */
- private $db;
- /**
- * Constructor
- *
- * @param Studiou_WC_Product_Manage_Product_DB $db Database instance
- */
- public function __construct($db) {
- $this->db = $db;
- }
- /**
- * Export products by category IDs
- *
- * @param array $category_ids Array of category IDs
- * @return string CSV content
- */
- public function export_products($category_ids) {
- $product_ids = $this->db->get_products_by_categories($category_ids);
- if (empty($product_ids)) {
- return '';
- }
- // Define CSV header
- $header = array(
- 'ID', 'Typ', 'Katalogové číslo', 'Jméno', 'Nadřazené', 'Krátký popis', 'Popis',
- 'Název 1 vlastnosti', 'Hodnota(y) 1 vlastnosti', 'Kategorie', 'Obrázky', 'Běžná cena',
- 'Vlastnost 1 viditelnost', 'Vlastnost 1 globální', 'Vlastnost 1 varianta',
- 'Povolit zákaznické recenze?', 'Minimum Quantity', 'Maximum Quantity'
- );
- // Start CSV output
- $csv = '';
- $csv .= '"' . implode('","', $header) . '"' . "\n";
- // Process each product
- foreach ($product_ids as $product_id) {
- $product = wc_get_product($product_id);
- if (!$product) {
- continue;
- }
- // Only export variable products and variations
- if (!$product->is_type('variable') && !$product->is_type('variation')) {
- continue;
- }
- $csv .= $this->format_product_row($product);
- }
- return $csv;
- }
- /**
- * Format product data as CSV row
- *
- * @param WC_Product $product Product object
- * @return string CSV row
- */
- private function format_product_row($product) {
- $row = array();
- // ID
- $row[] = $product->get_id();
- // Typ (Type)
- $row[] = $product->is_type('variation') ? 'variation' : 'variable';
- // Katalogové číslo (SKU)
- $row[] = $product->get_sku();
- // Jméno (Name)
- $row[] = $product->get_name();
- // Nadřazené (Parent) - only for variations
- if ($product->is_type('variation')) {
- $parent_id = $product->get_parent_id();
- if ($parent_id) {
- $parent = wc_get_product($parent_id);
- $row[] = $parent ? $parent->get_sku() : '';
- } else {
- $row[] = '';
- }
- } else {
- $row[] = '';
- }
- // Krátký popis (Short description)
- $row[] = $product->get_short_description();
- // Popis (Description)
- $row[] = $product->get_description();
- // Název 1 vlastnosti (Attribute 1 name)
- // Hodnota(y) 1 vlastnosti (Attribute 1 value(s))
- $attributes = $product->get_attributes();
- $attribute_name = '';
- $attribute_values = '';
- $attribute_visible = '0';
- $attribute_global = '0';
- $attribute_variation = '0';
- if (!empty($attributes)) {
- $first_attribute = reset($attributes);
- if ($first_attribute instanceof WC_Product_Attribute) {
- $attribute_name = $first_attribute->get_name();
- // Remove 'pa_' prefix if present
- if (strpos($attribute_name, 'pa_') === 0) {
- $attribute_name = substr($attribute_name, 3);
- }
- // Get attribute values
- if ($product->is_type('variation')) {
- // For variations, get the specific value
- $variation_attributes = $product->get_variation_attributes();
- if (!empty($variation_attributes)) {
- $attribute_values = reset($variation_attributes);
- }
- } else {
- // For variable products, get all options
- $options = $first_attribute->get_options();
- if (is_array($options)) {
- $attribute_values = implode('|', $options);
- }
- }
- $attribute_visible = $first_attribute->get_visible() ? '1' : '0';
- $attribute_global = $first_attribute->is_taxonomy() ? '1' : '0';
- $attribute_variation = $first_attribute->get_variation() ? '1' : '0';
- }
- }
- $row[] = $attribute_name;
- $row[] = $attribute_values;
- // Kategorie (Categories) - with full hierarchy for subcategories
- $category_ids = $product->get_category_ids();
- $category_names = array();
- foreach ($category_ids as $cat_id) {
- $category_path = $this->get_category_path($cat_id);
- if ($category_path) {
- $category_names[] = $category_path;
- }
- }
- $row[] = implode('|', $category_names);
- // Obrázky (Images)
- $image_id = $product->get_image_id();
- $row[] = $image_id ? wp_get_attachment_url($image_id) : '';
- // Běžná cena (Regular price)
- $row[] = $product->get_regular_price();
- // Vlastnost 1 viditelnost (Attribute 1 visibility)
- $row[] = $attribute_visible;
- // Vlastnost 1 globální (Attribute 1 global)
- $row[] = $attribute_global;
- // Vlastnost 1 varianta (Attribute 1 variation)
- $row[] = $attribute_variation;
- // Povolit zákaznické recenze? (Enable reviews)
- $row[] = $product->get_reviews_allowed() ? '1' : '0';
- // Minimum Quantity
- $min_qty = $product->get_meta('_minimum_quantity');
- $row[] = $min_qty ? $min_qty : '';
- // Maximum Quantity
- $max_qty = $product->get_meta('_maximum_quantity');
- $row[] = $max_qty ? $max_qty : '';
- // Format as CSV row
- $formatted_row = array();
- foreach ($row as $value) {
- // Escape double quotes
- $value = str_replace('"', '""', $value);
- $formatted_row[] = '"' . $value . '"';
- }
- return implode(',', $formatted_row) . "\n";
- }
- /**
- * Get full category path (Parent > Child format for subcategories)
- *
- * @param int $category_id Category term ID
- * @return string Category path
- */
- private function get_category_path($category_id) {
- $term = get_term($category_id, 'product_cat');
- if (!$term || is_wp_error($term)) {
- return '';
- }
- // Build path from child to root
- $path = array($term->name);
- $current_term = $term;
- while ($current_term->parent != 0) {
- $parent_term = get_term($current_term->parent, 'product_cat');
- if (!$parent_term || is_wp_error($parent_term)) {
- break;
- }
- array_unshift($path, $parent_term->name);
- $current_term = $parent_term;
- }
- // Return full path with ">" separator if subcategory, otherwise just the name
- return count($path) > 1 ? implode(' > ', $path) : $path[0];
- }
- /**
- * Generate CSV file and return download URL
- *
- * @param array $category_ids Array of category IDs
- * @return array Result with download URL or error message
- */
- public function generate_export_file($category_ids) {
- $csv_content = $this->export_products($category_ids);
- if (empty($csv_content)) {
- return array(
- 'success' => false,
- 'message' => __('No products found in selected categories', 'studiou-wc-product-cat-manage')
- );
- }
- // Create uploads directory if it doesn't exist
- $upload_dir = wp_upload_dir();
- $export_dir = $upload_dir['basedir'] . '/studiou-wc-product-exports';
- if (!file_exists($export_dir)) {
- wp_mkdir_p($export_dir);
- }
- // Generate filename with timestamp
- $filename = 'product-export-' . date('Y-m-d-H-i-s') . '.csv';
- $file_path = $export_dir . '/' . $filename;
- // Write CSV content to file
- $result = file_put_contents($file_path, $csv_content);
- if ($result === false) {
- return array(
- 'success' => false,
- 'message' => __('Failed to create export file', 'studiou-wc-product-cat-manage')
- );
- }
- // Generate download URL
- $download_url = $upload_dir['baseurl'] . '/studiou-wc-product-exports/' . $filename;
- return array(
- 'success' => true,
- 'message' => sprintf(
- __('Export successful! %d products exported.', 'studiou-wc-product-cat-manage'),
- substr_count($csv_content, "\n") - 1 // Subtract header row
- ),
- 'download_url' => $download_url
- );
- }
- }
|