I've been stuck on this for far too long. I've tried everything I've found on stackoverflow and I feel like I'm going around in circles.
I have a form on a product page asking for an invoice number and an amount on the invoice. I can get product attributes to update and display on the Cart page using the following code:
function invoice_save_price( $cart_item_data, $product_id ) {
if( isset( $_POST['invoice_id'] ) ) {
$cart_item_data = array();
$cart_item_data[ "invoice_number" ] = $_POST['invoice_number'];
$cart_item_data[ "invoice_amount" ] = $_POST['invoice_amount'];
}
return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'invoice_save_price', 99, 2 );
I'm using a custom product type - extending simple products. I'm not sure if that's the correct way to do this, as my product has to have a default price set ($1 for now) or it doesn't display.
function wcpt_register_invoice_type () {
class WC_Product_Invoice extends WC_Product {
public function __construct( $product ) {
$this->product_type = 'invoice';
parent::__construct( $product );
}
}
}
However, I can't get the price to update. My code for that is here:
add_action( 'woocommerce_before_calculate_totals', 'invoice_add_custom_price', 99 );
function invoice_add_custom_price($cart_object) {
if (isset($_POST['invoice_id'])) {
foreach ( $cart_object->get_cart() as $value ) {
$newValue = number_format($_POST['invoice_amount'],2);
$value['data']->set_price($newValue);
}
return $value;
}
}
I thought there might've been a conflict with another plugin, so I started testing on a clean install of Wordpress 5.8 and WooCommerce 5.5.2
I've found a lot of references to this in older versions of WooCommerce (3.x) but I'm not having much luck in later versions.
Can someone please tell me what I'm missing? Any help will be appreciated!
Thanks,
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.