Woocommerce ajax message issue - ajax

I've got this working well except for one small problem.
I have a custom archive-product.php page which shows each product with a quantity selector...
<ul class="woo-products">
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => 100, 'product_cat' => 'hair-extensions' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<li class="product">
<a href="<?php echo get_permalink( $loop->post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">
<h3><?php the_title(); ?></h3>
<?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="300px" height="300px" />'; ?>
<?php echo the_excerpt(); ?>
<span class="price"><?php echo $product->get_price_html(); ?></span>
</a>
<?php // woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>
<?php
add_action('woocommerce_after_shop_loop_item','woocommerce_template_single_add_to_cart');
remove_action('woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
do_action( 'woocommerce_after_shop_loop_item' );
?>
</li>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</ul>
If I add a product from there, it goes into my cart no problem. But if I then click on the product to visit it's own page, I see a "this product was added to your cart" message.
Likewise if while on the shop page I add several different products they go into my cart, but if I visit one of those product pages I will see "this product was added to your cart" message several times (once for each product I added to my cart).
Here's an image which might help with my description of the problem: http://i.imgur.com/hou3ZRW.png
In my code above if I un-comment out this line...
woocommerce_template_loop_add_to_cart( $loop->post, $product );
...and remove these lines...
add_action('woocommerce_after_shop_loop_item','woocommerce_template_single_add_to_cart');
remove_action('woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
do_action( 'woocommerce_after_shop_loop_item' );
...it works fine except that I don't have the quantity selector option any more.
Any suggestions as to how to stop the "this product was added to your cart" message appearing when I visit a product page after adding it to the cart would be helpful.

This helped me get around the problem, but including the quantity selectors using a different method the problem went away:
http://christianvarga.com/how-to-add-quantity-to-product-archives-in-woocommerce-and-keep-ajax/

There is a function in WooCommerce called wc_clear_notices That does exactly what you need.
It was introduced in WooCommerce 2.1, you can see the very brief documentation here: http://hookr.io/functions/wc_clear_notices/
See Woocommerce cart notice showing multiple times for a way to implement it.

Related

Custom shipping method place with choosing point of receive order - Woocommerce Checkout

I want to change place of standard Woocommerce shipping method area. In standard Shipping methods area is in review-order.php in table.
I placed that in form-checkout.php. Ajax is working, price of shipping methods updates in table with summary, but some shipping methods add custom options, for example one of them add new field with select and load maps with points where I can receive package. It is working with ajax too, but when I choose another option, field from previous option is still visible - it should disappear.
Code which shows shipping method placed on top of form checkout:
<form name="checkout" method="post" class="checkout woocommerce-checkout main-form-checkout" action="<?php echo esc_url( wc_get_checkout_url() ); ?>" enctype="multipart/form-data">
<div class="row">
<div class="col-12 col-sm-6">
<?php if ( WC()->cart->needs_shipping() && WC()->cart->show_shipping() ) : ?>
<?php do_action( 'woocommerce_review_order_before_shipping' ); ?>
<div class="c-shipping-methods">
<?php wc_cart_totals_shipping_html(); ?>
</div>
<?php do_action( 'woocommerce_review_order_after_shipping' ); ?>
<?php endif; ?>
...
I added function in functions.php which update order review fragments:
function woocommerce_checkout_shipping_options( $deprecated = false ) {
wc_get_template(
'checkout/shipping-options.php',
array(
'checkout' => WC()->checkout(),
)
);
}
add_action( 'woocommerce_checkout_shipping_option', 'woocommerce_checkout_shipping_options', 10 );
function my_custom_shipping_table_update2( $fragments ) {
ob_start();
woocommerce_checkout_shipping_options();
//wc_cart_totals_shipping_html();
$woocommerce_checkout_shipping_options = ob_get_clean();
$fragments['.c-shipping-methods'] = $woocommerce_checkout_shipping_options;
return $fragments;
}
add_filter( 'woocommerce_update_order_review_fragments', 'my_custom_shipping_table_update2');
So what can I do to delete shipping options fields when I choose another shipping method?
In addition.. when I place
<?php if ( WC()->cart->needs_shipping() && WC()->cart->show_shipping() ) : ?>
<?php do_action( 'woocommerce_review_order_before_shipping' ); ?>
<div class="c-shipping-methods">
<?php wc_cart_totals_shipping_html(); ?>
</div>
<?php do_action( 'woocommerce_review_order_after_shipping' ); ?>
<?php endif; ?>
in any place of table summary in review-order.php it's working properly..

Remove text "item/items" from Storefront mini cart in header, with ajax enabled

In Storefront, there is a mini cart in the header, where the price and number of items are displayed. I need to remove the word "item"/"items" from the .count element. I tried overriding storefront_cart_link() with the following:
function storefront_cart_link() {
if ( ! storefront_woo_cart_available() ) {
return;
}
?>
<a class="cart-contents" href="<?php echo esc_url( wc_get_cart_url() ); ?>" title="<?php esc_attr_e( 'View your shopping cart', 'storefront' ); ?>">
<?php /* translators: %d: number of items in cart */ ?>
<?php echo wp_kses_post( WC()->cart->get_cart_subtotal() ); ?> <span class="count"><?php echo wp_kses_data( WC()->cart->get_cart_contents_count() ); ?></span>
</a>
<?php }
The code above works but when the page is loaded it says "items" after the count number again. I guess it has something to do with AJAX. As the word isn't wrapped in its own element, I can't hide it with CSS. How do I remove it while keeping the same functionality (if possible)?
I found that the problem was caused by Woocommerce Cart Fragments. The fragments does not update until the contents of the cart are modified. I didn't understand this because making a hard refresh did not work. I modified the storefront_cart_link() and Woocommerce template file mini-cart.php,. After adding/removing products in the cart, the fragments were updated and it worked as expected.

Update woocommerce shopping cart icon dynamically with ajax

I Have searched for an answer to this, but not been able to find one.
In my woocommerce shop, i want a basket icon at the top of the site. When a customer puts something in the cart, they want to swap this icon for a different one - this icon ("not empty") will stay as long as the cart is not empty.
What I'm after is a PHP-type if / else call, which says "if the cart is empty, display icon A, else display icon B". If anyone could shine some light on this, I'd be most grateful!
I am able to update the cart text dynamically following this woocommerce tutorial,
http://docs.woothemes.com/document/show-cart-contents-total/
In my Page
<?php global $woocommerce;
if($woocommerce->cart->get_cart_total()=='0') { ?>
<img src="<?php echo get_stylesheet_directory_uri(); ?>/images/empty_cart.png" alt="" width="30" height="30">
<?php }else{ ?>
<img src="<?php echo get_stylesheet_directory_uri(); ?>/images/full_cart.png" alt="" width="30" height="30">
<?php } ?>
<a class="cart-contents" href="<?php echo $woocommerce->cart->get_cart_url(); ?>" >Your Cart : <?php echo sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);?> - <?php echo $woocommerce->cart->get_cart_total(); ?></a>
In Functions.php
add_filter('add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment');
function woocommerce_header_add_to_cart_fragment( $fragments ) {
global $woocommerce;
ob_start();
if($woocommerce->cart->get_cart_total()=='0') { ?>
<img src="<?php echo get_stylesheet_directory_uri(); ?>/images/empty_cart.png" alt="" width="30" height="30">
<?php
}else{ ?>
<img src="<?php echo get_stylesheet_directory_uri(); ?>/images/full_cart.png" alt="" width="30" height="30">
<?php }
?>
<a class="cart-contents" href="<?php echo $woocommerce->cart->get_cart_url(); ?>" >Your Cart : <?php echo sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);?> - <?php echo $woocommerce->cart->get_cart_total(); ?></a>
<?php
$fragments['a.cart-contents'] = ob_get_clean();
return $fragments;
}
currently it is showing both the images no matter what is in the cart.
You can see the implementation of the above at
http://fungass.com/testing/shop/uncategorized/abc/
Finally solved it like this,
<?php global $woocommerce; ?>
<a href="<?php echo $woocommerce->cart->get_cart_url(); ?>" ><?php if ($woocommerce->cart->cart_contents_count == 0){
printf( '<img src="%s/images/empty.png" alt="empty" height="25" width="25">', get_stylesheet_directory_uri());
}else{
printf( '<img src="%s/images/full.png" alt="full" height="25" width="25">', get_stylesheet_directory_uri());
}
?> </a>
<a class="cart-contents" href="<?php echo $woocommerce->cart->get_cart_url(); ?>" >
Your Cart : <?php echo sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);?> - <?php echo $woocommerce->cart->get_cart_total(); ?></a>
I would actually recommend that you use cookies & JavaScript to handle this functionality for the added benefit of allowing caching plugins to serve static pages on non-WooCommerce pages but still have some dynamic pieces on the page (like cart totals or just simply a populated cart icon).
On the PHP side of things, on init, check for cart totals and set a cookie value...
// this should only ever fire on non-cached pages (so it SHOULD fire
// whenever we add to cart / update cart / etc
function update_cart_total_cookie()
{
global $woocommerce;
$cart_total = $woocommerce->cart->cart_contents_count;
setcookie('woocommerce_cart_total', $cart_total, 0, '/');
}
add_action('init', 'update_cart_total_cookie');
On the JavaScript side of things, examine the cookie and then change the icon...
// use the custom woocommerce cookie to determine if the empty cart icon should show in the header or the full cart icon should show
// *NOTE: I'm using the jQuery cookie plugin for convenience https://github.com/carhartl/jquery-cookie
var cartCount = $.cookie("woocommerce_cart_total");
if (typeof(cartCount) !== "undefined" && parseInt(cartCount, 10) > 0) {
$(".full-cart-icon").show();
$(".empty-cart-icon").hide();
// optionally you can even use the cart total count if you want to show "(#)" after the icon
// $(".either-cart-icon span").html("(" + cartCount + ")");
}
else {
$(".full-cart-icon").hide();
$(".empty-cart-icon").show();
}
Hope this helps a bit! Have fun!

