Add Product Thumbnail to My Account - Recent Orders - Woocommerce - php

I was wondering if there is a way to add a product thumbnail into the buyer's 'Recent Orders' page in 'My Account' in Woocommerce frontend.
I've being trying to find some sort of solution, but no luck whats so ever.
I haven't tried anything to give you a code, just because I have no idea how to
actually go about this.
Would someone be able to point me to the right direction ?

You'll need to edit templates/myaccount/my-orders.php. Add the following code to where you want the thumbnails to show.
<?php
// Get a list of all items that belong to the order
$products = $order->get_items();
// Loop through the items and get the product image
foreach( $products as $product ) {
$product_obj = new WC_Product( $product["product_id"] );
echo $product_obj->get_image();
}
?>

Related

How to add/display an attribute for a Simple Product on Checkout Page?

I'm trying to display the Attribute "marke" from a Single Product on Checkout Page but all I get to see is "Array".
<?php $product_attributes = get_post_meta($cart_item['product_id'], '_product_attributes', true); $product_attributes['marke']['value']; ?>
This Code i paste below <?php echo WC()->cart->get_item_data( $cart_item ); ?>
I know its stupid but I'm realy new in php/wocommerce, would be nice if someone could say me what I'm doing wrong there, or what could I change?
Woocommerce Ver. 2.3.7

Display WooCommerce variable product price on other pages

I'm using WooCommerce on a WordPress site that I'm building and I need to be able to display a specific product's price throughout the site. Normally that wouldn't be an issue, but in this instance it's a product which has 2 variations, so I need to show both of them (e.g. £4.99 - £9.99). How can I retrieve these values and echo them out?
Put the following in your theme's functions.php file:
function so_28073705( $product_id ) {
$wc_product_variable = new WC_Product_Variable( $product_id );
$variation_price_html = $wc_product_variable->get_price_html( );
return $variation_price_html;
}
When you want to use it:
<?php echo so_28073705( <product_id> ); ?>
returns:
<span class="amount">$low-price</span>–<span class="amount">$high-price</span>

Prestashop get product attributes from its id

In Prestashop I want to get product's all attribute from its product id. Lets say I have a product with id as 3. Now from product id 3 I want to get all of its attribute like name, price, stock, category, product link.. etc.
So far I have tried
$product_id = $result['id_product'];
$id_product = (int)$product_id;
$product = new Product(Tools::getValue($id_product));
var_dump($product);
But its giving me an array with all attributes in blank. The array can be seen here
So can someone tell me how to get its all attribute from id? Any help and suggestions will be really appreciable. Thanks
Well, one obvious mistake is
Tools::getValue($id_product)
Which is basically
$_GET[$id_product]
So in your case that would probably evaluate to
$product = new Product($_GET['3']);
So remove Tools::getValue and try again:
$product_id = $result['id_product'];
$id_product = (int)$product_id;
$product = new Product($id_product);
var_dump($product);
or you can safe some lines, which is more simple:
$product = new Product( (int)$result['id_product']);
var_dump($product);
Good luck!

Using magento product model in footer

I am trying to display the Swatch attributes of all the associated simple products to the configurable product that the user is viewing.
I need to do this in the footer which is proving more difficult than I thought as a lot of methods etc are not available in the footer.
I have this code which just shows the Swatch attribute for the configurable product, I need this modified to show the Swatch attribute for all the simple products associated to this configurable.
<?php
$SKU = "2726578";
$product = Mage::getModel('catalog/product')->loadByAttribute('sku',$SKU);
echo $product->getSwatch();
?>
$sku = "2726578";
$product = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku);
if($product->getTypeId() == "configurable"){
$childs = $product->getTypeInstance()->getUsedProducts();
}
Hope this helps! Just iterate over the childs and fetch the values.
cheers!

Woocommerce Displaying Products With Product Images

I have a wordpress site running the wordpress plugin WooCommerce. Because of the sheer volume of products this site is handling we have been managing the product list outside of the site and uploading it. A lot of the products don't have images yet but they have a hard coded image url so we can add them when we get them. To get around broken images I just do a little search for the image size and if I can't find it and replace it with a placeholder.
$src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), $size);
if (#getimagesize($src[0])) {
//display product image
} else {
//display placeholder image
}
This is working fine in most cases but now I am working on displaying the products in a category. I want to display all the products with images first and then display the products without images. The problem is once the loop starts if I exclude products without images it will loop through the first 12 products and only display a subset of the 12 that have images. What I want it to do is keep looping until I have 12 products with images (if there are 12 products with images).
This is what I have right now that doesn't work.
<?php if ( have_posts() ) : ?>
<ul class="products">
<?php while ( have_posts() ) : the_post(); ?>
<?php
$src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), $size);
if (#getimagesize($src[0])) {
woocommerce_get_template_part( 'content', 'product' );
}
?>
<?php endwhile; // end of the loop. ?>
</ul>
<?php endif; ?>
Possible logical solutions that I have been unable to code would be to ignore some product while in the loop (so it would make another run if there is no image) or somehow code my query as part of the requirement of the loop i.e. pit it in the $args?
Any help would be greatly appreciated.
I have managed to find a workable solution to my problem. It was just impossible to list the products through separate loops without making a mess of pagination. So the logical step was to use the loop and essentially order the products based on whether or not the image exists. This creates a new problem because the Wordpress ordering cannot determine whether the image link points to a file or not.
You can however set a "Menu order" for a product in woocommerce. Then if you set "Default product sorting" to "Default sorting" under "Woocommerce -> Settings -> Catalog" it will use this menu order to order the products in a catalog view.
Great! But I still have 17000 products and I need to specify a Menu order for each. There was no way I could do this using the native woocommerce tools it would have taken weeks. So I decided to write a little plugin to change the "Menu order" of each product based on whether the image exists or not.
Here is the function used to write to the post database:
/**
* This function sets the value of the menu_order of a product to 0 if the product contains an image and 1 if it does not
* #param {int} $offset this is the start number for the batch
* #param {int} $batch The number of products to process in the batch
*/
function setProductMenuOrder($offset, $batch) {
global $post;
$number_completed = 0;
//define the arguments to be used in the loop
$args = array( 'post_type' => 'product','offset' => $offset, 'numberposts' => $batch );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post);
$src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID)); //getting image source
//define post to be updated
$my_post = array();
$my_post['ID'] = $post->ID;
if (#getimagesize($src[0])) { //if image source points to actual image menu order is set to 0
$my_post['menu_order'] = '0';
wp_update_post( $my_post ); //Update the post into the database
$number_completed+=1;
} else { //if it doesn't menu order is set to 1
$my_post['menu_order'] = '1';
wp_update_post( $my_post ); //Update the post into the database
$number_completed+=1;
}
endforeach;
echo '<p>Number of products edited: <strong>'.$number_completed.'</strong>.</p>';
}
I since I have so many products my plugin processes them in smaller batches. I am managing a batch of around 2000 products at a time without failing. I did have to adjust my php memory limit in config.php
define('WP_MAX_MEMORY_LIMIT', '256M');
I still wonder if there might be an easier way of accomplishing this but for the time being this solution will suffice.

Categories