Showing the number of items in WooCommerce mini cart - php

I am creating a cart link for my mobile header. I just want to show number of cart items after the cart icon. No text, only a number.
How can I do it?
My code is something like this:
<div class="mobile-cart">
<a href="mywebsite.com/cart">
<img src="cart.png">
Here should be the code for no. of items
</a>
</div>
Thanks.

You just need to use the method get_cart_contents_count() from WC_cart class, this way:
<div class="mobile-cart">
<a href="mywebsite.com/cart">
<img src="cart.png">
<span><?php echo WC()->cart->get_cart_contents_count( ); ?></span>
</a>
</div>
Reference: WC_Cart class - get_cart_contents_count() method

Related

How to add 'view cart' section to a page using woocomerce?

I would like to make a section at the top of my page for customers to be able to access their cart. Is there a way of doing this?
Thanks
You can add view cart button anywhere you want, here is the simple html :
<div style="text-align: right;">
<p>
<a href="http://site_name/shop/cart/" class="button">View Cart
<a href="http://site_name/shop/checkout/" class="button checkout">Checkout
</p>
</div>

"Undefined" is display with discount price in magento

I am using magento 1.8.1 and i am trying to display discount percent with sell price on frontend.
with the help of this site discount
i am putting these code:
<?php // Discount percents output start ?>
<?php if($_finalPrice < $_price): ?>
<?php $_savePercent = 100 - round(($_finalPrice / $_price)*100); ?>
<p class="special-price yousave">
<span class="label"><?php echo $this->__('You Save:') ?></span>
<span class="price">
<?php echo $_savePercent; ?>%
</span>
</p>
<?php endif; ?>
in this page: app/design/frontend/yourpackage/yourtheme/template/catalog/product/price.phtml
but after that the result is showing as like this:
Now i am not able to find why "undefined" is showing here and how to remove this.
Please tell me how to remove this.
I seen in your question image you need this effect on product detail page so have to edit media.phtml
app\design\frontend\YOUR_PACKAGE\YOUR_THEME\default\template\catalog\product\view\media.phtml
And in that you have to do same coding to get this value. you can make wrapper class which is override to product image with some dynamic text over product image like as you want.
You can use css like in this page
I think its work...try in this way

Display current value of PHP variable in realtime using js/jQuery

This might seem easy enough for the lovely experts here at SO :) but I can't find a decent answer/question about this on SO or google. Pls help. :)
How do i display a current value of a php variable in realtime via AJAX/js/jQuery
Basically im trying to do an E-commerce site and I am storing the shopping cart contents using a session and displaying it with an echo statement counting how many contents is currently in the basket, however when they are on a certain product and tried adding it in the cart, the value of the cart doesn't echo the added item until there is a page refresh, so i need something like an AJAX to display the updated value of the session when an item is added without having a page refresh.
Thank you so much.
Here is my code snippet for the cart as a sample:
// Assign shopping cart ($_SESSION['cart']) into variable
$basket = count($_SESSION['cart']);
And for displaying:
<!-- Display contents of the shopping cart -->
<ul id="basket" class="clearfix">
<li class="top">
<a href="checkout.php" class="top_link">
<span>Items: <? echo $basket ?></span>
</a>
</li>
</ul>
Thank you so much.. kindly provide a sample code on how to achieve this (either js or jQuery) and would help as well for best practices in dealing with this type of scenario.
First solution (based on your request - BAD IDEA)
get-count-items.php
<?php
session_start();
die(count($_SESSION['cart']));
?>
Javascript
<script>
setInterval(function(){
$.post('get-count-items.php', {}, function (total_items){
$('#total-items').html(total_items);
});
}, '1000');
</script>
HTML
<ul id="basket" class="clearfix">
<li class="top">
<a href="checkout.php" class="top_link">
<span>Items: <span id="total-items"><? echo $basket ?></span></span>
</a>
</li>
</ul>
This javascript will make a request to server at each 1 second.
Second solution (event on button "Add to cart")
add-to-cart.php
<?php
session_start();
// code for add to cart...
die(count($_SESSION['cart']));
?>
Javascript
<script>
$('.add-to-cart').click(function(){
$.post('add-to-cart.php', {}, function (total_items){
$('#total-items').html(items);
});
return false;
});
</script>
HTML
<ul id="basket" class="clearfix">
<li class="top">
<a href="checkout.php" class="top_link">
<span>Items: <span id="total-items"><? echo $basket ?></span></span>
</a>
</li>
</ul>
<a href="" class="add-to-cart">Add to cart</>

having trouble with jquery .context and .index

