• Hola Buenos días estoy desarrollando un script para woocomerce , para que me calcule un precio a partir de los valores que le paso por un formulario, tengo lo siguiente:

    add_filter('woocommerce_before_add_to_cart_button', 'medirlargo', 5 );
    function medirlargo(){
        echo '<div class="custom-text text">
        <label>Largo (mm)</label>
        <input type="number" id="largo" name="largo" min="100" max="2500">
        </div>';
    }
    // Get custom field value, calculate new item price, save it as custom cart item data
     function return_custom_price($price, $product) {
    	  global $post, $blog_id;
    	$largo =  $_POST['largo'] ;
        $product = wc_get_product( $post_id );
        $post_id = $post->ID;
        $price = $price+$largo;
        return $price;
    }
    add_filter('woocommerce_get_price', 'return_custom_price', 10, 2);

    pero no me pasa el valor de largo a la funcion

    • This topic was modified 4 years, 11 months ago by kaesarus.
Viewing 6 replies - 1 through 6 (of 6 total)
  • $product = wc_get_product( $post_id );
    $product is passed to your function so it already exists, no need to find it.
    $post_id does not exist in the function
    $product is not used later in the function

    $post_id = $post->ID;
    $post_id is not used later in the function

    If you need the $product_id, you would do:
    $product_id = $product->get_id();

    It should work without these two lines:

    $product = wc_get_product( $post_id );
    $post_id = $post->ID;

    they don’t seem to do anything.

    Thread Starter kaesarus

    (@kaesarus)

    perfecto si elimino esas lineas me queda:
    function return_custom_price($price, $product) {

    	$product_id = $product->get_id();
        $price = ($price+$largo;
        return $price;
    }
    add_filter('woocommerce_get_price', 'return_custom_price', 10, 2);

    Lo que quiero hacer es que el valor de largo de function medirlargo(){, vaya a la funcion
    return_custom_price y sume el precio al largo, como sigo?

    $product_id = $product->get_id();
    $product_id is not used.

    $price = ($price+$largo;
    has an opening bracket but not a closing one.

    global $post, $blog_id;
    $post and $blog_id are not used.

    So that leaves only:

    function return_custom_price( $price, $product ) {
        $largo = $_POST['largo'] ;
        $price = $price + $largo;
        return $price;
    }
    add_filter( 'woocommerce_get_price', 'return_custom_price', 10, 2 );
    Thread Starter kaesarus

    (@kaesarus)

    pero lo que quiero es que $price se vea en el front-end de la pagina del producto y no se ve no cambia el precio
    function return_custom_price( $price, $product ) {
    $largo = $_POST[‘largo’] ;
    $price = $price + $largo;
    return $price;
    }

    add_filter( ‘woocommerce_get_price’, ‘return_custom_price’, 10, 2 );

    Necesito cambiar algo en add-filter?

    add_filter('woocommerce_before_add_to_cart_button', 'medirlargo', 5 );
    function medirlargo(){
        $largo = isset( $_POST['largo'] ) ? $_POST['largo'] : '';
        echo '<div class="custom-text text">';
        echo '<label>Largo (mm)</label>';
        echo '<input type="number" id="largo" name="largo" min="100" max="2500" value="'.$largo.'">';
        echo '</div>';
    }
    
    // Get custom field value, calculate new item price, save it as custom cart item data
    add_filter( 'woocommerce_product_get_price', 'return_custom_price', 10, 2 );
    function return_custom_price( $price, $product ) {
        if( isset( $_POST['largo'] ) ) {
          $largo =  $_POST['largo'] ;
          $price = $price + $largo;
        }
        return $price;
        // can't add to cart item data because its not a cart item at this point
    }

    This will alter the pice on the product page, but it won’t alter the price of the item when its added to the cart a few moments later.

    To do that would need a lot more code than you would normally get in a forum answer.

    You’ll need to look at product add-on plugins. Unfortunately, I think the free ones won’t do this. You’ll need to carefully review the premium product add-on plugins to make sure they will do what you want. You can ask pre-sales questions if you want.

    Thread Starter kaesarus

    (@kaesarus)

    Perfecto para que se muestre en el carrito tendría que hacer algo como esto:
    add_filter( ‘woocommerce_add_cart_item_data’, ‘add_cart_item_custom_data_vase’, 10, 2 );
    function add_cart_item_custom_data_vase( $cart_item_meta, $product_id ) {
    $cart_item_meta[‘largo’] = $_POST[‘largo’];
    return $cart_item_meta;
    }
    pero en el carrito tendría que mostrar precio nuevo,nombre producto,cantidad y el custom value(que es largo)

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘formulario no pasa valor de la variable a funcion’ is closed to new replies.