Asked  1 Year ago    Answers:  5   Viewed   15 times
$replace_order = new WC_Cart();
$replace_order->empty_cart( true );
$replace_order->add_to_cart( "256", "1");

The above code add product 256 to the Cart 1 time. But the issue I'm having is that I want to be able to completely override the product price... as far as I can tell, the only thing I can do it apply a coupon to the Cart.

Is there a way to completely override the price to something totally custom?

 Answers

1

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...

Thursday, April 1, 2021
 
KHM
 
KHM
3

No, you can't change or delete, which could break everything, read more here - https://github.com/woocommerce/woocommerce/issues/12191.

Saturday, May 29, 2021
 
McAn
 
5

All of this code goes into functions.php

  1. This captures additional posted information (all sent in one array)

    add_filter('woocommerce_add_cart_item_data','wdm_add_item_data',1,10);
    function wdm_add_item_data($cart_item_data, $product_id) {
    
        global $woocommerce;
        $new_value = array();
        $new_value['_custom_options'] = $_POST['custom_options'];
    
        if(empty($cart_item_data)) {
            return $new_value;
        } else {
            return array_merge($cart_item_data, $new_value);
        }
    }
    
  2. This captures the information from the previous function and attaches it to the item.

    add_filter('woocommerce_get_cart_item_from_session', 'wdm_get_cart_items_from_session', 1, 3 );
    function wdm_get_cart_items_from_session($item,$values,$key) {
    
        if (array_key_exists( '_custom_options', $values ) ) {
            $item['_custom_options'] = $values['_custom_options'];
        }
    
        return $item;
    }
    
  3. This displays extra information on basket & checkout from within the added info that was attached to the item.

    add_filter('woocommerce_cart_item_name','add_usr_custom_session',1,3);
    function add_usr_custom_session($product_name, $values, $cart_item_key ) {
    
        $return_string = $product_name . "<br />" . $values['_custom_options']['description'];// . "<br />" . print_r($values['_custom_options']);
        return $return_string;
    
    }
    
  4. This adds the information as meta data so that it can be seen as part of the order (to hide any meta data from the customer just start it with an underscore)

    add_action('woocommerce_add_order_item_meta','wdm_add_values_to_order_item_meta',1,2);
    function wdm_add_values_to_order_item_meta($item_id, $values) {
        global $woocommerce,$wpdb;
    
        wc_add_order_item_meta($item_id,'item_details',$values['_custom_options']['description']);
        wc_add_order_item_meta($item_id,'customer_image',$values['_custom_options']['another_example_field']);
        wc_add_order_item_meta($item_id,'_hidden_field',$values['_custom_options']['hidden_info']);
    
    }
    
  5. If you want to override the price you can use information saved against the product to do so

    add_action( 'woocommerce_before_calculate_totals', 'update_custom_price', 1, 1 );
    function update_custom_price( $cart_object ) {
        foreach ( $cart_object->cart_contents as $cart_item_key => $value ) {       
            // Version 2.x
            //$value['data']->price = $value['_custom_options']['custom_price'];
            // Version 3.x / 4.x
            $value['data']->set_price($value['_custom_options']['custom_price']);
        }
    }
    

All your custom information will appear in the customer email and order from within wordpress providing you added it as meta data (4.)

Wednesday, July 28, 2021
 
4

Here is the code i was come up with success.

$a_options = array(
    'options' => array(
         'label' => 'Choice',
         'value' => $pkg_selected_products,
    )
);

$quoteItem->addOption(new Varien_Object(
    array(
        'product' => $quoteItem->getProduct(),
        'code' => 'additional_options',
        'value' => serialize($a_options)
    )
));

$quote->addItem($quoteItem);
$quote->save();
Saturday, November 13, 2021
 
Jason
 
2

Updated for WC 3+

Using $woocommerce->cart = new WC_Cart(); to create a new object instance is an apparently the solution to avoid error:

class WC_Product_My_Product extends WC_Product_Simple {

    public function some_method() {
        WC()->cart = new WC_Cart();
        $href = WC()->cart->get_cart_url();     
    }
}
Sunday, November 14, 2021
 
bsd
 
bsd
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share