Woocommerce get stock function in php - php

I need to add echo $product->get_stock_quantity() in following code to display it in pdf. Can you please help me to insert this $product->get_stock_quantity() into code bellow:
foreach ($order->get_items() as $item_id => $item) {
$product = wc_get_product($item->get_product_id());
$with_tax = $product->get_price_including_tax();
$without_tax = $product->get_price_excluding_tax();
$tax_amount = $with_tax - $without_tax;
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $item->get_product_id() ),'thumbnail' );
$css = ($item->get_quantity() > 1)?'color:red;':'';
?>
<tr><td><?php echo $product->get_sku();?></td><td><img width='60' src="<?php echo $image[0]; ?>"/></td><td style="text-align: center;font-family:DejaVu Sans, sans-serif;font-size:12px;"><?php echo $item->get_name();?></td><td style="text-align: center;"><img width='60' src="https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=<?php echo $product->get_sku();?>"/></td><td style="text-align: center; <?php echo $css;?>"><?php echo $item->get_quantity();?></td><td><?php echo wc_price( $with_tax );?> </td><td style="text-align: center;font-family:DejaVu Sans, sans-serif;">[]</td></tr>
Not succed to make myself.

Related

Customize WooCommerce attribute table to two columns

I'm looking for some help getting customize the attribute table on WooCommerce.
<table>
<?php foreach (array_chunk($product_attributes, 2) as $product_attribute_key => $product_attribute) :{ ?>
<tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--<?php echo esc_attr( $product_attribute_key ); ?>">
<?php foreach ($product_attribute as $value) :{ ?>
<th class="woocommerce-product-attributes-item__label"><?php echo wp_kses_post( $value['label'] ); ?></th>
<td class="woocommerce-product-attributes-item__value"><?php echo wp_kses_post( $value['value'] ); ?></td>
<?php } endforeach; ?>
</tr>
<?php } endforeach; ?>
</table>
I tried modify the above snippet in order to break line between Attribute Name and Attribute Value to display the attributes like it is in the below picture.
attributes
Is there any easy hook to get this work?
Thanks in Advance.
Create child-theme in case of theme update to not lose your settings.
Create in child-theme folders /woocommerce/single-product/ and copy from woocommerce/templates/single-product the file product-attributes.php.
Change:
defined( 'ABSPATH' ) || exit;
if ( ! $product_attributes ) {
return;
}
?>
<table class="woocommerce-product-attributes shop_attributes">
<?php foreach ( $product_attributes as $product_attribute_key => $product_attribute ) : ?>
<tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--<?php echo esc_attr( $product_attribute_key ); ?>">
<th class="woocommerce-product-attributes-item__label"><?php echo wp_kses_post( $product_attribute['label'] ); ?></th>
<td class="woocommerce-product-attributes-item__value"><?php echo wp_kses_post( $product_attribute['value'] ); ?></td>
</tr>
<?php endforeach; ?>
</table>
to
defined( 'ABSPATH' ) || exit;
if ( ! $product_attributes ) {
return;
}
$i = 0;
?>
<div class="woocommerce-product-attributes shop_attributes">
<?php foreach ( $product_attributes as $product_attribute_key => $product_attribute ) : ?>
<div class="woocommerce-product-attributes-item woocommerce-product-attributes-item--<?php echo esc_attr( $product_attribute_key ); ?>">
<div class="woocommerce-product-attributes-item__label"><?php echo wp_kses_post( $product_attribute['label'] ); ?></div>
<div class="woocommerce-product-attributes-item__value"><?php echo wp_kses_post( $product_attribute['value'] ); ?></div>
</div>
<?php
if ($i % 2 != 0){
echo '<div class="clear"></div>';
}
$i++;
endforeach; ?>
</div>
add CSS
/*2 Columnt style on product attributes*/
.woocommerce-product-attributes.shop_attributes .woocommerce-product-attributes-item {
float: left;
width: 50%;
margin-bottom: 8px;
padding-right:10px;
}

