Woocommerce: Add Product to Cart with Price Override

WooCommerce: Add product to cart with price override?

Here is the code for overriding price of product in cart

add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );

function add_custom_price( $cart_object ) {
$custom_price = 10; // This will be your custome price
foreach ( $cart_object->cart_contents as $key => $value ) {
$value['data']->price = $custom_price;
// for WooCommerce version 3+ use:
// $value['data']->set_price($custom_price);
}
}

Hope it will be useful...

Override cart item price and image in WooCommerce

Your code is a bit obsolete since WooCommerce 3 and there are some missing things. Try the following replacement to include also your custom image attachment ID as follows:

In Your file My_Plugin.php:

$custom_price   = 200;
// $regular_price = 200; // Not needed and not defined (in your code)
$thumbnail_id = 35; // <=== Here define the post type "attachment" post ID for your image (Image attachment ID)
$product_id = 2569;
$variation_id = 2697;
$variation = array(); // ? | Not defined in your code
$quantity = 1;
$cart_item_data = array(
'custom_price' => $custom_price,
// 'regular_price' => $regular_price,
'thumbnail_id' => $thumbnail_id, // Here we add the image attachment ID
);

WC()->cart->add_to_cart( $product_id, $quantity, $variation_id, $variation, $cart_item_data );
WC()->cart->calculate_totals();

In functions.php file of your child theme:

add_action( 'woocommerce_before_calculate_totals', 'custom_cart_item_data_replacement' );
function custom_cart_item_data_replacement( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;

// Loop through cart items
foreach ( $cart->cart_contents as $cart_item ) {
// Custom price
if( isset($cart_item["custom_price"]) ) {
$cart_item['data']->set_price($cart_item["custom_price"]);
}
// Custom image attachment id
if( isset($cart_item["thumbnail_id"]) ) {
$cart_item['data']->set_image_id($cart_item["thumbnail_id"]);
}
}
}

It should works.

You can also alter all product properties via the WC_Product setter available methods.

WooCommerce: Cart price override with text

expanding on Helga's answer, here's the full code that changes the prices to 'to be determined' on cart pages, checkout pages and order confirmation emails too. Text kept different so people can spot where each filter goes.

    add_filter( 'woocommerce_cart_item_price', 'filter_cart_item_price', 10, 3 );
function filter_cart_item_price( $price, $cart_item, $cart_item_key ) {
if ( $cart_item[ 'data' ]->price == 0 ) {
$price = __( 'Special Order', 'yourtheme' );
}
return $price;
}

add_filter( 'woocommerce_cart_item_subtotal', 'filter_cart_item_subtotal', 10, 3 );
function filter_cart_item_subtotal( $subtotal, $cart_item, $cart_item_key ) {
if ( $cart_item[ 'data' ]->price == 0 ) {
$subtotal = __( 'To be determined', 'yourtheme' );
}
return $subtotal;
}

add_filter( 'woocommerce_cart_subtotal', 'filter_woocommerce_cart_subtotal', 10, 3 );
function filter_woocommerce_cart_subtotal( $cart_subtotal, $compound, $instance ) {
$cart_subtotal = __( 'To be determined 4', 'yourtheme' );
return $cart_subtotal;
};

add_filter( 'woocommerce_order_formatted_line_subtotal', 'filter_order_item_subtotal', 10, 3 );
function filter_order_item_subtotal( $subtotal, $item, $order ) {
if ( isset( $item[ 'line_subtotal' ] ) && $item[ 'line_subtotal' ] == 0 ) {
$subtotal = __( 'To be determined 2', 'yourtheme' );
}
return $subtotal;
}

add_filter( 'woocommerce_order_subtotal_to_display', 'filter_woocommerce_order_subtotal_to_display', 10, 3 );
function filter_woocommerce_order_subtotal_to_display( $subtotal, $compound, $instance ) {
$subtotal = __( 'To be determined 6', 'yourtheme' );
return $subtotal;
};

add_filter( 'woocommerce_get_formatted_order_total', 'filter_woocommerce_get_formatted_order_total', 10, 2 );
function filter_woocommerce_get_formatted_order_total( $formatted_total, $instance ) {
$formatted_total = __( 'To be determined 8', 'yourtheme' );
return $formatted_total;
};

Cart total price Override in WooCommerce

You're not accumulating the subtotal in your woocommerce_calculate_totals function. You're writing $cart_sub_total = $final_price; every time so you'll only have the price of the last item at the end. Instead it should be $cart_sub_total = $cart_sub_total + $final_price;.



Related Topics



Leave a reply



Submit