$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.
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 );
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.
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)