Display pricing and add to cart button in related products Virtuemart 2.0

I would like to display the related product pricing and have add to cart button along with each of the related products.
Below is the code snippet from the related products page. The $field does not have any pricing available. How can I show the pricing and "add to cart" button? Thanks in advance
<?php
foreach ($this->product->customfieldsRelatedProducts as $field) {
?><div class="product-field product-field-type-<?php echo $field->field_type ?>">
<span class="product-field-display"><?php echo $field->display ?></span>
<span class="product-field-desc"><?php echo jText::_($field->custom_field_desc) ?></span>
</div>
<?php } ?>
I have found solution here and it works for me:
no need to edit core files
It requires copying the "default_relatedproducts.php", "default_showprices.php" and "default_addtocart.php" to your "template/html/com_virtuemart/productdetails" folder. Then replace all of the code in the "default_relatedproducts.php" with the following code:
<?php
// Check to ensure this file is included in Joomla!
defined ( '_JEXEC' ) or die ( 'Restricted access' );
$model = new VirtueMartModelProduct();
$calculator = calculationHelper::getInstance();
$currency = CurrencyDisplay::getInstance();
?>
<div class="product-related-products">
<h4><?php echo JText::_('COM_VIRTUEMART_RELATED_PRODUCTS'); ?></h4>
<div>
<?php
foreach ($this->product->customfieldsRelatedProducts as $field) {
?>
<div class="product-field">
<?php
$product = $model->getProductSingle($field->custom_value,true);
?>
<h2><?php echo JHTML::link ($product->link, $product->product_name); ?></h2>
<a title="<?php echo $product->product_name ?>" rel="vm-additional-images" href="<?php echo $product->link; ?>">
<?php
echo $this->product->images[0]->displayMediaThumb('class="browseProductImage"', false);
?>
</a>
<div class="short_desc"><?php echo $product->product_s_desc; ?></div>
<?php include 'default_showprices.php'; ?>
<?php include 'default_addtocart.php'; ?>
</div>
<?php } ?>
</div>
</div>
Had a same problem. But I had to show only the price.
So the fastest way is to change sql select statement in customfields.php
Path in Joomla 2.5 for Virtuemart 2.0 administrator/components/com_virtuemart/models/customfields.php
line 548 of
public function getProductCustomsFieldRelatedProducts($product)
change only
$query=
with
'SELECT C.`virtuemart_custom_id` , `custom_parent_id` , `admin_only` , `custom_title` , `custom_tip` , C.`custom_value`
AS value, `custom_field_desc` , `field_type` , `is_list` , `is_hidden` , C.`published` , field.`virtuemart_customfield_id` ,
field.`custom_value`, field.`custom_param`, price.`product_price`, field.`ordering`
FROM `#__virtuemart_customs` AS C
LEFT JOIN `#__virtuemart_product_customfields` AS field ON C.`virtuemart_custom_id` = field.`virtuemart_custom_id`
LEFT JOIN `#__virtuemart_product_prices` AS price ON
field.`custom_value` = price.`virtuemart_product_id`
Where field.`virtuemart_product_id` ='.(int)$product->virtuemart_product_id.' and `field_type` = "R"';
After all on line 559 change
$field->custom_price
to
$field->product_price
And finally...
In template view of product description insert the code below to show the prices of related products
<?php echo $field->product_price ?>
The only problem with the below solution is that it doesn't display the correct images for the related products. It's using the main products image and just repeating it.

Magento view related products in list.phtml

I have the following requirement;
I want to show a products configured related products on the category listing page (list.phtml).
I figured I could make some call on the list.phtml inbetween the for each loop for each product using the [b]$_product[/b] variable but I can't seem to populate the relatedProductCollection
sorry new to all this is it even possible.
<?php foreach ($_productCollection as[b] $_product[/b]):?>
<li class="item<?php if( ++$_iterator == sizeof($_productCollection) ): ?>last<?php endif; ?>" >
<div class="product-shop">
<div class="f-fix">
<?php $product->getRelatedProductCollection(); ?>
</li>
<?php endforeach; ?>
$product has been used to call the getRelatedProductCollection function.
but,
In that foreach loop you have taken it as $_product.
Can you see the difference ??
Underscore is missing. This is taken from your code. Make the variable same in both the places and try.

Resources