1

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');?>
wimkreatix
  • 43
  • 7
  • **1/2** At first sight I see 2 mistakes in your current code. **1)** [Shortcodes aren't meant to output anything, they should only return content](https://stackoverflow.com/a/21538351/11987538) **2)** If you use `WC()->cart` , then it is recommended to first check if this is actually available (NOT null), otherwise it will give an error when it is not available (on certain pages). – 7uc1f3r Nov 17 '21 at 18:21
  • **2/2** In your question you state the following: _"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."_ - Is it possible to add a (small) example? so that your question contains [a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) – 7uc1f3r Nov 17 '21 at 18:21

0 Answers0