Displaying WooCommerce product images in DK PDF Template

I am trying to adapt this code to echoing out a fore each for the image gallary of the products on woocommerce into a pdf. If you could point me into the correct direction that would be great
<?php
/**
* dkpdf-index.php
* This template is used to display the content in the PDF
*
* Do not edit this template directly,
* copy this template and paste in your theme inside a directory named dkpdf
*/
?>
<html>
<head>
<link type="text/css" rel="stylesheet" href="<?php echo get_bloginfo( 'stylesheet_url' ); ?>" media="all" />
<?php
$wp_head = get_option( 'dkpdf_print_wp_head', '' );
if( $wp_head == 'on' ) {
wp_head();
}
?>
<style type="text/css">
body {
background:#fff;
font-size: 6pt;
}
/* fontawesome compatibility */
.fa {
font-family: fontawesome;
display: inline-block;
font: normal normal normal 14px/1 FontAwesome;
font-size: inherit;
text-rendering: auto;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
transform: translate(0, 0);
}
<?php
// get pdf custom css option
$css = get_option( 'dkpdf_pdf_custom_css', '' );
echo $css;
?>
</style>
</head>
<body>
<?php
global $post;
$pdf = get_query_var( 'pdf' );
$post_type = get_post_type( $pdf );
if( $pdf && $post_type == 'product' ) {
$title = get_the_title();
if( has_post_thumbnail( $post->ID ) ) {
$thumbnail = get_the_post_thumbnail( $post->ID, 'FULL' );
}
$description = get_the_excerpt();
$description2 = get_post($item['product_id'])->post_content;
$product = new WC_Product( $post->ID );
$link = get_the_permalink();
?>
<?php function yikes_custom_product_tabs_shortcode( $args ) {
global $post;
// Define our default values
$defaults = array(
'product_id' => $post->ID,
'tab_number' => 'all',
);
// Let the user-defined values override our defaults
$values = is_array( $args ) ? array_merge( $defaults, $args ) : $defaults;
// Make sure we have a product ID and that the product ID is for a product
if ( empty( $values['product_id'] ) || ! empty( $values['product_id'] ) && get_post_type( $values['product_id'] ) !== 'product' ) {
return;
}
// Fetch our tabs
$tabs = get_post_meta( $values['product_id'], 'yikes_woo_products_tabs', true );
// Get just the specified tab. (minus tab number by one so it starts at 0)
$tabs = absint( $values['tab_number'] ) < 1 ? $tabs : ( isset( $tabs[ absint( $values['tab_number'] ) - 1 ] ) ? array( $tabs[ absint( $values['tab_number'] ) - 1 ] ) : array() );
if ( empty( $tabs ) ) {
return;
}
// Debug statement to show all tab data. Feel free to remove.
// echo '<pre>'; var_dump( $tabs ); echo '</pre>';
$html = '';
// Loop through the tabs and display each one
foreach( $tabs as $tab ) {
$html .= '<tr>
<td style="padding-top:15px; padding-bottom:15px; text-align:center; line-height:2em; background:#f8f8f8;">';
$html .= '<h5>' . $tab['title'] . '</h5>';
$html .= '</td></tr>';
$html .= '<tr>
<td style="padding-top:20px; padding-bottom:20px; text-align:center; line-height:2em;">';
$html .= '<p style="font-size:10px;">' . $tab['content'] . '</p>';
$html .= '</td></tr>';
}
// Make sure to return your content, do not echo it.
return $html;
}
add_shortcode( 'custom_product_tabs', 'yikes_custom_product_tabs_shortcode' ); ?>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td style="padding-top:20px; padding-bottom:20px;text-align:center; color:#fff;" class="bg-dark">
<h3 style="color:#fff;"><?php echo esc_attr( $title );?></h3>
</td>
</tr>
<tr>
<td style="padding-top:20px; padding-bottom:20px; text-align:center; line-height:2em;">
<p style="font-size:10px;"><?php echo $description;?></p>
</td>
</tr>
<tr>
<td style="padding-bottom:15px; text-align:center; line-height:2em; ">
<?php if( $thumbnail ) { echo $thumbnail; } ?>
</td>
</tr>
<tr>
<td style="padding-top:15px; padding-bottom:15px; text-align:center; line-height:2em; background:#f8f8f8;">
<h5>DESCRIPTION</p>
</td>
</tr>
<tr>
<td style="padding-top:20px; padding-bottom:20px; text-align:center; line-height:2em;">
<p style="font-size:10px;"><?php echo $description2;?> <?php echo wpautop( $post->post_content ); ?></p>
</td>
</tr>
<?php echo yikes_custom_product_tabs_shortcode('custom_product_tabs'); ?>
</table>
<?php }
?>
</body>
</html>

Woocommerce customize email checkout

This is my custom e-mail template which gets sent when a order is completed.
<?php
/**
* Customer completed order email
*
* #author WooThemes
* #package WooCommerce/Templates/Emails
* #version 1.6.4
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly ?>
<?php do_action( 'woocommerce_email_header', $email_heading ); ?>
<p><?php printf( __( "Hi there. Your recent order on %s has been completed. Your order details are shown below for your reference:", 'woocommerce' ), get_option( 'blogname' ) ); ?></p>
<?php do_action( 'woocommerce_email_before_order_table', $order, $sent_to_admin, $plain_text ); ?>
<h2><?php echo __( 'Order:', 'woocommerce' ) . ' ' . $order->get_order_number(); ?></h2>
<table cellspacing="0" cellpadding="6" style="width: 100%; border: 1px solid #eee;" border="1" bordercolor="#eee">
<thead>
<tr>
<th scope="col" style="text-align:left; border: 1px solid #eee;"><?php _e( 'Product', 'woocommerce' ); ?></th>
<th scope="col" style="text-align:left; border: 1px solid #eee;"><?php _e( 'Quantity', 'woocommerce' ); ?></th>
<th scope="col" style="text-align:left; border: 1px solid #eee;"><?php _e( 'Price', 'woocommerce' ); ?></th>
</tr>
</thead>
<tbody>
<?php echo $order->email_order_items_table( true, false, true ); ?>
</tbody>
<tfoot>
<?php
if ( $totals = $order->get_order_item_totals() ) {
$i = 0;
foreach ( $totals as $total ) {
$i++;
?><tr>
<th scope="row" colspan="2" style="text-align:left; border: 1px solid #eee; <?php if ( $i == 1 ) echo 'border-top-width: 4px;'; ?>"><?php echo $total['label']; ?></th>
<td style="text-align:left; border: 1px solid #eee; <?php if ( $i == 1 ) echo 'border-top-width: 4px;'; ?>"><?php echo $total['value']; ?></td>
</tr><?php
}
}
?>
</tfoot>
</table>
<?php do_action( 'woocommerce_email_after_order_table', $order, $sent_to_admin, $plain_text ); ?>
<?php do_action( 'woocommerce_email_order_meta', $order, $sent_to_admin, $plain_text ); ?>
<h2><?php _e( 'Customer details', 'woocommerce' ); ?></h2>
<?php if ($order->billing_email) : ?>
<p><strong><?php _e( 'Email:', 'woocommerce' ); ?></strong> <?php echo $order->billing_email; ?></p>
<?php endif; ?>
<?php if ($order->billing_phone) : ?>
<p><strong><?php _e( 'Tel:', 'woocommerce' ); ?></strong> <?php echo $order->billing_phone; ?></p>
<?php endif; ?>
<?php wc_get_template( 'emails/email-addresses.php', array( 'order' => $order ) ); ?>
<?php do_action( 'woocommerce_email_footer' ); ?>
I want to insert PHP code there which connects to a database, does a select query which needs the article ID and how many of the article ID´s are purchased and then sends a new email with this data.
I tried it multiple times but at the end the mail always stopped working due to some PHP error which I couldn't address without debugging opportunity.
As an example I tried:
<?php
$db = mysqli_connect("sqwffwq", "qwfqwf", "qwfqwf", "qwfwqf");
// here I have no clue how to receive the article ID´s and there amounts...
// I wasn't able to find the woocommerce function
$result = mysqli_query('SELECT * FROM etc. etc. ');
mail(results) //Should be no problem after I got the information
?>
run this and see whats going on with each result. What you've failed to mention above is where you are adding this code, are you sure you have $order available?
global $wpdb;
$rere2 = substr( $order->get_order_number(), 4);
echo 'rere2='.$rere2.'<br>';
$myrows = $wpdb->get_results( 'SELECT * FROM wp_woocommerce_order_items WHERE order_id = "'.$rere2.'"' );
var_dump($myrows);
foreach ( $myrows as $myrow) {
//mail("etix#foxfiredev.net",$rere2,$myrow->order_item_id);
$x=wp_mail( "etix#foxfiredev.net", $rere2 ,$myrow->order_item_id);
echo $x.'<br>';
$wpdb->update( 'wp_woocommerce_order_items',
array('id'=>$variable),
array('order_item_id'=>$myrow->order_item_id),
); // change table name, column names and values to suit..
}
exit;

Converting Html to PHP

Can anyone help me converting Html to PHP
I want to add a paypal button to my woocommerce website but I am running into a few errors.
here is the code I have in html format
<a class="paypal_checkout_button" href="websitelinkhere"><img src="https://www.paypal.com/en_US/i/btn/btn_xpressCheckout.gif" width="145" height="42" style="width: 145px; height: 42px; padding-top: 30px; padding-left:20px; " border="0" align="top" alt="Check out with PayPal"></a>
Basically im using the theme - theretailer.getbowtied.com/demo the shopping cart dropdown at the top of the main page im trying to add the button into that but cant get it to display
Any help would be amazing
Edit*
this is the page im trying to add the code to
<div class="gbtr_minicart_wrapper">
<div class="gbtr_minicart">
<?php echo '<a class="paypal_checkout_button" href="websitelinkhere"><img src="https://www.paypal.com/en_US/i/btn/btn_xpressCheckout.gif" width="145" height="42" style="width: 145px; height: 42px; padding-top: 30px; padding-left:20px; " border="0" align="top" alt="Check out with PayPal"></a>';?>
<?php
echo '<ul class="cart_list">';
if (sizeof($woocommerce->cart->cart_contents)>0) : foreach ($woocommerce->cart->cart_contents as $cart_item_key => $cart_item) :
$_product = $cart_item['data'];
if ($_product->exists() && $cart_item['quantity']>0) :
echo '<li class="cart_list_product">';
echo '<a class="cart_list_product_img" href="'.get_permalink($cart_item['product_id']).'">' . $_product->get_image().'</a>';
echo '<div class="cart_list_product_title">';
$gbtr_product_title = $_product->get_title();
//$gbtr_short_product_title = (strlen($gbtr_product_title) > 28) ? substr($gbtr_product_title, 0, 25) . '...' : $gbtr_product_title;
echo '' . apply_filters('woocommerce_cart_widget_product_title', $gbtr_product_title, $_product) . '';
echo '<div class="cart_list_product_quantity">'.__('Quantity:', 'theretailer').' '.$cart_item['quantity'].'</div>';
echo '</div>';
echo apply_filters( 'woocommerce_cart_item_remove_link', sprintf('×', esc_url( $woocommerce->cart->get_remove_url( $cart_item_key ) ), __('Remove this item', 'woocommerce') ), $cart_item_key );
echo '<div class="cart_list_product_price">'.woocommerce_price($_product->get_price()).'</div>';
echo '<div class="clr"></div>';
echo '</li>';
endif;
endforeach;
?>
<div class="minicart_total_checkout">
<?php _e('Cart subtotal', 'theretailer'); ?><span>
<?php echo $woocommerce->cart->get_cart_total(); ?></span>
</div>
<?php _e('View Shopping Bag', 'theretailer'); ?>
<?php _e('Proceed to Checkout', 'theretailer'); ?>
<?php
else: echo '<li class="empty">'.__('No products in the cart.','woocommerce').'</li>'; endif;
echo '</ul>';
?>
</div>
</div>
</div>
<span><?php echo $woocommerce->cart->cart_contents_count; ?></span>
</div>
Im looking for it to go under the buttons
Im looking for it to go into the gbtr_minicart section under the buttons
here you go
//some php code
?>
<a class="paypal_checkout_button" href="websitelinkhere"><img src="https://www.paypal.com/en_US/i/btn/btn_xpressCheckout.gif" width="145" height="42" style="width: 145px; height: 42px; padding-top: 30px; padding-left:20px; " border="0" align="top" alt="Check out with PayPal"></a>
<?php
//php code again
<?php
echo '<a class="paypal_checkout_button" href="websitelinkhere"><img src="https://www.paypal.com/en_US/i/btn/btn_xpressCheckout.gif" width="145" height="42" style="width: 145px; height: 42px; padding-top: 30px; padding-left:20px; " border="0" align="top" alt="Check out with PayPal"></a>';
?>

Wordpress & Woocommerce add excerpt in order email

I have a little problem with Wordpress and Woocommerce I hope someone can help me.
When a person purchases receive an email with your data such as title, quantity, etc. ... I would also add the excerpt of the products near the quatity. The classic <? Php the_excerpt ();?> Does not work, does anyone know how to do?
The file is "email-order-items" in woocommerce\templates\emails
Here is the code:
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => 1, 'orderby' => 'desc' );
$loop = new WP_Query( $args ); ?>
<?php
/**
* Email Order Items
*
* #author WooThemes
* #package WooCommerce/Templates/Emails
* #version 2.0.3
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
global $woocommerce;
foreach ($items as $item) :
// Get/prep product data
$_product = $order->get_product_from_item( $item );
$item_meta = new WC_Order_Item_Meta( $item['item_meta'] );
$image = ($show_image) ? '<img src="'. current(wp_get_attachment_image_src( get_post_thumbnail_id( $_product->id ), 'thumbnail')) .'" alt="Product Image" height="'.$image_size[1].'" width="'.$image_size[0].'" style="vertical-align:middle; margin-right: 10px;" />' : '';
?>
<tr>
<td style="text-align:left; vertical-align:middle; border: 1px solid #eee;"><?php
// Show title/image etc
echo apply_filters( 'woocommerce_order_product_image', $image, $_product, $show_image);
// Product name
echo apply_filters( 'woocommerce_order_product_title', $item['name'], $_product );
// SKU
echo ($show_sku && $_product->get_sku()) ? ' - cod. articolo (#' . $_product->get_sku() . ')' : '';
// File URLs
if ( $show_download_links && $_product->exists() && $_product->is_downloadable() ) {
$download_file_urls = $order->get_downloadable_file_urls( $item['product_id'], $item['variation_id'], $item );
$i = 0;
foreach ( $download_file_urls as $file_url => $download_file_url ) {
echo '<br/><small>';
if ( count( $download_file_urls ) > 1 ) {
echo sprintf( __('Download %d:', 'woocommerce' ), $i + 1 );
} elseif ( $i == 0 )
echo __( 'Download:', 'woocommerce' );
echo ' ' . basename( $file_url ) . '</small>';
$i++;
}
}
// Variation
echo ($item_meta->meta) ? '<br/><small>' . nl2br( $item_meta->display( true, true ) ) . '</small>' : '';
?>
</td>
<td style="text-align:left; vertical-align:middle; border: 1px solid #eee;"><?php echo $item['qty'] ;?> x <?php the_excerpt(); ?> </td>
<td style="text-align:left; vertical-align:middle; border: 1px solid #eee;"><?php echo $order->get_formatted_line_subtotal( $item ); ?></td>
</tr>
<?php if ($show_purchase_note && $purchase_note = get_post_meta( $_product->id, '_purchase_note', true)) : ?>
<tr>
<td colspan="3" style="text-align:left; vertical-align:middle; border: 1px solid #eee;"><?php echo apply_filters('the_content', $purchase_note); ?></td>
</tr>
<?php endif; ?>
<?php endforeach; ?>
thank you very much
To get excerpt in order email try this code.
<?php
/**
* Email Order Items
*
* #author WooThemes
* #package WooCommerce/Templates/Emails
* #version 2.1.2
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
foreach ( $items as $item_id => $item ) :
$_product = apply_filters( 'woocommerce_order_item_product', $order->get_product_from_item( $item ), $item );
/* To get product excerpt */
$product_data = get_post( $item['product_id'] );
$excerpt = strip_tags($product_data->post_excerpt);
echo $excerpt;
$item_meta = new WC_Order_Item_Meta( $item['item_meta'], $_product );
?>
<tr>
<td style="text-align:left; vertical-align:middle; border: 1px solid #eee; word-wrap:break-word;"><?php
// Show title/image etc
if ( $show_image ) {
echo apply_filters( 'woocommerce_order_item_thumbnail', '<img src="' . ( $_product->get_image_id() ? current( wp_get_attachment_image_src( $_product->get_image_id(), 'thumbnail') ) : wc_placeholder_img_src() ) .'" alt="' . __( 'Product Image', 'woocommerce' ) . '" height="' . esc_attr( $image_size[1] ) . '" width="' . esc_attr( $image_size[0] ) . '" style="vertical-align:middle; margin-right: 10px;" />', $item );
}
// Product name
echo apply_filters( 'woocommerce_order_item_name', $item['name'], $item );
// SKU
if ( $show_sku && is_object( $_product ) && $_product->get_sku() ) {
echo ' (#' . $_product->get_sku() . ')';
}
// File URLs
if ( $show_download_links && is_object( $_product ) && $_product->exists() && $_product->is_downloadable() ) {
$download_files = $order->get_item_downloads( $item );
$i = 0;
foreach ( $download_files as $download_id => $file ) {
$i++;
if ( count( $download_files ) > 1 ) {
$prefix = sprintf( __( 'Download %d', 'woocommerce' ), $i );
} elseif ( $i == 1 ) {
$prefix = __( 'Download', 'woocommerce' );
}
echo '<br/><small>' . $prefix . ': ' . esc_html( $file['name'] ) . '</small>';
}
}
// Variation
if ( $item_meta->meta ) {
echo '<br/><small>' . nl2br( $item_meta->display( true, true ) ) . '</small>';
}
?></td>
<td style="text-align:left; vertical-align:middle; border: 1px solid #eee;"><?php echo $item['qty'] ;?></td>
<td style="text-align:left; vertical-align:middle; border: 1px solid #eee;"><?php echo $order->get_formatted_line_subtotal( $item ); ?></td>
</tr>
<?php if ( $show_purchase_note && is_object( $_product ) && $purchase_note = get_post_meta( $_product->id, '_purchase_note', true ) ) : ?>
<tr>
<td colspan="3" style="text-align:left; vertical-align:middle; border: 1px solid #eee;"><?php echo wpautop( do_shortcode( $purchase_note ) ); ?></td>
</tr>
<?php endif; ?>
<?php endforeach; ?>
I've used the following code to get the excerpt from Woocommerce order posts (delivery notes).
$post_id = $post_id_of_the_product;
$page_data = get_page( $post_id );
$excerpt = strip_tags($page_data->post_excerpt);

Categories