John Davidson

php - A copy of the Woocommerce Cart on another page is not showing the correct prices. How can I fix this?

0 comments
Message:


We created an additional page on our webshop for some kind of 'quotation'. On that page we'd like to load the cart. The contents of the 'quotation' is basically the same as the cart content.


We tried copying all of the WooCommerce code from the template file cart.php.


Everything is showing correctly, except for the prices. Our quotation cart is showing the basic prices that are set up in the WooCommerce back-end.


But here's our problem: When adding a product to the cart, there are additional price settings which we custom coded by using some hooks, like cart item data that's being added.


All of our custom code works perfectly fine when opening the regular WooCommerce cart, but when showing that exact Cart on another page, prices are 'reset' to the regular starting prices.


The reason we copied the code from the cart.php template file is the ability to change the structure with additional css/html.


Here you can find the shortcode & complete function of our 'quotation' cart:


<?php function toon_winkelmand_offerte() {
do_action( 'woocommerce_before_cart_table' );?>
<form class="woocommerce-cart-form" action="<?php echo esc_url( wc_get_cart_url() ); ?>" method="post">
<?php do_action( 'woocommerce_before_cart_table' ); ?>
<div class="offerte_wrapper">
<?php
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
?>
<div class="offerte_item">
<?php
$_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
$product_id = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );
$product_qty = $cart_item['quantity'];

if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters( 'woocommerce_cart_item_visible', true, $cart_item, $cart_item_key ) ) {
$product_permalink = apply_filters( 'woocommerce_cart_item_permalink', $_product->is_visible() ? $_product->get_permalink( $cart_item ) : '', $cart_item, $cart_item_key );
?>
<div class="product-thumbnail">
<?php
$thumbnail = apply_filters( 'woocommerce_cart_item_thumbnail', $_product->get_image(), $cart_item, $cart_item_key );

if ( ! $product_permalink ) {
echo $thumbnail; // PHPCS: XSS ok.
} else {
printf( '<a href="%s">%s</a>', esc_url( $product_permalink ), $thumbnail ); // PHPCS: XSS ok.
}
?>
</div>

<div class="product-name" data-title="<?php esc_attr_e( 'Product', 'woocommerce' ); ?>">
<?php
if ( ! $product_permalink ) {
echo wp_kses_post( apply_filters( 'woocommerce_cart_item_name', $_product->get_name(), $cart_item, $cart_item_key ) . '&nbsp;' );
?>
<span class="offerte_qty"><?php echo ' x ';
echo $product_qty;
?>
</span>
<?php
} else {
echo wp_kses_post( apply_filters( 'woocommerce_cart_item_name', sprintf( '<a href="%s">%s</a>', esc_url( $product_permalink ), $_product->get_name() ), $cart_item, $cart_item_key ) );
?>
<span class="offerte_qty"><?php echo ' x ';
echo $product_qty;
?>
</span>
<?php
}

do_action( 'woocommerce_after_cart_item_name', $cart_item, $cart_item_key );
echo wc_get_formatted_cart_item_data( $cart_item );
?>
<div class="product_prijs">
<?php
echo apply_filters( 'woocommerce_cart_item_subtotal', WC()->cart->get_product_subtotal( $_product, $cart_item['quantity'] ), $cart_item, $cart_item_key ); // PHPCS: XSS ok.
?>
<span class="prijs_per_stuk"><?php echo apply_filters( 'woocommerce_cart_item_price', WC()->cart->get_product_price( $_product ), $cart_item, $cart_item_key );
echo ' ';
printf(esc_html__('per stuk', 'astra-child'));
// print_r($_product);
?>
</span>
</div>

</div>
<div class="product-remove">
<?php
echo apply_filters( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
'woocommerce_cart_item_remove_link',
sprintf(
'<a href="%s" class="remove" aria-label="%s" data-product_id="%s" data-product_sku="%s"><img src="https://kreatixlabs.be/rivanco/wp-content/uploads/2021/08/trash.png"></a>',
esc_url( wc_get_cart_remove_url( $cart_item_key ) ),
esc_html__( 'Remove this item', 'woocommerce' ),
esc_attr( $product_id ),
esc_attr( $_product->get_sku() )
),
$cart_item_key
);
?>
</div>
<?php
}
?>
</div>
<?php
}
?>

</div>
</form>
<?php
}

add_shortcode('toon_winkelmand_offerte', 'toon_winkelmand_offerte');?>


My Answer:
To fix the issue with incorrect prices showing on the copied WooCommerce Cart on another page, you need to ensure that all the necessary WooCommerce functions and hooks are properly loaded on the new page.

Here are some steps you can take to troubleshoot and fix the issue:

1. Make sure that all the required WooCommerce scripts and styles are enqueued on the new page where you are displaying the copied cart. You can do this by adding the following code to your theme's functions.php file:

php
add_action('wp_enqueue_scripts', 'enqueue_woocommerce_scripts');
function enqueue_woocommerce_scripts() {
if (function_exists('is_cart') && is_cart()) {
wp_enqueue_script('wc-cart');
wp_enqueue_style('woocommerce-cart');
}
}


2. Check if any custom price calculations or modifications are being applied using hooks or filters in your theme's functions.php file or in a custom plugin. Make sure that these customizations are also applied on the new page where you are displaying the cart.

3. Verify that the WooCommerce global object `$woocommerce` is properly initialized on the new page. You can do this by adding the following code at the beginning of your function:

php
global $woocommerce;


4. Double-check the custom price calculations and modifications that you have implemented in your theme or plugin to ensure that they are correctly applied to the cart items on the new page.

5. If you are using any custom templates or template parts for the cart display, make sure that they are correctly loaded on the new page and that they include all the necessary WooCommerce functions and hooks.

By following these steps and ensuring that all the required WooCommerce functions, scripts, and customizations are properly applied on the new page, you should be able to fix the issue with incorrect prices showing on the copied WooCommerce Cart.

Rate this post

3 of 5 based on 2527 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us