fix: Product list display, redirect after create, and edit form data loading

Fixed 3 critical issues:

1.  Price and Type Column Display
   Problem: Columns showing empty even though data exists
   Root Cause: price_html returns empty string for products without prices
   Solution:
   - Added fallback chain in index.tsx:
     1. Try price_html (formatted HTML)
     2. Fallback to regular_price (plain number)
     3. Fallback to "—" (dash)
   - Added fallback for type: {product.type || '—'}

   Now displays:
   - Formatted price if available
   - Plain price if no HTML
   - Dash if no price at all

2.  Redirect After Create Product
   Problem: Stays on form after creating product
   Expected: Return to products index
   Solution:
   - Changed New.tsx redirect from:
     navigate(`/products/${response.id}`) → navigate('/products')
   - Removed conditional logic
   - Always redirect to index after successful create

   User flow now:
   Create product → Success toast → Back to products list 

3.  Edit Form Not Loading Data
   Problem: Edit form shows empty fields instead of product data
   Root Cause: Missing fields in API response (virtual, downloadable, featured)
   Solution:
   - Added to format_product_full() in ProductsController.php:
     * $data['virtual'] = $product->is_virtual();
     * $data['downloadable'] = $product->is_downloadable();
     * $data['featured'] = $product->is_featured();

   Now edit form receives complete data:
   - Basic info (name, type, status, descriptions)
   - Pricing (SKU, regular_price, sale_price)
   - Inventory (manage_stock, stock_quantity, stock_status)
   - Categories & tags
   - Virtual, downloadable, featured flags
   - Attributes & variations (for variable products)

Result:
 Products list shows prices and types correctly
 Creating product redirects to index
 Editing product loads all data properly
This commit is contained in:
dwindown
2025-11-19 23:13:52 +07:00
parent 7bab3d809d
commit 4d185f0c24
3 changed files with 15 additions and 8 deletions

View File

@@ -27,12 +27,8 @@ export default function ProductNew() {
toast.success(__('Product created successfully')); toast.success(__('Product created successfully'));
queryClient.invalidateQueries({ queryKey: ['products'] }); queryClient.invalidateQueries({ queryKey: ['products'] });
// Navigate to product detail or edit page // Navigate back to products index
if (response?.id) {
navigate(`/products/${response.id}`);
} else {
navigate('/products'); navigate('/products');
}
}, },
onError: (error: any) => { onError: (error: any) => {
toast.error(error.message || __('Failed to create product')); toast.error(error.message || __('Failed to create product'));

View File

@@ -382,8 +382,16 @@ export default function Products() {
<td className="p-3"> <td className="p-3">
<StockBadge value={product.stock_status} quantity={product.manage_stock ? product.stock_quantity : undefined} /> <StockBadge value={product.stock_status} quantity={product.manage_stock ? product.stock_quantity : undefined} />
</td> </td>
<td className="p-3" dangerouslySetInnerHTML={{ __html: product.price_html || '—' }} /> <td className="p-3">
<td className="p-3 text-sm capitalize">{product.type}</td> {product.price_html ? (
<span dangerouslySetInnerHTML={{ __html: product.price_html }} />
) : product.regular_price ? (
<span>${parseFloat(product.regular_price).toFixed(2)}</span>
) : (
<span className="text-muted-foreground"></span>
)}
</td>
<td className="p-3 text-sm capitalize">{product.type || '—'}</td>
<td className="p-3 text-right"> <td className="p-3 text-right">
<Link to={`/products/${product.id}/edit`} className="text-sm text-primary hover:underline"> <Link to={`/products/${product.id}/edit`} className="text-sm text-primary hover:underline">
{__('Edit')} {__('Edit')}

View File

@@ -493,6 +493,9 @@ class ProductsController {
$data['tags'] = $product->get_tag_ids(); $data['tags'] = $product->get_tag_ids();
$data['image_id'] = $product->get_image_id(); $data['image_id'] = $product->get_image_id();
$data['gallery_image_ids'] = $product->get_gallery_image_ids(); $data['gallery_image_ids'] = $product->get_gallery_image_ids();
$data['virtual'] = $product->is_virtual();
$data['downloadable'] = $product->is_downloadable();
$data['featured'] = $product->is_featured();
// Gallery images // Gallery images
$gallery = []; $gallery = [];