Created
December 4, 2020 16:07
-
-
Save jmarreros/fd58a36e81922e830887ba1f94a73e3a to your computer and use it in GitHub Desktop.
Muestra una selección que varía el precio y se guarda ese adicional tanto en el carrito como el ítem de la orden al hacer el pedido
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // No copiar esta línea | |
// Para el detalle de producto, creación de controlo y cambio de precio | |
add_action('woocommerce_before_add_to_cart_button', 'dcms_build_select_field'); | |
function dcms_build_select_field(){ | |
global $product; | |
$options = [ | |
"0" => 'Selecciona una opción', | |
"10" => 'Agrega, +10 al precio', | |
"20" => 'Agrega, +20 al precio', | |
"30" => 'Agrega, +30 al precio' | |
]; | |
woocommerce_form_field('extras_product', array( | |
'type' => 'select', | |
'label' => 'Agregar al producto', | |
'required' => true, | |
'options' => $options, | |
),''); | |
$base_price = (float) wc_get_price_to_display( $product ); | |
$currency = get_woocommerce_currency_symbol(); | |
?> | |
<script> | |
jQuery(function($){ | |
const base_price = <?php echo $base_price; ?>; | |
const currency = "<?php echo $currency; ?>"; | |
$('#extras_product').on( 'change', function(){ | |
new_price = currency + (Number(this.value) + base_price).toFixed(2); | |
$('p.price').html( new_price ); | |
}); | |
}); | |
</script> | |
<?php | |
} | |
// Almacenamos el precio extra y el nuevo precio en los datos del ítem | |
add_filter( 'woocommerce_add_cart_item_data', 'dcms_extra_data_to_cart_item', 20, 2 ); | |
function dcms_extra_data_to_cart_item( $cart_item_data, $product_id ){ | |
if( ! isset($_POST['extras_product']) ) return $cart_item_data; | |
$extra_price = (float)$_POST['extras_product']; | |
if ( ! $extra_price ) return $cart_item_data; | |
$product = wc_get_product($product_id); | |
$base_price = (float) $product->get_price(); | |
$new_price =$base_price + $extra_price; | |
$cart_item_data['extras_product'] = array( | |
'extra_price' => $extra_price, | |
'new_price' => (float) $new_price, | |
); | |
return $cart_item_data; | |
} | |
// Realizamos el cálculo del nuevo precio y lo asignamos a los datos de carro | |
add_action('woocommerce_before_calculate_totals', 'dcms_custom_cart_item_price', 20, 1); | |
function dcms_custom_cart_item_price( $cart ) { | |
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return; | |
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return; | |
foreach ( $cart->get_cart() as $cart_item ) { | |
if ( isset( $cart_item['extras_product']['new_price'] ) ) | |
$cart_item['data']->set_price( $cart_item['extras_product']['new_price'] ); | |
} | |
} | |
// Mostrarmos los datos extra en los ítems del carrito | |
add_filter( 'woocommerce_get_item_data', 'display_custom_cart_item_data', 10, 2 ); | |
function display_custom_cart_item_data( $cart_data, $cart_item ) { | |
if ( isset( $cart_item['extras_product']['new_price'] ) ){ | |
$cart_data[] = [ | |
'name' => 'Extras', | |
'value' => '+' . $cart_item['extras_product']['extra_price'] | |
]; | |
} | |
return $cart_data; | |
} | |
// Para guardar los datos en el ítem de producto y que se muestren en las órdenes del backend | |
function dcms_save_new_order_item( $item_id, $item, $order_id ) { | |
wc_add_order_item_meta($item_id, 'Extra', $item->legacy_values['extras_product']['extra_price']); | |
}; | |
add_action( 'woocommerce_new_order_item', 'dcms_save_new_order_item', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment