Fetching non formatted Product values in WooCommerce - php

I have below formatting for microdata and adding this on content-single-product.php WooCommerce template. Below is the code I am trying to add:
<div itemscope itemtype="http://schema.org/Product">
<meta itemprop="name" content="Product Name">
<div itemscope itemtype="http://schema.org/Offer">
<meta itemprop="sku" content="Product SKU">
<meta itemprop="price" content="Product Price" />
<meta itemprop="priceCurrency" content="Price Currency ex. INR" />
<meta itemprop="category" content="Category Name">
<meta itemprop="availability" content="http://schema.org/InStock" />
</div>
<div itemscope itemtype="http://schema.org/PriceSpecification">
<meta itemprop="minPrice" content="Special Price">
<meta itemprop="validFrom" content="Sale Start Date">
<meta itemprop="validThrough" content="Sale End Date">
</div>
<div itemscope itemtype="http://schema.org/Thing">
<meta itemprop="image" content="Product Image URL">
<meta itemprop="description" content="Product Description">
</div>
</div>
In this content-single-product.php template, I am adding some code below this line:
<div itemscope itemtype="<?php echo woocommerce_get_product_schema(); ?>" id="product-<?php the_ID(); ?>" <?php post_class(); ?>>
I am using WooCommerce functions to fetch product details, but it returns HTML formatted values and I need non-formatted product values.
Here are the TAGs details:
1. Tags for Product Basic Details
Product Name:
<meta itemprop="name" content="Product Name">
Assign product name under CONTENT . This specifies the product title and will be shown to users during search.
Product SKU Code
<meta itemprop="sku" content="Product SKU">
Assign unique product code for your product.
Product URL:
<meta itemprop="url" content="Product URL">
This tag contain product page URL from your website. This may be like http://www.YourWebsiteName.com/ProductName.php
Product Category
<meta itemprop="category" content="Category Name">
Assign Category Name under which your product is listed on your website.
Product Picture
<meta itemprop="image" content="Product Image URL">
This tags contains product image url. You need to put your product image full url here.
For example: http://www.YourWebsite.com/images/product_image.png
Product Description
<meta itemprop="description" content="Product Description">
Include product detail here.
2. Tags for Price
Price Currency:
<meta itemprop="priceCurrency" content="INR" />
Mention your product currency here.
Product Sale Price:
<meta itemprop="price" content="Product Price" />
This tags will contain product sale price/mrp. This is actual price above which product is not available in market. The price should be a number without separators between thousands or spaces (e.g. '8.99').
Product Special Price:
<meta itemprop="minPrice" content="Special Price">
Provide discounted price, offer price or special price for the products. This is not mandatory but important to get top listing in search results. If left blank, system will assign value 0 automatically. The price should be a number without separators between thousands or spaces (e.g. '8.99').
Special Price Start Date:
<meta itemprop="validFrom" content="Sale Start Date">
If Discounted price is offered, mention special price start date in DD/MM/YYYY format. If blank, system will assign current date automatically.
Special Price End Date:
<meta itemprop="validThrough" content="Sale End Date">
If Discounted price is offered, mention special price end date in DD/MM/YYYY format. If blank, system will assign date after 30 days automatically.
Thanks.

You need to get the current Product object and for this you will use together get_the_id() WordPress function with wc_get_product() WooCommerce function this way:
$product = wc_get_product(get_the_ID()); // Getting the Product object from current post ID
With that $product object you can access to any raw data for the current product.
You can use all WC_Product class properties and magic properties using the arrow syntax:
echo $product->property_slug; // displays the product property (if is a string value)
(Here property_slug should be replaced by one of the properties listed in WC_Product class).
$product_id = $product->id;
$product_type = $product->product_type;
$product_post_object = $product->post; // see below
With the post property you can access to all post object properties (just as in wp_post table):
$product_slug = $product->post->post_name;
$product_title = $product->post->post_title;
$product_description = $product->post->post_content;
$product_short_description = $product->post->post_excerpt;
Then using WC_Product class methods, you will get the following:
$product_sku = $product->get_sku(); // or $product->sku
$product_url = $product->get_permalink();
// As a product can be in many categories (array)
$product_categories_array = get_the_terms( $product->id, 'product_cat' );
// The categories in a coma separated string
$product_categories_string = $product->get_categories();
To get the currency you will use dedicated function get_woocommerce_currency()
For the prices you will use related WC_product properties:
$product_price = $product->price;
$product_reg_price = $product->regular_price;
// Product Sale price
$product_sale_price = $product->sale_price;
// Scheduled Sale price dates (timestamps)
$product_sale_start_ts = get_post_meta($product->id, '_sale_price_dates_from', true);
$product_sale_end_ts = get_post_meta($product->id, '_sale_price_dates_to', true);
To format Product Scheduled Sale price dates in DD/MM/YYYY format, as they are stored in database as timestamps values, we will use PHP date() function this way:
$product_sale_start_ts = get_post_meta($product->id, '_sale_price_dates_from', true);
$product_sale_start_date = date('d/m/Y', $product_sale_start_ts);
$product_sale_end_ts = get_post_meta($product->id, '_sale_price_dates_to', true);
$product_sale_end_date = date('d/m/Y', $product_sale_end_ts);
You can also access to this product meta data using get_post_meta() WordPress function, for example to get the SKU you will use:
$product_sku = get_post_meta($product->id, '_sku', true);
For product image The dedicated function is get_the_post_thumbnail() function:
$product_image = get_the_post_thumbnail( $product_id, 'shop_single' );
But as a product can have or not an image, or even a gallery of images you should look at the code in woocommerce templates > single-product > product-image.php template…

