<?php woocommerce_cart_totals(); ?> is somehow displaying twice - php

Woocommerce shopping cart page is acting very strange.
I just updated my theme ( X | Theme v3.2.5 ) and woocommerce ( v2.3.8 )
So I am running the most current versions.
For some reason, since the update my cart totals are now displaying twice on my cart page.
I have two subtotal columns, two proceed to checkout buttons, two empty cart buttons, etc.
When I look at the HTML, it's literally duplicated.
<div class="cart-collaterals">
<div class="cart_totals ">
<div class="cart_totals ">
</div>
Obviously cart_totals has more HTML underneath, but you get the idea.
On the actual cart.php page I can locate this php:
<?php woocommerce_cart_totals(); ?>
only one time. Here is all the markup from the cart.php page:
<div class="cart-collaterals">
<?php do_action( 'woocommerce_cart_collaterals' ); ?>
<?php woocommerce_cart_totals(); ?>
</div>
<?php do_action( 'woocommerce_after_cart' ); ?>
Is there any way to ensure that this php is only being ran one time?
I might try to just limit the height of the CSS container, but that's not the right fix...

In their latest update, they must have moved the cart total to the woocommerce_cart_collaterals, which means it is now printed once in this action, and once in your code.

I had the same problem with TM Extra Options plugin on Themeforest. In the plugin remove <?php woocommerce_cart_totals(); ?> in the templates/cart/cart.php file (it's at the bottom) and you will remove duplicate "Cart Totals". Hope this helps someone.

Related

Ajax remove coupon from mini-cart WooCommerce

I am using the mini-cart.php file and the cart widget to show cart content and I am using AJAX add to cart standard built in WooCommerce.
Adding products to my cart works fine. I added an area to show if any coupon is added (see my code). Works all fine but when deleting the coupon I want it to be done without loading a next page. It only needs to update the mini-cart.php. Can someone help me? Basically I need some function or something which updates mini-cart.php without reloading the page.
<?php foreach ( WC()->cart->get_coupons() as $code => $coupon ) : ?>
<p>Actieve kortingscode: <?php echo esc_attr( sanitize_title( $code ) ); ?></p>
<p><?php wc_cart_totals_coupon_html( $coupon ); ?></p>
<?php endforeach; ?>
You will find the answer to your question in WooCommerce documentation
https://docs.woocommerce.com/wc-apidocs/source-class-WC_Cart.html
The short answer is you need to make a call to WC()->cart->remove_coupons();
As of the "how", check this SO answer (different thing but same way to resolve it)
https://stackoverflow.com/a/41593818/10864482

Woocommerce Multiple Checkout Pages

rSo I have been banging my head against my computer trying to figure out how to get this to work and am wondering if this is possible (well anything is possible with a bit of work).
My end goal is to have more than one page that holds the Woocommerce checkout form so that I can have a page for a custom one-page subscription checkout.
This works fine when I set the checkout form to be this page via the settings panel but if not set to this page it will not load the form with the proper scripts and is unable to perform ajax requests to update shipping etc, thus being restrictive to one page per working checkout form.
Steps taken:
So far I have tried bringing the form using the following :
<?php echo do_shortcode('[woocommerce_checkout]'); ?>
Through the Page's WYSIWYG (not a solution in my case eithey way)
And Manually building the form via the provided functions
<div class="checkout">
<div class="col2-set" id="customer_details">
<div class="col-1">
<?php echo $woocommerce->checkout->checkout_form_shipping() ?>
</div>
<div class="col-2">
<?php echo $woocommerce->checkout->checkout_form_billing()?>
</div>
</div>
<h3 id="order_review_heading">Your order</h3>
<div id="order_review" style="zoom: 1; position: relative;">
<?php echo wc_get_template( "checkout/review-order.php", array( "checkout" => WC()->checkout() ) ) ?>
</div>
All of these steps brought in the form but with the script/ajax errors. Please if anyone has accomplished this in the past I would love to hear what it takes. Suggestions are always welcome too! Thanks in advanced :)
I had the same problem, I fixed it by using this at the top of my custom checkout page:
if ( ! defined( 'WOOCOMMERCE_CHECKOUT' ) ) {
define( 'WOOCOMMERCE_CHECKOUT', true );
}

Magento - Check if item has specific custom option or not in shopping cart

In my shopping cart page i need to be able to check if any added products have a particular custom option set on them - in this case a custom option called 'age_guide'.
If so i need to add a small disclaimer text line. How would i go about doing so?
If your custom option set on a product is age_guide, then in your
template/checkout/cart/item/default.phtml
around line 46
<?php echo $this->htmlEscape($_option['label']) ?>
Use below code:
<?php if($_option['label'] =='age_guide'): // or whatever your custom option label ?>
<div>
<span><?php echo $this->__('Your Disclaimer Text Goes Here');?></span>
</div>
<?php endif; ?>

