CopyPastor

Detecting plagiarism made easy.

Score: 0.803358793258667; Reported for: String similarity Open both answers

Possible Plagiarism

Reposted on 2018-09-12
by LoicTheAztec

Original Post

Original - Posted on 2018-01-06
by LoicTheAztec



            
Present in both answers; Present only in the new answer; Present only in the old answer;

First for testing purpose we add a price in the hidden input field as you don't give the code that calculate the price:
add_action( 'woocommerce_before_add_to_cart_button', 'custom_hidden_product_field', 11 ); function custom_hidden_product_field() { echo '<input type="hidden" id="hidden_field" name="custom_price" class="custom_price" value="20">'; // Price is 20 for testing }
Then you will use the following to change the cart item price:
// Save custom calculated price as custom cart item data add_action( 'woocommerce_add_cart_item_data', 'save_custom_fields_data_to_cart', 10, 2 ); function save_custom_fields_data_to_cart( $cart_item_data, $product_id ) {
if( ! empty( $_REQUEST['custom_price'] ) ) { // Set the custom data in the cart item $cart_item_data['custom_price'] = $_REQUEST['custom_price'];
// Make each item as a unique separated cart item $cart_item_data['unique_key'] = md5( microtime().rand() ); } return $cart_item_data; }
// Updating cart item price add_action( 'woocommerce_before_calculate_totals', 'change_cart_item_price', 30, 1 ); function change_cart_item_price( $cart ) { if ( ( is_admin() && ! defined( 'DOING_AJAX' ) ) ) return;
// Loop through cart items foreach ( $cart->get_cart() as $cart_item ) { // Set the new price if( isset($cart_item['custom_price']) ){ $cart_item['data']->set_price($cart_item['custom_price']); } } }
Code goes in function.php file of your active child theme (or active theme). Tested and works.
**Updated**
For testing this successfully (and as I don't know how you make calculations), I have added a custom hidden field in product add to cart form with the following:
// The hidden product custom field add_action( 'woocommerce_before_add_to_cart_button', 'add_gift_wrap_field' ); function add_gift_wrap_field() { global $product; // The fake calculated price ?> <input type="hidden" id="my-price" name="my-price" value="115"> <?php }
When product is added to cart, this `my-price` custom field is also submitted (posted). To set this value in cart object I use the following function:
add_filter( 'woocommerce_add_cart_item', 'custom_cart_item_prices', 20, 2 ); function custom_cart_item_prices( $cart_item_data, $cart_item_key ) { // Get and set your price calculation if( isset( $_POST['my-price'] ) ){ $cart_item_data['my-price'] = $_POST['my-price']; }
return $cart_item_data; }
Now to apply (set) the new calculated price `my-price` to the cart item, I use this last function:
add_action( 'woocommerce_before_calculate_totals', 'set_calculated_cart_item_price', 20, 1 ); function set_calculated_cart_item_price( $wc_cart ) { if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return; // Loop through cart items foreach ( $wc_cart->get_cart() as $cart_item ){ if( ! empty( $cart_item['my-price'] ) || $cart_item['my-price'] != 0 ){ // Set the calculated item price (if there is one) $cart_item['data']->set_price( $cart_item['my-price'] ); } } }
*All code goes in function.php file of your active child theme (or active theme).*
Tested and works

        
Present in both answers; Present only in the new answer; Present only in the old answer;