Woocommerce filter by price - php

I've been trying to show products in the shop page of my woocommerce store, when I use the normal shortcode method and try to use the filter by price function, it doesn't work at all.However, when I use the function woocommerce_content() to output the products, the filter works without any issues.Is there anyway to make sure the filter works with "echo do_shortcode()" method?
Also, woocommerce_content() method outputs all the products and I couldn't find a way to only output sales or most most purchased products? Is there a way to output a specific type of products while using woocommerce_content() ?
<?php echo do_shortcode('[products limit=4 columns=4]'); ?>
<?php get_sidebar();
// or do_action('woocommerce_sidebar');
?>

Related

change HTML class of a PHP element - Woocommerce

I have a Woocommerce store that is using Elementor mini cart.
I would like to add suggested products inside the mini cart, using this php code:
function content_below_products_minicart() {
echo 'You may also like these products';
echo do_shortcode('[products ids="13"]');
};
add_action( 'woocommerce_mini_cart_contents', 'content_below_products_minicart', 10);
And it works fine, but unfortunately the UI is not the best.
So I would like to customize the CSS but the styles of those products are the same as the product archive, so editing that CSS code will also modify the appearance of the products in the Archive.
How can I create an HTML Class that I can edit on its own?

How to add an HTML code to show a logo on the product image

I have a script from a price comparison website that I want to use on my woocommerce product page so that if my product price is cheaper on the price comparison website then a logo will appear on the product image as shown here:
The script that that I have received is like below and they told me to set this script for all product pages. The [article_number] should be replaced by woocommerce SKU. Can some one help me in adding this script in woocommerce to get the desired result? I guess I might need to call an action under functions.php of the child theme not not sure exactly which hook to call etc.
<img src="https://instore.prisjakt.nu/cheapest.php?id=[article_number]&f=123456" />
I tried the below code but its not working
add_action( 'woocommerce_after_single_product_summary' , 'cheap_logo', 5 );
function cheap_logo() {
global $product;
$sku = $product->get_sku();
echo '<img src="https://instore.prisjakt.nu/cheapest.php?id=$sku&f=123456" />';
}

Adding script after woocommerce product title

I want to display a wordpress shortcode after the product title on my woocommerce products page.
Does anyone know how I can do this? I have looked all over but I cannot work out where or how to input this. All I keep doing is breaking the site :(
You can use woocommerce_template_single_title hook. Example is given below.
add_action('woocommerce_template_single_title', 'your_function');
function your_function(){
// Do your stuff
}
Woo Commerce doesn't have a shortcode for displaying a product name but you can easily create your own function.
function displayProductName($item)
{
$productName = get_the_title($item['id']);
return $productName;
}
add_shortcode('product_name', 'displayProductName');
Add the above code to your functions.php file and then use [product_name id="10"] to output the product title, where id is the id of the product in question. The above example uses the get_the_title() in-built WordPress function:
https://developer.wordpress.org/reference/functions/get_the_title/

Custom woocommerce Cart AJAX Update

I am trying to AJAXify my Woocommerce cart but I am failing hard and I really can't find any help or documentation (I searched for days). So any help is desperately appreciated.
I tried two approaches:
Custom AJAX Function
When changing the quantity (on the cart page), a AJAX call is fired (-> admin-ajax.php) which triggers a function in my functions.php:
function setQty() {
global $woocommerce;
WC()->cart->set_quantity( $_POST['itemKey'], $_POST['quantity'], true);
echo json_encode(array('totalCount'=>WC()->cart->get_cart_contents_count(), 'total'=>$woocommerce->cart->total)); }
The echoed JSON String contains the correct number of items in cart but the total amount is 0. When I reload the page the altered quantity is not saved.
Same with
WC()->cart->add_to_cart($product_id, $quantity) or adding WC()->cart->calculate_totals()
I tried it this way so i can format the values I need to update my cart how i want.
What am I missing here in order to get the cart update saved?
The woocommerce built-in AJAX update function doesn't trigger, because I have a custom design and I think it needs a specific HTML-structure in order to work properly but i can't find any documentation or examples. Also I need the updated values (total amount, total count etc.) in a custom format. So far it just passes me "updated fragments" as predefined html code.
How do i need to build the HTML code of my cart.php that the ajax update will work? And how do i alter the returning values?
Thank you very much in advance.
I'm not sure what you'd like to update, but I guess you want to update the totals amount of products in the basket.
This example replaces and updates the HTML
Let's say you have a div where your total amount is in:
<div class="cart-totals"><?php echo WC()->cart->get_cart_contents_count(); ?></div>
Add this to your functions.php:
// Mini Cart update with AJAX
add_filter( 'woocommerce_add_to_cart_fragments', 'custom_cart_count_fragments', 10, 1 );
function custom_cart_count_fragments( $fragments ) {
$fragments['div.cart-totals'] = '<div class="cart-totals">' . WC()->cart->get_cart_contents_count() . '</div>';
return $fragments;
}
Note: You are replacing not adding
Is it a mini cart/counter you are trying to create or a full page to review/remove products?
If it is a mini-cart/cart counter you wanted, I've created a repo under https://github.com/samisonline/ajax_cart for you that has the basic functionality baked in. This may not be the exact format you're looking for, but it shows the woocommerce functionality at work, as well as the needed markup for AJAX to work!

getShippingIncludeTax - the right way to code

Im building a webshop on Magento Platform (still localhost) and would like to have the shipping displayed in my header cart.
Currently the shipping rate is shown okay in the main cart but in the header cart it's displayed withouth tax.
This is the code of the main cart: ( the right one with tax included)
<?php echo $this->helper('checkout')->formatPrice($this->getShippingIncludeTax()) ?>
This is the code of the header cart: ( displayed without tax)
<?php echo Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress()->getShippingAmount(); ?>
As you can see the "getShippingIncludeTax" should be added instead of just the amount.
Any ideas how to implement this code together?
Extra:
This code also works for the header, but has the same amount withouth tax.
<?php $totals = Mage::getSingleton('checkout/session')->getQuote()->getTotals();
if(isset($totals['shipping']))
print __(number_format($totals['shipping']->getDat('value'),2)); ?>
would that be better ?
<?php echo Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress()->getShippingIncTax(); ?>
It's actually rather easy to see whats inside your object and what you can ask from it
<?php print_r(array_keys(Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress()->getData()));?>

Categories