Get sku and product image in woocommerce emails - image

I made a copy of the file "email-order-details.php" to my child theme. Now I can modify the html, but would like to add an extra column to my table for the product image. With this code I am able to display the image and sku, but it's in the same column with product name, sku, product description.
<?php
echo wc_get_email_order_items( $order, array( // WPCS: XSS ok.
'show_sku' => $true,
'show_image' => false,
'image_size' => array( 32, 32 ),
'plain_text' => $plain_text,
'sent_to_admin' => $sent_to_admin,
) );
?>
I also want to display the sku without the () and I want to place it on top of the column. I think there must be an array or variables or something like this to get the dates.
This line, for example displays the Price, can anybody tell me how to display sku and product image in the same way?
<th class="td" scope="col" style="text-align:<?php echo esc_attr( $text_align ); ?>;"><?php esc_html_e( 'Price', 'woocommerce' ); ?></th>
Thank you in advance

try this snippet code, I use this in functions.php in child theme:
function sww_add_wc_order_email_images( $table, $order ) {
ob_start();
$template = $plain_text ? 'emails/plain/email-order-items.php' : 'emails/email-order-items.php';
wc_get_template( $template, array(
'order' => $order,
'items' => $order->get_items(),
'show_download_links' => $show_download_links,
'show_sku' => $show_sku,
'show_purchase_note' => true,
'show_image' => true,
'image_size' => array( 200, 200 )
) );
return ob_get_clean();
}
add_filter( 'woocommerce_email_order_items_table', 'sww_add_wc_order_email_images', 10, 2 );

Related

Woocommerce - store all products of a specific attribute in an array

With this code I can get the value of the current product attribute:
$terms = get_the_terms( $product->id, 'pa_myAttribute');
foreach ( $terms as $term ) {
echo $term->name;
}
I don't know how to get all products with this attribute and store their ids in an array.
Knowing that an attribute is merely a custom taxonomy you can use WP_Query's taxonomy parameters to run a quick get_posts() to return an array of posts that have a particular term in the desired attribute taxonomy. Finally you can use wp_list_pluck() to quickly convert that to an array of only the post IDs.
$args = array( 'post_type' => 'product',
'tax_query' => array(
array(
'taxonomy' => 'pa_color',
'field' => 'slug',
'terms' => 'blue',
)
)
);
$posts = get_posts( $args );
$post_ids = wp_list_pluck( $posts, 'ID' );
In the above example, the get_posts() will return all products that have been assigned the blue term in the color attribute.

Woocommerce Filter Product by Attribute

I've been searching for many blogs and forum but this seems not yet answered yet. So i am trying to find a way how to filter product by attribute. I am using ajax to pull data and append it.The question is what will be the query to make a loop depend on what attribute.
Example.
Product A has Color:Blue,Red,Green and has a brand : BrandA , BrandB Product B has Color:Pink,Red,black.
All i want is to get all product with an attribute of Color [red and green] and Brand [BrandA] in a single query without using any plugin.
Here my code in my functions.php
function advanced_search(){
$html = "";
$args = array( 'post_type' => 'product','product_cat' => 'yarn');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ){
$loop->the_post();
ob_start();
get_template_part( 'templates/part', 'search-result' );
$html = ob_get_contents();
ob_end_clean();
}
wp_send_json(array( "product_id" => $product_id , "html" => $html ));
}
add_action('wp_ajax_get_advanced_search', 'advanced_search');
add_action('wp_ajax_nopriv_get_advanced_search', 'advanced_search');
I dont know where should i put the product attributes in my query. I hope anyone found an answer for this.Thank you very much.
You should be able to achieve what you're looking to do by using a tax_query combined with your WP_Query. Attached is a small example using a 'Brand' attribute and the 'EVGA' term inside it (which WooCommerce will turn into 'pa_brand' - product attribute brand).
$query = new WP_Query( array(
'tax_query' => array(
'relation'=>'AND',
array(
'taxonomy' => 'pa_brand',
'field' => 'slug',
'terms' => 'evga'
)
)
) );
You can find some more documentation on the above links to string a few tax queries together for additional filtering and functionality.

I want to add products in cart with custom labels on cart page

I have tried several methods to add products to cart programatically but i want to show the quote item labels as well one cart page. Can anyone help me to do this or any references?
You can use the below code in your observer.
$item = ( $item->getParentItem() ? $item->getParentItem() : $item);
$additionalOptions = array(array(
'code' => 'option_code',
'label' => 'Some_option_Label',
'value' => 'Option_Value'
));
$item->addOption(array(
'code' => 'additional_options',
'value' => serialize($additionalOptions),
));
// Enable super mode on the product.
$item->getProduct()->setIsSuperMode(true);
here is the code you are looking for;)
<?php
$loadProductData = Mage::getModel('catalog/product')->load($productId);
$quote = Mage::getSingleton('checkout/session')->getQuote();
$OrderquoteItem = Mage::getModel('sales/quote_item');
$quoteItem = $OrderquoteItem->setProduct($loadProductData);
//custom options to show user on cart page
$a_options = array(
'options' => array(
'label' => 'OptionLabel :',
'value' => "OptionaValue",
));
//add above options array to this cart item which is going to get added on cart
$quoteItem->addOption(array(
'code' => 'additional_options',
'value' => serialize($a_options),
));
// set price and quantity
$quoteItem->setQuote($quote)
->setQty($productOptions['qty'])
->setOriginalCustomPrice($productOptions['price'])
->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);

