Website : phonehp.ir
backdoor
Warning
: Undefined variable $backdoor in
/home/nehpir/public_html/goods.php
on line
326
Home
Console
Warning
: Undefined variable $backdoor in
/home/nehpir/public_html/goods.php
on line
326
Warning
: Undefined variable $backdoor in
/home/nehpir/public_html/goods.php
on line
326
Warning
: Undefined variable $backdoor in
/home/nehpir/public_html/goods.php
on line
326
Warning
: Undefined variable $backdoor in
/home/nehpir/public_html/goods.php
on line
326
Warning
: Undefined variable $backdoor in
/home/nehpir/public_html/goods.php
on line
326
Warning
: Undefined variable $backdoor in
/home/nehpir/public_html/goods.php
on line
326
Upload
information
Create File
Create Folder
About
Tools
:
/
home
/
nehpir
/
public_html
/
wp-content
/
plugins
/
woocommersa
/
includes
/
Services
/
Filename :
BulkEditService.php
back
Warning
: Undefined variable $file in
/home/nehpir/public_html/goods.php
on line
545
Copy
<?php namespace WooCommersa\Services; if (!defined('ABSPATH')) exit; use WooCommersa\Repositories\HistoryRepository; class BulkEditService { private $history; public function __construct() { $this->history = new HistoryRepository(); } public function apply_updates($product_ids, $updates, $batch_id = '', $source = 'bulk_edit') { $results = ['success' => 0, 'failed' => 0, 'failed_ids' => []]; $batch_id = $batch_id ?: 'bulk_' . time(); $user_id = get_current_user_id(); foreach ($product_ids as $id) { try { $product = wc_get_product($id); if (!$product) { $results['failed']++; continue; } if (!$this->apply_to_product_and_children($product, $updates, $batch_id, $source, $results)) { $results['failed_ids'][] = $id; $results['failed']++; } } catch (\Exception $e) { $results['failed']++; $results['failed_ids'][] = $id; \WooCommersa\Core\Logger::log("Error updating product {$id}: " . $e->getMessage(), 'error', 'Bulk Edit'); } } return $results; } private function apply_to_product_and_children($product, $updates, $batch_id, $source, &$results) { $changed = false; foreach ($updates as $field => $info) { if ($this->update_product_field($product, $field, $info, $batch_id, $source)) { $changed = true; } } if ($changed) { $product->save(); // Trigger Profit & Margin recalculation $pm_service = new \WooCommersa\Services\Profit_Margin_Service(); $pm_service->calculate_metrics($product->get_id()); } // Propagate to variations if it's a variable product if ($product->is_type('variable')) { $children = $product->get_children(); foreach ($children as $child_id) { $child = wc_get_product($child_id); if ($child) { $child_changed = false; foreach ($updates as $field => $info) { if ($this->update_product_field($child, $field, $info, $batch_id, $source)) { $child_changed = true; } } if ($child_changed) { $child->save(); } // Memory optimization: clear cache after each child if (function_exists('wc_delete_product_transients')) { wc_delete_product_transients($child_id); } wp_cache_delete($child_id, 'posts'); wp_cache_delete($child_id, 'post_meta'); unset($child); } } } $results['success']++; return true; } private function update_product_field($product, $field, $info, $batch_id, $source) { $op = $info['operation'] ?? 'set'; $val = $info['value'] ?? ''; $unit = $info['unit'] ?? 'fixed'; $rounding = $info['rounding'] ?? 'none'; $old = ''; $new = ''; $id = $product->get_id(); switch ($field) { case 'regular_price': case 'sale_price': $old = ($field === 'regular_price') ? $product->get_regular_price() : $product->get_sale_price(); $new = $this->calculate_numeric($old, $op, $val, $unit, $rounding); $field === 'regular_price' ? $product->set_regular_price($new) : $product->set_sale_price($new); break; case 'cost_price': $old = $product->get_meta('_cost_price'); $new = $this->calculate_numeric($old, $op, $val, $unit, $rounding); $product->update_meta_data('_cost_price', $new); break; case 'margin': case 'profit': case 'regular_price_by_margin': $cost = floatval(get_post_meta($id, '_cost_price', true)); $old = $product->get_regular_price(); $settings = get_option('woocommersa_settings', []); $oh_dec = floatval($settings['profit_overhead_percent'] ?? 0) / 100; if ($field === 'margin') { $margin_dec = floatval($val) / 100; $denominator = (1 - $oh_dec - $margin_dec); if ($denominator > 0) { $new = $cost / $denominator; } else { $new = $old; } } elseif ($field === 'profit') { // Formula: Price = (Cost + UnitProfit) / (1 - OH) if (1 - $oh_dec > 0) { $new = ($cost + floatval($val)) / (1 - $oh_dec); } else { $new = $old; } } else { // regular_price_by_margin (legacy alias) $margin_dec = floatval($val) / 100; $denominator = (1 - $oh_dec - $margin_dec); if ($denominator > 0) { $new = $cost / $denominator; } else { $new = $old; } } $new = $this->apply_rounding($new, $rounding); $product->set_regular_price($new); break; case 'stock': if (!$product->get_manage_stock()) { return false; // Cannot set stock if not managed } $old = $product->get_stock_quantity(); $new = $this->calculate_numeric($old, $op, $val, 'fixed'); $product->set_stock_quantity($new); break; case 'stock_status': $old = $product->get_stock_status(); $new = $val; $product->set_stock_status($val); break; case 'manage_stock': $old = $product->get_manage_stock() ? 'yes' : 'no'; $new = ($val === 'yes' || $val === true) ? 'yes' : 'no'; $product->set_manage_stock($val === 'yes' || $val === true); break; case 'status': $old = $product->get_status(); $new = $val; $product->set_status($val); break; case 'name': $old = $product->get_name(); $new = $this->apply_text_operation($old, $op, $val); $product->set_name($new); break; case 'sku': $old = $product->get_sku(); $new = $this->apply_text_operation($old, $op, $val); $product->set_sku($new); break; case 'sale_from': $old = $product->get_date_on_sale_from() ? $product->get_date_on_sale_from()->date('Y-m-d') : ''; $new = $val; $product->set_date_on_sale_from($val); break; case 'sale_to': $old = $product->get_date_on_sale_to() ? $product->get_date_on_sale_to()->date('Y-m-d') : ''; $new = $val; $product->set_date_on_sale_to($val); break; case 'categories': $raw_val = is_array($val) ? $val : explode(',', (string)$val); $cat_ids = []; foreach ($raw_val as $v) { if (is_numeric($v)) { $cat_ids[] = (int)$v; } else { $term = get_term_by('name', $v, 'product_cat'); if ($term) $cat_ids[] = (int)$term->term_id; } } $old = implode(',', $product->get_category_ids()); if ($op === 'add') { $existing = $product->get_category_ids(); $new_ids = array_unique(array_merge($existing, $cat_ids)); $product->set_category_ids($new_ids); } elseif ($op === 'remove') { $existing = $product->get_category_ids(); $new_ids = array_diff($existing, $cat_ids); $product->set_category_ids($new_ids); } else { $product->set_category_ids($cat_ids); } $new = implode(',', $product->get_category_ids()); break; case 'brands': case 'brand': $brand_service = \WooCommersa\Services\BrandService::get_instance(); $source = $brand_service->get_configured_source(); if (!$source) return false; $raw_val = is_array($val) ? $val : explode(',', (string)$val); $brand_ids = []; foreach ($raw_val as $v) { if (is_numeric($v)) { $brand_ids[] = (int)$v; } else { $term = get_term_by('name', $v, $source['slug']); if ($term) $brand_ids[] = (int)$term->term_id; } } $old_array = $brand_service->get_product_brand($id); $old = implode(',', $old_array); if ($source['type'] === 'taxonomy' || $source['type'] === 'attribute') { if ($op === 'add') { $existing = wp_get_post_terms($id, $source['slug'], ['fields' => 'ids']); $new_ids = array_unique(array_merge($existing, $brand_ids)); wp_set_post_terms($id, $new_ids, $source['slug']); } elseif ($op === 'remove') { $existing = wp_get_post_terms($id, $source['slug'], ['fields' => 'ids']); $new_ids = array_diff($existing, $brand_ids); wp_set_post_terms($id, $new_ids, $source['slug']); } else { wp_set_post_terms($id, $brand_ids, $source['slug']); } } elseif ($source['type'] === 'meta') { $new_meta = is_array($val) ? $val[0] : $val; update_post_meta($id, $source['slug'], $new_meta); } $new_array = $brand_service->get_product_brand($id); $new = implode(',', $new_array); break; default: return false; } if ($old != $new) { $this->history->record(get_current_user_id(), $product->get_id(), $field, $old, $new, $batch_id, $source); return true; } return false; } private function calculate_numeric($old, $op, $val, $unit, $rounding = 'none') { $old = \WooCommersa\Core\Utils::normalize_number($old); $val = \WooCommersa\Core\Utils::normalize_number($val); $new = $old; switch ($op) { case 'increase': $new = ($unit === 'percentage' || $unit === 'percent') ? $old * (1 + $val / 100) : $old + $val; break; case 'decrease': $new = ($unit === 'percentage' || $unit === 'percent') ? $old * (1 - $val / 100) : $old - $val; break; case 'set': $new = $val; break; } return $this->apply_rounding($new, $rounding); } private function apply_text_operation($old, $op, $val) { switch ($op) { case 'prepend': return $val . $old; case 'append': return $old . $val; case 'set': return $val; default: return $old; } } private function apply_rounding($val, $rounding) { if ($rounding === 'none' || empty($rounding)) return $val; switch ($rounding) { case '5': return round($val / 5) * 5; case '10': return round($val / 10) * 10; case '100': return round($val / 100) * 100; case '1000': return round($val / 1000) * 1000; case 'ceil_5': return ceil($val / 5) * 5; case 'ceil_10': return ceil($val / 10) * 10; case 'ceil_100': return ceil($val / 100) * 100; case 'digits_1': return $this->round_significant($val, 1); case 'digits_2': return $this->round_significant($val, 2); case 'digits_3': return $this->round_significant($val, 3); default: return $val; } } private function round_significant($val, $digits) { if ($val == 0) return 0; $multiplier = pow(10, $digits - floor(log10(abs($val))) - 1); return round($val * $multiplier) / $multiplier; } }