feat: edycja personalizacji produktu w koszyku
Nowa metoda basketUpdateCustomFields() w ShopBasketController — AJAX endpoint z walidacją required fields, przeliczaniem product_code (MD5 hash) i merge duplikatów. UI: przycisk "Edytuj personalizację" + formularz inline + JS. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -446,6 +446,79 @@ class ShopBasketController
|
||||
] );
|
||||
}
|
||||
|
||||
public function basketUpdateCustomFields()
|
||||
{
|
||||
$basket = \Shared\Helpers\Helpers::get_session( 'basket' );
|
||||
$product_code = \Shared\Helpers\Helpers::get( 'product_code' );
|
||||
|
||||
if ( !isset( $basket[ $product_code ] ) )
|
||||
{
|
||||
echo json_encode( [ 'result' => 'error', 'message' => 'Pozycja nie istnieje w koszyku' ] );
|
||||
exit;
|
||||
}
|
||||
|
||||
$position = $basket[ $product_code ];
|
||||
$new_custom_fields = [];
|
||||
$custom_fields_raw = \Shared\Helpers\Helpers::get( 'custom_field' );
|
||||
|
||||
if ( is_array( $custom_fields_raw ) )
|
||||
{
|
||||
foreach ( $custom_fields_raw as $field_id => $value )
|
||||
{
|
||||
$new_custom_fields[ (int)$field_id ] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
$productRepo = new \Domain\Product\ProductRepository( $GLOBALS['mdb'] );
|
||||
$missing_fields = [];
|
||||
|
||||
foreach ( $new_custom_fields as $field_id => $value )
|
||||
{
|
||||
$field_meta = $productRepo->findCustomFieldCached( $field_id );
|
||||
if ( $field_meta && (int)$field_meta['is_required'] === 1 && trim( $value ) === '' )
|
||||
{
|
||||
$missing_fields[] = $field_meta['name'];
|
||||
}
|
||||
}
|
||||
|
||||
if ( count( $missing_fields ) > 0 )
|
||||
{
|
||||
echo json_encode( [ 'result' => 'error', 'message' => 'Wypełnij wymagane pola: ' . implode( ', ', $missing_fields ) ] );
|
||||
exit;
|
||||
}
|
||||
|
||||
$attributes_implode = '';
|
||||
if ( isset( $position['attributes'] ) && is_array( $position['attributes'] ) && count( $position['attributes'] ) > 0 )
|
||||
{
|
||||
$attributes_implode = implode( '|', $position['attributes'] );
|
||||
}
|
||||
|
||||
$message = isset( $position['message'] ) ? $position['message'] : '';
|
||||
$new_product_code = md5( $position['product-id'] . $attributes_implode . $message . json_encode( $new_custom_fields ) );
|
||||
|
||||
if ( $new_product_code === $product_code )
|
||||
{
|
||||
$basket[ $product_code ]['custom_fields'] = $new_custom_fields;
|
||||
}
|
||||
elseif ( isset( $basket[ $new_product_code ] ) )
|
||||
{
|
||||
$basket[ $new_product_code ]['quantity'] += $position['quantity'];
|
||||
unset( $basket[ $product_code ] );
|
||||
}
|
||||
else
|
||||
{
|
||||
$position['custom_fields'] = $new_custom_fields;
|
||||
$basket[ $new_product_code ] = $position;
|
||||
unset( $basket[ $product_code ] );
|
||||
}
|
||||
|
||||
$basket = ( new \Domain\Promotion\PromotionRepository( $GLOBALS['mdb'] ) )->findPromotion( $basket );
|
||||
\Shared\Helpers\Helpers::set_session( 'basket', $basket );
|
||||
|
||||
echo json_encode( [ 'result' => 'ok' ] );
|
||||
exit;
|
||||
}
|
||||
|
||||
private function jsonBasketResponse( $basket, $coupon, $lang_id, $basket_transport_method_id )
|
||||
{
|
||||
global $settings;
|
||||
|
||||
Reference in New Issue
Block a user