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

Facebook redirect in application
Getting values from different keys belonging to different arrays and merging them into another array
php_imap.dll for php v 5.2.6 apache for windows
Change 'sort by' in plugin
Difference between set_time_limit() and ini_set('max_execution_time', …)
British Date Formatting in PHP
Sending mail in PHP [closed]
Image upload form not posting input value when auto submitting form
jQuery looping only works on last row
Using a button outside a form to submit form and trigger jQuery validation
Codeigniter session problems
URI's to fetch database data (CodeIgniter)
Constructing a more efficient javascript user-based chat
ReCaptcha always returns false, with no error
how to pass an valuable with redirect?
create a multilingual website with php [closed]

Categories

HOME
hpoo
clearcase
focus
workflow
applescript
haproxy
risk-management
informatica
celery
semantic-web
malware
gentelella
amp
javafx-8
firebase-dynamic-links
minecraft
scheduler
mutation-testing
twitter-oauth
rangy
spring-jpa
google-tasks-api
nest-api
bazaar
spyder
spring-ldap
qa
web-config-transform
morris.js
unrar
network-analysis
bobo-browse.net
sfml
fusion
social-networking
location-services
windowsiot
progid
sql-execution-plan
cmis
bootstrap-sass
spinner
fastlane
klee
viewcontroller
doctrine-extensions
linq-to-excel
photoswipe
nusoap
visual-format-language
exc-bad-access
nslayoutconstraint
stateless-session-bean
dcount
livecycle
traversable
piping
qpixmap
unsigned
android-syncadapter
textblock
msgpack
arbre
highest
android-studio-import
domino-designer-eclipse
actionpack
jcheckbox
phonegap-facebook-plugin
password-recovery
xml-signature
relationships
webautomation
accessory
hyperloglog
thruway
tabletools
gpu-programming
hyphen
towers-of-hanoi
idispatch
exponent
database-deadlocks
fork-join
rtd
grails-validation
airprint
django-pagination
jdownloader
principles
raw-data
main-method
twitter-feed
regioninfo
service-factory
synthesizer

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App