Display Product Image from WooCommerce plugin using a custom plugin - php

I'm trying to learn WordPress and developing custom plugins. I'm trying to make a custom plugin where it will display just the Product Image from the Woocommerce plugin to the page. I have 1 product, and I've been reading documentation and haven't really found anything that explains it clearly or works. This is all I have now
<?php
$gallery_shortcode = '[gallery id="' .intval($post->$post_parent).'"]';
print apply_filters('the_content', $gallery_shortcode);
?>

Honestly the question is not very clear, so I'll try to guess what you're looking for.
You can "get" a product image with this function:
woocommerce_get_product_thumbnail();
If you need it in a custom single product PHP template, simply:
echo woocommerce_get_product_thumbnail();
If you need it in a shortcode inside the single product page:
add_shortcode( 'product_image', 'bbloomer_product_image_shortcode' );
function bbloomer_product_reviews_shortcode() {
return woocommerce_get_product_thumbnail();
}
...and then use [product_image] shortcode to show the image

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?

Turn custom Woocommerce shortcodes into php code for archive-product.php

Tl;dr: I want to use similar functionality in WC shortcodes but within the WC shop page.
I have many custom Woocommerce shortcodes I have created. I use them throughout non-shop pages.
E.g:
function displayProductIngredients($item) {
$product = wc_get_product($item['id']);
return $product->get_attribute( 'ingredients' );
}
add_shortcode('product_ingredients', 'displayProductIngredients');
I would like to use them in the archive-product.php page too.
E.g. in the example this shortcode adds the content of the "ingredients" attribute. How can the same be added for each new product added?
Is this just a case of moving around some of the php? If so, how might it look?
Thank you very much.

Post name and custom fields in Wordpress Shortcode

I'm using shortcodes in my Wordpress page to display a responsive Google Map centered at specific location. To do it I'm using shortcode supported by external Wordpress plugin. Such shortcode is let's say [map address="New York"].
As all my Wordpress posts titles are names of the cities, I would like to automatize the process and be able to display the titles of the post in place of the address parameter in [map] shortcode.
Is it possible? Is there any way to do it? As far as I know nesting code or variables in shortcode isn't supported by Wordpress but maybe there is some kind of a workaround.
The same behavior I'd like to accomplish for 'custom field' - really similar situation.
I'd appreciate any help and any advice
Try to put this code to functions.php file in your theme:
function mymap_shortcode() {
echo do_shortcode('[map address="'.esc_html( get_the_title() ).'"]');
}
function mymap_shortcodes_init() {
add_shortcode('mymap', 'mymap_shortcode');
}
add_action('init', 'mymap_shortcodes_init');
And then in posts/pages (or even put it directly to template, or create another filter to add that) use shortcode [mymap] that will run [map] shortcode with current page title as parameter.

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/

Adding Image to custom woocommerce Taxonomy with ACS

I added a custom taxonomy called "Topics" to woocomerce products and Now i would like to add an image to each Topic and have it shown on the page of that Topic.
I am trying to use Advanced Custom fields plugin but I do not know where to the the ACF php code.
Any help would be greatly appreciated.
Advanced Custom Fields Example Link Here https://www.advancedcustomfields.com/resources/code-examples/
get the ACF image filed in Taxonomy
<?php
$products_category_object = get_queried_object();
$product_category_taxonomy = $products_category_object->taxonomy;
$product_category_term_id = $products_category_object->term_id;
$category_acf_image = get_field('your ACF Image Field name', $product_category_taxonomy.'_'.$product_category_term_id);
echo "<pre>";
print_r($category_acf_image);
echo "</pre>";
?>
<img src="<?php echo $category_acf_image['your url key']; ?>" />
There is a plugin Custom Category Image available in wordpress to do that.
This plugin will add option to add image file for each category/taxonomy.
As specified in the plugin description use following code to get the image anywhere in your code -
MGC_Custom_Category_Image::get_category_image($term_id, $size);

Categories