Related

Add order customer note to YITH Woocommerce PDF Invoice

In Woocommerce I am using a plugin called YITH WooCommerce PDF Invoice and Shipping List and I would like to add customer note to the PDF invoice.
I would like to add it after the first span line in the code below:
<span class="notes-title"><?php _e( "Notes", "yith-woocommerce-pdf-invoice" ); ?></span>
<div class="notes">
<span><?php echo nl2br( $notes ); ?></span>
<?php do_action( 'yith_ywpi_after_document_notes', $document );?>
</div>
</div>
<?php
But i can't figure out how to get the customer note from $document variable.
I have tried to use this answer thread: "Display Customer order comments (customer note) in Woocommerce" which looks pretty much like the same problem but still could'nt figure it out as $document->order->customer_message; doesn't work.
Any help is appreciated.
Since Woocommerce 3 you can't access anymore properties From the WC_Order object. You need to use the WC_Order method [get_customer_note()][1].
So from the $document (YITH global object) you will use:
$document->order->get_customer_note();
To add customer notes to YITH invoice you can choose between 2 ways:
1) Using the available yith_ywpi_after_document_notes action hook:
add_action( 'yith_ywpi_invoice_template_products_list', 'add_customer_notes_after_document_notes', 5 );
function add_customer_notes_after_document_notes( $document ) {
?><span><?php echo $document->order->get_customer_note(); ?></span><?php
}
Code goes in function.php file of your active child theme (or active theme). Untested (as I dont have the premium version of the plugin) but it should work normally (depending on the plugin settings).
2) Overriding templates (in your provided code):
<span class="notes-title"><?php _e( "Notes", "yith-woocommerce-pdf-invoice" ); ?></span>
<div class="notes">
<span><?php echo nl2br( $notes ); ?></span>
<span><?php echo $document->order->get_customer_note(); ?></span>
<?php do_action( 'yith_ywpi_after_document_notes', $document );?>
</div>
</div>
<?php
It should work.
For the free plugin version
There is no available hooks (like in the provided code)…
The YITH PDF global object need to be called and it's not $document.
So you will be able to use the following code in templates/invoice/invoice-footer.php template:
<?php global $ywpi_document; echo $ywpi_document->order->get_customer_note(); ?>

Add special price and discount amount to the catergory page

hello I am trying to display the special price and discount percentage to the category page products. I have managed to do it on the actual product view but it still displays just the base price on the category page so without clicking into the product the user would not be aware a different price exists. I have added the following code to this file:
app/design/frontend/mytheme/default/template/catalog/price
above this
<?php // Display Discount percents start ?>
<?php if($_finalPrice < $_price): ?>
<?php $_savingPercent = 100 - round(($_finalPrice / $_price)*100); ?>
<p class="special-price yoursaving">
<span class="label"><?php echo $this->__('Your Saving:') ?></span>
<span class="price">
<?php echo $_savingPercent; ?>%
</span>
</p>
<?php endif; ?>
It works nicely for the product page view but i just need to figure how to display on the category pages with the special price also shown.
Any help appreciated thanks!

wordpress head individual php meta description

I've got a wordpress website and tried to insert the following code in my head element to display an individual meta description for a custom post type taxonomy called "all-cars" but in won't work:
<?php if( is_tax('all-cars') ) { ?>
<meta name="description" content="text text text" />
<?php } ?>
Could somebody give me some advice?

Variation Product Shows Price Twice | Wordpress WooCommerce