How to modify woocommerce_before_cart action

I'm trying to make my woocommerce cart template display as a full 12 column layout.
The existing layout is using bootstrap's col-sm-8 column. I need to change it to col-sm-12.
<main class="main col-sm-8" role="main">
<div class="page-header">
<h1>Cart</h1>
</div>
<div class="woocommerce">...</div>
<div class="woocommerce-info">...</div>
<div class="cart-collaterals">
// shipping code etc.
</div>
</main>
I checked out the relevant woo-templates shown here, and copied the cart.php template into my theme to override. However, it looks like I need to modify the woocommerce_before_cart action to change the <main> layout and insert the col-sm-12 class. I found the relevant actions on this woocommerce page.
I can see from the cart.php template the action called before the <form> element as shown below:
global $woocommerce;
wc_print_notices();
do_action( 'woocommerce_before_cart' ); ?>
<form action="<?php echo esc_url( WC()->cart->get_cart_url() ); ?>" method="post">
<?php do_action( 'woocommerce_before_cart_table' ); ?>
<table class="shop_table cart" cellspacing="0">enter code here
I'm new to php, my question is how do I modify the output of this action so I can change the layout to 12 columns?
Woocommerce inserts the content in cart.php into page.php in the root of your theme. :)
Doesn't look like woocommerce creates action hooks for 'woocommerce_before_cart' or 'woocommerce_before_cart_table', you can check this with has_action(). They seem to be there as suggestions for developers to extend upon. Should be right to remove them from cart.php (although developers might have them there for future releases or popular plugins) or if you want to use them add this to your themes functions.php.
add_action('woocommerce_before_cart', 'sample', 1);
function sample() {
echo '<h1>hello</h1>';
}
EDIT:
Just read your response to the previous answer, looks like that theme you're using might be creating the hook in its functions.php file, look for
add_action('woo_commerce_before_cart', 'sample', X);
'sample' is the name of the function that is called and X is its priority. You can either modify the output of that function or add another function to the hook.
For anyone who works with child theme, please note that sometimes your parent theme already override the cart.php template, especially heavily customised like Themeforest products. So don't copy the original cart.php from Woocommerce, copy it from the parent theme template.

Where is 'addtocart' for configurable products?

Currently, when a product is out of stock, the 'addtocart' block disappears, but I want to change that so instead, it still displays the 'addtocart' block, but instead of the actual 'add to basket' button, it displays some text explaining that the product is out of stock.
I have managed to get this to work on simple products by taking
<?php echo $this->getChildHtml('addtocart') ?>
out of the
if ($_product->isSaleable())
section, and editing the 'addtocart' block so that if the product isn't saleable it displays the required text instead of the 'add to basket' button.
My issue is, for configurable products, whatever code dictates whether or not the 'addtocart' block is displayed isn't particularly obvious, I have looked all over view.phtml and load of other files, and grep'd through the entire directory, but the section I have already altered for simple products is the only one that shows up (in view.phtml)...
Any ideas?
it's the same code (if ($_product->isSaleable())) but in the file /app/design/frontend/base/default/template/catalog/product/view/addtocart.phtml
Turns out it was much simpler than I thought, in the 'view.phtml' file there were two sections of code:
<?php if ($_product->isSaleable() && $this->hasOptions()): ?>
<?php echo $this->getChildChildHtml('container2', '', true, true) ?>
<?php endif; ?>
Which grabs 'container2' if the product is BOTH a configurable product '$this->hasOptions()' AND is saleable. Container2 has the block for displaying configurable options, related products (but just for configurable products, the line of code above this code displays the related products for simple products), and the addtocart block. Below this code I had:
<?php if (!$this->hasOptions()): ?>
<div class="add-to-box">
<?php echo $this->getChildHtml('addtocart') ?>
<?php if ($_product->isSaleable()): ?>
<?php if ($this->helper('wishlist')->isAllow() || $_compareUrl = $this->helper('catalog/product_compare')->getAddUrl($_product)): ?>
<?php endif; ?>
<?php endif; ?>
Which displays the addtocart block if the product is a simple product 'if (!$this->hasOptions()'. I had to add the following bit of code as there was nothing which told the server what to do if the product was configurable but NOT saleable:
<?php if (!$_product->isSaleable() && $this->hasOptions()){ ?>
<?php echo $this->getChildHtml('addtocart') ?>
<?php } ?>
I'm fairly new to php so that was probably pretty obvious for a lot of people, but I managed to miss it for a good week or so (lucky this isn't my full time job!), hopefully this helps others!

Categories