here is my PHP code to generate a dynamic product table:
while($item = mysqli_fetch_array($result))
{
enter code here
$i++;
$pic = "cartimg/".$item[2];
echo "<div class='prod_box'".$i.">
<div class='center_prod_box'>
<div class='product_title'><a>".$item[1]."</a></div>
<div class='product_img'><a ><img src='".$pic."' alt='' border='0' /></a></div>
<div class='prod_price'><span class='reduce'>".$item[3]."$</span> <a class='price'>".$item[4]."$</a></div>
</div>
<div class='prod_details_tab'> <a class='prod_buy'>Add to Cart</a> <a class='prod_details'>Details</a> </div>
</div>";
}
?>
and here is my jQuery to respond to the clicked event to add the product into the shopping cart
enter code here
cart.addBuyButton(".prod_buy",{
name:'MacBook', // Item name appear on the cart
thumbnail:'media/macbook.jpg', // Thumbnail path of the item (Optional)
price:$("span").index(0), // Cost of the item
shipping:20
// Shipping cost for the item (Optional)
});
prettyPrint();
and here is the query for addBuyButton function
enter code here
self.addBuyButton=function(target,data){$(target).click(function(){self.cart.add(data)
the problem is that, I will have 10 containers, contain 10 products, with the same class name and Id names, and I cant figure out how to read the amount of ".$item[4]."$ if the customer clicked on Add to Cart of different products.
right now the function inserts 12 as the price.
please help me out here, I have surf through many jQuery tutorials but wasn't lucky to find a way.
thanks
Instead of having an addBuyButton for each item on the jQuery I would have just one event listener that handles all of it like this (I personally don't like huge chunks of php in quotes because my IDE doesn't color highlight so pardon my php syntax. You don't have to follow it if you don't like it):
PHP
<div id="products">
<?php while ($item = mysqli_fetch_array($result)) : ?>
<!-- additional code here -->
<div class="product prod_box<?php echo ++i; ?>">
<div class="center_prod_box">
<div class="product_title"><?php echo $item[1] ?></div>
<div class="product_img"><img src="cartimg/<?php echo $item[2] ?>" border="0" /></div>
<div class="prod_price">
<span class="reduce"><?php echo $item[3] ?>$</span>
<a class="price"><?php echo $item[4] ?>$</a>
</div>
</div>
<div class="prod_details_tab">
<a class="prod_buy">Add to Cart</a>
<a class="prod_details">Details</a>
</div>
</div>
<?php endwhile; ?>
</div>
JAVASCRIPT
$('#products').on('click', '.prod_buy', function() {
var product = $(this).closest('.product');
cart.add({
name: product.find('.product_title').first().text(),
thumbnail: product.find('.product_img').first().text(),
price: product.find('.price').first().text(),
shipping: 20
});
});
So that's one event listener for ALL of your prod_buy buttons and it gets the values for the product out of the DOM and adds it to the cart.
If you need any further help add a comment to this post and I'll add more to my answer.

add product link to sku (or product name) in new order mail

hy,
i'm trying to add a link to the new order email that the customer is getting when he is placeing a new order in magento (my version 1.6.2.0 )
i have edited /public_html/app/design/frontend/base/default/template/email/order/items/order/default.phtml
with the following:
<?php $_item = $this->getItem() ?>
<?php $_order = $this->getItem()->getOrder() ?>
----
<!-- Start of edit file -->
<a href="<?php echo $this->getProductUrl($_item) ?>">
<?php echo $this->htmlEscape($this->getSku($_item)) ?></a>
When i receive the confirmation email in the sku column the color changes form black ( default css ) to light blue link like, but it does not have any link property as shown below:
email_photo
I have also tried :
<a href="<?php echo $this->getUrlPath($_item) ?>">
<?php echo $this->htmlEscape($this->getSku($_item)) ?></a>
and i end up with the same thing.
Can anyone tell me what i'm doing wrong ?
Thanks.
In line
<a href="<?php echo $this->getUrlPath($_item) ?>">
$this is an instance of block *Mage_Sales_Block_Order_Email_Items_Order_Default*. It does not have a function getUrlPath() or getProductUrl.
You should use your $_item variable to get a product object and then it to get its URL
$_item->getProduct()->getProductUrl()
I earlier tried this code:
<?php echo $this->htmlEscape($this->getSku($_item)) ?>
Magento 2
To link a product name with its product page in your order emails, edit following files:
Magento_Sales/templates/email/items/order/default.phtml
Magento_Sales/templates/email/items/invoice/default.phtml
Magento_Sales/templates/email/items/shipment/default.phtml
To get the product URL, use the following snippet of code and insert it into a href attribute of the link.
<?= $_item->getProduct()->getProductUrl(); ?>
For example,
<p class="product-name"> <?= $block->escapeHtml($_item->getName()); ?> </p>
The result of the snippet of code will be a clickable product name in Magento Emails.
Tutorial from: https://themes.email/magento/product-links-in-magento-order-emails.html

Categories