Skip to content

Instantly share code, notes, and snippets.

@agustinprosperi
Created September 4, 2016 21:23
Show Gist options
  • Save agustinprosperi/de42f3e796bec7670d34ce31aa46084b to your computer and use it in GitHub Desktop.
Save agustinprosperi/de42f3e796bec7670d34ce31aa46084b to your computer and use it in GitHub Desktop.
<?php
/***************************woocommerce*hooks*********************************/
// simulacion de webservices
function wc_batela_get_vars(){
return $las_vars = array(
'var1'=>'Var 1',
'new_price'=>'Nuevo precio',
);
}
// aqui se imprimen las variaciones en el detalle del producto
add_action( 'woocommerce_before_add_to_cart_button', function(){
// global del producto para traer las variaciones
Global $product;
?>
<table>
<tr>
<td>Var1: <input name="var1" value="" /></td>
</tr>
<tr>
<td>Var3: <input name="new_price" value="15" /></td>
</tr>
</table>
<?php
} );
// por si se quiere validar algo
add_action( 'woocommerce_add_to_cart_validation', function( $true, $product_id = null, $quantity ){
if( isset( $product_id ) ) {
$is_passed = true;
foreach(wc_batela_get_vars() as $key => $value) {
if( empty($_REQUEST[ $key ]) ){
$is_passed = false;
wc_add_notice( $value.' no puede estar vacio.', 'error' );
}
}
return $is_passed;
} else {
return true;
}
}, 10, 3 );
// agregar al carrito las variaciones
add_filter( 'woocommerce_add_cart_item_data', function( $cart_item_data, $product_id ){
$unique_cart_item_key = md5( microtime().rand() );
$cart_item_data['wc_batela_unique_key'] = $unique_cart_item_key;
// echo'woocommerce_add_cart_item_data'; var_dump($product_id); exit();
if( $product_id ) {
foreach(wc_batela_get_vars() as $key => $value) {
if( isset($_REQUEST[ $key ]) ){
WC()->session->set( $unique_cart_item_key.$key , $_REQUEST[ $key ] );
}
}
}
return $cart_item_data;
}, 10, 2 );
// poner nuevo precio al producto antes de añadir al carrito
add_filter( 'woocommerce_add_cart_item' , function( $array, $cart_item_key ){
// echo .'<br>';
if( isset( $array['product_id'] ) && isset( $array['wc_batela_unique_key'] ) ) {
$var1 = WC()->session->get( $array['wc_batela_unique_key'].'var1' );
$new_price = WC()->session->get( $array['wc_batela_unique_key'].'new_price' );
$new_price *= $var1;
$array['data']->set_price( $new_price );
$array['my_price'] = $new_price;
}
// echo'woocommerce_add_cart_item'; var_dump($array); exit();
return $array;
}, 10, 2 );
// obtener el nuevo precio de producto para el carrito
add_filter( 'woocommerce_get_cart_item_from_session', function( $session_data , $values , $key ){
if ( !isset( $session_data['my_price'] ) || empty ( $session_data['my_price'] ) ) { return $session_data; }
$session_data['data']->set_price( $session_data['my_price'] );
return $session_data;
}, 20 , 3 );
// obtener el dato extra
add_filter( 'woocommerce_get_item_data', function( $item_data, $cart_item = null ){
$wc_batela_items = array();
if( isset( $cart_item['product_id'] ) && isset( $cart_item['wc_batela_unique_key'] ) ) {
foreach(wc_batela_get_vars() as $key => $value) {
if( WC()->session->__isset( $cart_item['wc_batela_unique_key'].$key ) && trim( WC()->session->get( $cart_item['wc_batela_unique_key'].$key ) ) ) {
$wc_batela_items[] = array( "name" => $value, "value" => WC()->session->get( $cart_item['wc_batela_unique_key'].$key ) );
}
}
}
return $wc_batela_items;
}, 1, 2 );
// mostrar variaciones en el checkout
add_action( 'woocommerce_add_order_item_meta', function( $item_id, $values, $cart_item_key ){
if( isset($values["product_id"] ) ) {
foreach(wc_batela_get_vars() as $key => $value) {
if( WC()->session->__isset( $values['wc_batela_unique_key'].$key ) && trim( WC()->session->get( $values['wc_batela_unique_key'].$key ) ) ) {
wc_add_order_item_meta( $item_id, $value, WC()->session->get( $values['wc_batela_unique_key'].$key ) );
}
}
}
}, 1, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment