php


Dynamic cart item pricing not working on orders in WooCommerce 3.0+


I am using WooCommerce 3.0+ and I have set the product price on a certain page.
$regular_price = get_post_meta( $_product->id, '_regular_price', true);
$buyback_percentage = get_post_meta( $_product->id, '_goldpricelive_buy_back', true);
$fixed_amount = get_post_meta( $_product->id, '_goldpricelive_fixed_amount', true);
$markedup_price = get_post_meta( $_product->id, '_goldpricelive_markup', true);
$buyback_price = ($regular_price - $fixed_amount)/(1 + $markedup_price/100) * (1-$buyback_percentage/100);
$_product->set_price($buyback_price);
The price is updating on my cart but when I click on to submit my order, Order object doesn't seem to get the price I set. It takes the origin product price.
Any idea on how I can accomplish this?
Thanks
You should use woocommerce_before_calculate_totals action hook setting inside this custom hooked function, your products IDs or an array of product IDs. Then for each of them you can make a custom calculation to set a custom price that will be set on Cart, checkout and after submitting in the order.
Here is that functional code tested on WooCommerce version 3.0+:
add_action( 'woocommerce_before_calculate_totals', 'adding_custom_price', 10, 1);
function adding_custom_price( $cart_obj ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Set below your targeted individual products IDs or arrays of product IDs
$target_product_id = 53;
$target_product_ids_arr = array(22, 56, 81);
foreach ( $cart_obj->get_cart() as $cart_item ) {
// The corresponding product ID
$product_id = $cart_item['product_id'];
// For a single product ID
if($product_id == $target_product_id){
// Custom calculation
$price = $cart_item['data']->price + 50;
$cart_item['data']->set_price( $price );
}
// For an array of product IDs
elseif( in_array( $product_id, $target_product_ids_arr ) ){
// Custom calculation
$price = $cart_item['data']->price + 30;
$cart_item['data']->set_price( $price );
}
}
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Then you can easily replace the fixed values in my fake calculations by your product dynamic values with that with get_post_meta() function just like in your code as you have the $product_id for each cart item…

Related Links

Passing a PHP data string to Ajax
php Pagination next prev page
Warning: mysqli_affected_rows() expects parameter 1 to be mysqli, boolean given in C:\xampp\htdocs\ajax-complete\live-table-post.php on line 23 [duplicate]
empty(func($var)) triggers a “Can't use function return value” error
Redis driver seems locked when loaded by codeigniter
PHP pgsql query to populate select List is resulting in empty option tags
Woocommerce Subscriptions - two sign-up fees or offer varying recurring payments
PHP fopen path is not working [duplicate]
Insert Image in TD only in Small Screen Sizes?
On/Off Button - Return True/False value with PHP [closed]
Exception class not found in Laravel
two submit buttons from one form laravel php
How to perform a query at specific time given php
php default active class for sorting arrays in wordpress
Compare ArrayCollection with array of objects
Assign attribute to SoapVar

Categories

HOME
php
python-2.7
oracle
visual-studio
magento2
gaming
facebook-messenger
opencv-python
exchange-server
website
elisp
fogbugz
openflow
material-components
jint
javafx-8
angular2-template
telegraf
lc3
csrf
circleci
android-externalstorage
c#-7.0
maven-plugin
salt-cloud
bazaar
opam
gitkraken
android-permissions
backtracking
fido-u2f
stackexchange.redis
pubxml
webviewclient
crud
tf-idf
jackrabbit-oak
ibm-connections
source-insight
devtools
winrm
gnu-classpath
rails-postgresql
numerics
ftp-server
fastlane
android-n
widestring
export-to-pdf
utf
fluid-mac-app-engine
doctrine-extensions
music21
qvtkwidget
cloudwatch
aws-kinesis-firehose
amazon-elastic-beanstalk
nivo-slider
glassfish-4.1
instafeedjs
annotatorjs
oracle-fusion-apps
gapi
nvda
distributed-cache
xcopy
largenumber
artisan
phpredis
whitespace-language
azure-xplat-cli
scala-swing
mod-auth-openidc
encryption-symmetric
storing-data
mp4parser
coalesce
node.js-stream
ofstream
semantic-merge
clrprofiler
chronometer
acpi
type-safety
quartz-core
servercontrol
uipasteboard
qcar-sdk
iphone-sdk-4.3
sqlsitemapprovider

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App