In wordpress WooCommerce, when i add a Variation Product, variation is set only by Two colors,
Starwhite = 9000
Ivory = 15000
On the Single Product page, I get to see price mentioned twice, as shown in screenshots.
I want to retain the price defined in variation. Remove the other one.
The price which is not required is showing up under the div below
<div itemprop="offers" itemscope="" itemtype="http://schema.org/Offer">
<p class="price"><span class="amount">Rs.9,000.00</span>–<span class="amount">Rs.15,000.00</span></p>
<meta itemprop="price" content="9000" style="
/* display: none; */
">
<meta itemprop="priceCurrency" content="INR">
<link itemprop="availability" href="http://schema.org/InStock">
</div>
But when i mark it has display none or visibility hidden, the price from variation is also gone..
1st Color Variation settings:
2nd Color Variation settings:
Also, there are times when i use Simple Product on the website, so any changes suggested should not impact simple product settings.
If i have to make any changes in the code, what should be the code and under which php files should it be changes.
I believe the file you need to edit is "/single-product/price.php", you should have a copy of that file in your theme folder (/your-theme/woocommerce/single-product/price.php), if not you can copy it there from /wp-content/plugins/woocommerce/templates/
Change:
<p class="price"><?php echo $product->get_price_html(); ?></p>
To:
<?php if( $product->is_type( 'simple' ) ){ ?><p class="price"><?php echo $product->get_price_html(); ?></p><?php } ?>
That will make it only display the price for Simple Products. The other price display that changes when you select a different variation is set in /single-product/add-to-cart/variable.php so that will be unaffected.

Magento custom Upsell image not showing

I have a problem where I can't get my custom attribute called upsell to get shown as my upsell image.
Attribute information
Attribute code: upsell
Scope: Store View
Catalog Input type for store owner: Media Image
Apply To: All product Types
I have created the thumbnail in the skin/frontend/.../images/catalog/product/placeholder/
Here's the code to show the image
<img src="<?php echo $this->helper('catalog/image')->init($_item, 'upsell')->constrainOnly(FALSE)->keepAspectRatio(TRUE)->keepFrame(FALSE)->resize(150); ?>" alt="<?php echo $this->htmlEscape($_item->getName()) ?>" />
If I change the ($_item, 'upsell') to ($_item, 'thumbnail') it's showing the right thumbnail.
Did I miss something in the process of creating the attribute?
ayTo call an image you can use:
$this->helper('catalog/image')->init($_product, 'product_details_image')->resize()
and the full script is:
<?php
$_detailsImg = $this->helper('catalog/image')->init($_product,'product_details_image')->resize();
if ($_detailsImg != 'no_selection'):
$_detailsImg ='<img src="'.$this->helper('catalog/image')->init($_product, 'product_details_image')->resize().'" alt="'.$this->htmlEscape($this->getImageLabel()).' "/>';
echo $_helper->productAttribute($_product, $_detailsImg , 'product_details_image');
endif;
?>
Please change product_details_image to your attribute image code.
In case you don’t have image assigned to the product you will get this error : Image file was not found. in (var/ reports)
so to fix this create an image with same name as the attribute code (product_details_image.jpg) to act as placeholder and add it to:
skin/frontend/base/default/images/catalog/product/placeholder/
Also you can add it to your skin folder. that is it.
EDIT:
I would rewrite the upsell.phtml again, get all ids add it to an array call product collections, filter by ids and you are ready to call any attribute including the media image - and I can confirm this works for me
EDIT:
This is may sound stupid but I can't find any other way, I had to load the collection for each product I hope some one can find other way.
I did try to call one collection and filter it using products IDs ( up sell products IDs ) but it didn't work.
<?php if(count($this->getItemCollection()->getItems())):
$items = $this->getItemCollection()->getItems();
?>
<div class="box-up-sell">
<ul class="products-grid up-sell-grid">
<?php foreach ($items as $item): ?>
<li class="item">
<?php $product = Mage::getModel('catalog/product')->load($item->getId()); ?>
<span><?php echo $product->getName(); ?></span>
<img src="<?php echo Mage::helper('catalog/image')->init($product, 'measurements_guide_image')->resize(200, 200); ?>" alt="">
</li>
<?php endforeach ?>
</ul>
</div>
<?php endif ?>
You need to add this media attribute to product flat setting:
<frontend>
<product>
<collection>
<attributes>
<upsell/>
</attributes>
</collection>
</product>
</frontend>
At this code at my module config.xml and then you need to reindex from index management.

Categories