I'm working on a plugin where I need to override the price and the image of the product when is added to the cart. So far I was able to change just the price.
Can someone please help me to implement something similar also for the images?
My_Plugin.php
global $woocommerce;
$custom_price = 200;
$product_id = 2569;
$variation_id = 2697;
$quantity = 1;
$cart_item_data = array('custom_price' => $custom_price, 'regular_price' => $regular_price);
$woocommerce->cart->add_to_cart( $product_id, $quantity, $variation_id, $variation, $cart_item_data );
$woocommerce->cart->calculate_totals();
functions.php
function woocommerce_custom_price_to_cart_item($cart_object)
{
foreach ($cart_object->cart_contents as $key => $value) {
if (isset($value["custom_price"])) {
$value['data']->set_price($value["custom_price"]);
}
}
}
add_action( 'woocommerce_before_calculate_totals', 'woocommerce_custom_price_to_cart_item', 16 );
Is possible also to add some additional fields to the products when is added to the cart, like: url_file_uploaded, additional_description, etc
Many thanks!