Skip to content

Instantly share code, notes, and snippets.

@Mikodes
Last active February 13, 2025 00:27
Show Gist options
  • Save Mikodes/f947ec3a7f1efc30e003d1488e779d02 to your computer and use it in GitHub Desktop.
Save Mikodes/f947ec3a7f1efc30e003d1488e779d02 to your computer and use it in GitHub Desktop.
add_filter('woocommerce_product_add_to_cart_text', 'cambiar_boton_si_edicion', 10, 2);
function cambiar_boton_si_edicion($text, $product) {
if (isset($_GET['edit_personalization']) && isset($_GET['cart_item_key'])) {
return __('Actualizar Personalización', 'woocommerce');
}
return $text;
}
add_filter('wc_get_cart_url', 'modificar_url_redireccion_carrito');
function modificar_url_redireccion_carrito($url) {
if (isset($_GET['edit_personalization']) && isset($_GET['cart_item_key'])) {
return esc_url(add_query_arg(['cart_item_key' => sanitize_text_field($_GET['cart_item_key'])], wc_get_cart_url()));
}
return $url;
}
add_action('woocommerce_before_add_to_cart_button', 'agregar_input_cart_item_key');
function agregar_input_cart_item_key() {
if (isset($_GET['edit_personalization']) && isset($_GET['cart_item_key'])) {
echo '<input type="hidden" name="update_cart_item_key" value="' . esc_attr($_GET['cart_item_key']) . '">';
}
}
add_action('woocommerce_add_to_cart', 'actualizar_producto_existente_en_carrito', 10, 6);
function actualizar_producto_existente_en_carrito($cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data) {
if (!empty($_POST['update_cart_item_key'])) {
$update_cart_item_key = sanitize_text_field($_POST['update_cart_item_key']);
$cart_contents = WC()->cart->get_cart();
if (isset($cart_contents[$update_cart_item_key])) {
// Si el producto ya está en el carrito, actualizamos los datos
if (!empty($_POST['addon'])) {
$cart_contents[$update_cart_item_key]['addons'] = $_POST['addon'];
}
WC()->cart->set_session(); // Guardamos los cambios en el carrito
wc_add_notice(__('Personalización actualizada correctamente.', 'woocommerce'), 'success');
wp_safe_redirect(wc_get_cart_url());
exit;
}
}
}
add_action('template_redirect', 'evitar_redireccion_infinita_carrito');
function evitar_redireccion_infinita_carrito() {
if (is_cart() && isset($_GET['edit_personalization'])) {
wc_clear_notices(); // Eliminar mensajes previos
}
}
add_filter('woocommerce_cart_item_name', 'agregar_boton_personalizar_en_carrito', 10, 3);
function agregar_boton_personalizar_en_carrito($product_name, $cart_item, $cart_item_key) {
if (isset($cart_item['fgf_gift_product']) && fgf_check_is_array($cart_item['fgf_gift_product'])) {
$edit_url = add_query_arg([
'edit_personalization' => '1',
'cart_item_key' => $cart_item_key
], get_permalink($cart_item['product_id']));
$product_name .= '<br><a href="' . esc_url($edit_url) . '" class="button personalizar-btn">' . __('Personalizar', 'woocommerce') . '</a>';
}
return $product_name;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment