Generate PDF file on product page woocommerce - wordpress

I using wordpress and woocommerce plugin and added custom html button on product page. I did it in function.php file. How can i get PDF file with photo content from every product page after click on my button?
add_action( 'woocommerce_after_single_product_summary', 'custom_button_on_product_page' );
function custom_button_on_product_page() {
global $product;
echo '<button class="send_button">SEND PDF</button>';
}
function enqueue_jspdf() {
wp_enqueue_script( 'jspdf', 'https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js', array(), '2.5.1', true );
}
add_action( 'wp_enqueue_scripts', 'enqueue_jspdf' );
How can i do it? Help please. I tried add jsPDF library in function.php too and paste javascript code in code snippet. But i don't now how to hook on my code to photo content in product page. Great thanks for your answers

Related

Add a shortcode above the WooCommerce product short description

I am trying to add a shortcode to all my woocommerce single product pages. I want the shortcode to appear above the short description.
I have found this code and added it to functions.php, but I can see that it adds the shortcode below the short description (and above the add to cart button).
add_filter('woocommerce_short_description','ts_add_text_short_descr');
function ts_add_text_short_descr($description){
$text = do_shortcode('[my-shortcode]');
return $description.$text;
}
I would be very gratefull for some help :)
function customShortcodeBeforeShortDescription( $post_excerpt )
{
echo do_shortcode('[wpqr-code]');
}
add_filter('woocommerce_short_description','customShortcodeBeforeShortDescription', 10, 1);
You can use the woocommerce_single_product_summary action hook. try the below code. The code will go in your active theme functions.php file.
function add_shortcode_before_short_description(){
echo do_shortcode( '[wpqr-code]' );
}
add_action( 'woocommerce_single_product_summary', 'add_shortcode_before_short_description', 15, 1 );
Tested and works

Custom Theme Single Product Page Wordpress/Woocommerce

I am building my custom theme and my single.php displays woocommerce single product page with
if ( have_posts() ) {
while( have_posts() ) {
the_post();
the_content();
}
}
Why is the page missing - title, reviews & breadcrumbs?
Woocommerce has its own templates for their pages. A single product should be showing on single-product.php. You would create a woocommerce folder inside your custom theme and copy the directories and files you want to edit over to your theme.
How to override single-product.php
If you want to custimize the single-product.php, you would create a woocommerce folder in your theme and copy the single-product.php over into it. So the directory would be yourtheme/woocommerce/templates/single-product.php. This way you have all the actions hooking in the functionality you are missing.
Here are the docs:
https://woocommerce.com/document/template-structure/
function mytheme_add_woocommerce_support() {
add_theme_support( 'woocommerce' );
add_theme_support( 'wc-product-gallery-zoom' );
add_theme_support( 'wc-product-gallery-lightbox' );
add_theme_support( 'wc-product-gallery-slider' );
}
add_action( 'after_setup_theme', 'mytheme_add_woocommerce_support' );
Please add this one in the functions.php file
I was missing
add_theme_support('woocommerce');
in my functions.php file

How to get product data in header.php file wordpress woocommerce

I want to show something of product like: name, price, sku in the header.php file.
Below is my code to get product in header.php
global $product;
var_dump($product);
But var_dump just show me the product name like string(6) "hoodie", not array of product.
So, how can I get the product data in header.php file.
Thank you.
Use the Wordpress get_the_ID() function.
Wordpress - get_the_ID()
Obviously this will only work if you are on the product page, so you can add an extra control with WooCommerce's is_product() function.
WooCommerce - is_product()
Finally, to add custom code in the header you can use the wp_head hook.
Wordpress - wp_head
So the function will look like:
// gets the product data in the header
add_action( 'wp_head', 'get_product_data' );
function get_product_data() {
// only on the product page
if ( ! is_product() ) {
return;
}
// gets the product object
$product = wc_get_product( get_the_ID() );
?>
HTML CODE HERE
<?php
}
Tested and it works. The code goes into your active theme's functions.php file.

woocommerce : Unable to override single-product template

I am trying to override woo commerce single product template but it won't get overridden.
I have done the following steps:
Added file inside my theme directory.
Added support for Woo-commerce in my functions.php file
add_action( 'after_setup_theme', 'wpse319485_add_woocommerce_support' );
function wpse319485_add_woocommerce_support() {
add_theme_support( 'woocommerce' );
}
Made sure the debug mode is off in config file
define( 'WC_TEMPLATE_DEBUG_MODE', false );
Checked the status of templates over-riden
Added an h1 tag with test text in it.
I even tried deleting everything but the default template is loaded.
What am I missing ?
I'm not sure if you are still facing this problem. I ran into the very same. After a very frustrating day, tempted to hack the plugin files, I ended up adding woocommerce.php to my theme, containing:
if ( is_product() ) {
wc_get_template( 'woocommerce/single-product.php' );
}else{
//For ANY product archive.
//Product taxonomy, product search or /shop landing
wc_get_template( 'woocommerce/archive-product.php' );
}
This is an adaptation of what I found here:
https://wordpress.org/support/topic/archive-productphp-template-overwrite-not-working/
It did not work without adding
woocommerce/

Do we have to register all js files in functions.php in wordpress site?

I actually am a newbie to wordpress, i am converting an html website to wordpress template. There are alot of js file in my js directory but all of them are not loaded on every page. What I want to know is that do we have to register all js files in functions.php and use wp_head in our head tag to load them?
For theme you need to put below code in your functions.php
function wpb_adding_scripts() {
wp_register_script('my_amazing_script', get_template_directory_uri() . '/js/amazing_script.js', array('jquery'),'1.1', true);
wp_enqueue_script('my_amazing_script');
}
add_action( 'wp_enqueue_scripts', 'wpb_adding_scripts' )
If you are creating your custom plugin below code will help you.
function wpb_adding_scripts() {
wp_register_script('my_amazing_script', plugins_url('amazing_script.js', __FILE__), array('jquery'),'1.1', true);
wp_enqueue_script('my_amazing_script');
}
add_action( 'wp_enqueue_scripts', 'wpb_adding_scripts' );
For more understanding of wp_enqueue_script read below two links :
https://developer.wordpress.org/reference/functions/wp_enqueue_script/
http://www.wpbeginner.com/wp-tutorials/how-to-properly-add-javascripts-and-styles-in-wordpress/
As you are beginner i can say wpbeginner will be good site for you they give the all understanding very clearly

Resources