Customising prices with order again filter
-
Hi!
I need to calculate prices based on the weight of the product. I’ve successfully implemented this using the following filter:
add_filter( 'woocommerce_add_cart_item', 'set_custom_cart_item_prices', 20, 2 ); function set_custom_cart_item_prices( $cart_data, $cart_item_key ) { // Price calculation if ($cart_data['data']->get_weight()) { $new_price = $cart_data['data']->get_price() * ($cart_data['data']->get_weight()/1000); // Set and register the new calculated price $cart_data['data']->set_price( $new_price ); $cart_data['new_price'] = $new_price; } return $cart_data; }
In the archive-product.php file, I show the users last order and allow them to add all items from this order to the cart using the following code:
<?php if (!is_product_category()) { if ($last_order) { $products = $last_order[0]->get_items(); ?> <section class="last-order"> <h3>Laatste bestelling</h3> <section> <ul> <?php foreach ($products as $product) { ?> <a href="<?php the_permalink($product['product_id']); ?>"> <li> <p class="name"><span class="quantity"><?php echo $product->get_quantity(); ?>x</span> <?php echo $product->get_product()->get_name(); ?></p> <p class="price"><?php echo wc_price($product->get_quantity() * $product->get_product()->get_price()); ?></p> </li> </a> <?php } ?> </ul> </section> <div class="wp-block-button wp-block-button-width"> <a class="wp-block-button__link" href="<?php echo wp_nonce_url( add_query_arg( 'order_again', $last_order[0]->get_id() ) , 'woocommerce-order_again' ); ?>">Voeg toe aan winkelwagen<img src="<?php echo get_stylesheet_directory_uri(); ?>/images/icon-arrow-right.svg"/></a> </div> </section>
I noticed that using this functionality results in incorrect prices in the cart. That’s why I tried to solve it using the following code:
add_filter( 'woocommerce_add_order_again_cart_item ', 'set_custom_order_again_cart_item_prices', 20, 2 ); function set_custom_order_again_cart_item_prices( $cart_data, $cart_item_key ) { // Price calculation if ($cart_data['data']->get_weight()) { $new_price = $cart_data['data']->get_price() * ($cart_data['data']->get_weight()/1000); // Set and register the new calculated price $cart_data['data']->set_price( $new_price ); $cart_data['new_price'] = $new_price; } return $cart_data; }
Unfortunately, this doesn’t work. Does anyone know if the filter I’m using is wrong? Or if there’s a better way to calculate prices based on weight? I’d highly appreciate it if anyone could have a look!
Thanks in advance,
Quinten
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
- The topic ‘Customising prices with order again filter’ is closed to new replies.