WooCommerce Custom Loop to get all product from one specific category

I try to find the code (short code) in the woo comm plugin that made the list of all the product from one category, to be able to modify it... no luck after 1 hours, still no find.
So i start coding it myself (reinventing the wheel) and here what i try to get
get me all the product from category ID="151" and be able to output the name, the permalink etc etc...
this is the code now, that return everything.... way too much ! and i don't know how to filter it
{
$args = array(
'post_type' => 'product',
'posts_per_page' => 99
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
//echo get_title()."<br/>";
var_dump($loop);
endwhile;
}
here is the code i have found, and modify to my needs
function get_me_list_of($atts, $content = null)
{
$args = array( 'post_type' => 'product', 'posts_per_page' => 10, 'product_cat' => $atts[0], 'orderby' => 'rand' );
$loop = new WP_Query( $args );
echo '<h1 class="upp">Style '.$atts[0].'</h1>';
echo "<ul class='mylisting'>";
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
echo '<li>'.get_the_post_thumbnail($loop->post->ID, 'thumbnail').'</li>';
endwhile;
echo "</ul>";
wp_reset_query();
}
?>
The [product_category] shortcode defined in /woocommerce/includes/class-wc-shortcodes.php is a great starting place for this, particularly as Woocommerce is constantly evolving!
The guts of it is just a standard WP_Query with some extra code added on to do pagination, setting sort orders from the Woocommerce settings and some checking to see if the products are marked as visible or not.
So if you strip out the shortcode related code and wanted a function that just got visible products from a category with a specific slug, it would look like this:
function getCategoryProducts($category_slug) {
// Default Woocommerce ordering args
$ordering_args = WC()->query->get_catalog_ordering_args();
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'orderby' => $ordering_args['orderby'],
'order' => $ordering_args['order'],
'posts_per_page' => '12',
'meta_query' => array(
array(
'key' => '_visibility',
'value' => array('catalog', 'visible'),
'compare' => 'IN'
)
),
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'terms' => array( esc_attr( $category_slug ) ),
'field' => 'slug',
'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
)
)
);
if ( isset( $ordering_args['meta_key'] ) ) {
$args['meta_key'] = $ordering_args['meta_key'];
}
$products = new WP_Query($args);
woocommerce_reset_loop();
wp_reset_postdata();
return $products;
}
Pass in the slug and you'll get back a standard wordpress posts collection using the same ordering that you've configured in your Woocommerce settings.
I had similar problem as I wanted to get "Fabrics" product for a "Custom Page", here is the code I used.
<ul class="products">
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => 12,
'product_cat' => 'fabrics'
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
woocommerce_get_template_part( 'content', 'product' );
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
</ul><!--/.products-->
Using the above code means you get the default inline styles, classes and other necessary tags.

Wordpress - changing attributes of featured images

I want to use Featured Images (thumbnails) in my posts.
The thing is, I want to be able to change the following image attributes after the image has been attached to the post, but before the post has been published:
Title
Alternate Text
Caption
Description
How do you do that?
<?php
$size = array(150,150);
$default_attr = array(
'src' => $src,
'class' => "attachment-$size",
'alt' => trim(strip_tags( wp_postmeta->_wp_attachment_image_alt )),
'title' => trim(strip_tags( $attachment->post_title )),
);
the_post_thumbnail( $size, $attr );
?>
I'm still not clear what you're trying to do.
This will display the featured image in your markup using the post title as alt and title attributes.
$image_meta = wp_get_attachment_image_src( get_post_thumbnail_id(), 'medium');
// replace 'medium' with 'thumbnail', 'large', or 'full'.
echo '<img src="'.$image_meta[0].'" alt="'.$post->post_title.'" title="'.$post->post_title.'" width="'.$image_meta[1].'" height="'.$image_meta[2].'"/>';
If you want to actually change the featured image title, alt, caption, description etc in the database, then you could look at the post_publish hook. This should get you started:
function do_stuff($post_ID){
global $post;
$post_thumbnail_id = get_post_thumbnail_id($post_ID);
if ($post_thumbnail_id){
// Do Stuff with your featured image id - $post_thumbnail_id
}
return $post_ID;
}
add_action('publish_post', 'do_stuff');
try this and its work fine.
$title_attribute = the_title_attribute( array( 'echo' => FALSE ) );
the_post_thumbnail(
'full',
array(
'alt' => $title_attribute,
'title' => $title_attribute
)
);

Resources