want to add script tag codings in wocommerce add_action

add_action( 'woocommerce_single_product_summary', 'whatsappdirect1', 35 );

function whatsappdirect1() {
    echo '[anchor-tag a class="btn btn-social-icon btn-whatsapp" href="javascript:window.location=waCurrentPage();">[img tag border="0" alt="W3Schools" src="http://sareechoice.com/wp-content/uploads/2017/07/whatsappicon.png" width="290" height="200"][anchor-tag-closed]';

waCurrentPage = function() {
  return encodeURI("whatsapp://send?phone=+9199999999&text=please let me know the price of the product in the link : " + 'http://' + window.location.hostname + window.location.pathname);


i have made the following codes that allows mobile users to contact my support team on my website on my whatsapp along with the link of the page ..so now in the above add_action code i have put the codes that will display my button which has to be clicked on the woocommerce product page …but now the problem is that the script codes which does actual work of opening my whatsapp number with predefault link that allow users to send it to my number …so how to insert that script code and also where to insert that ..is there anyway where i can keep both codes at one add_action function of wooocommerce …

thanks in advance

Source: stackoverflow-javascript

How to add a custom button on an item that does not have Stock?

I’m working with WordPress and WooCommerce, I need to add a contact button if in my store some product runs out of stock, ie:

If Stock is 0, show me a contact button.

The following image looks more specific where I want to insert it:

Show image

I have found how to add the button, but it is added to all the products, and I just want the button to appear when I have no stock on any of my products.
Here is the code used:

add_action('woocommerce_after_add_to_cart_button', 'boton_subasta');
function boton_subasta(){
     global $product;
    if ( ! $product->is_in_stock(){

        echo '<button type="submit" class="button alt" onclick="window.location.href=http://">contacto</button>';    

Source: stackoverflow-php

Woocommerce: I want to make the shopping cart drop down after add to cart

I have been trying to do something rather simple: getting a cart to dropdown after a user added something to their cart and stay there, until they click it away.

I’ve tried adding a php function through functions.php in my childtheme, adding custom javascript code, but it doesn’t react. I can’t seem to capture the event with javascript/jquery.
Woocommerce reloads the page after adding something to the cart, since it’s a variation product.

This is what the html code for the cart looks like:

  • </li> <li> </li>
    TOTAL: 199,75
    </div> </ul> </div>

    And this is what I have been trying to do with jquery:

    window.jQuery( document ).ready( function ( $ ) {
        $( 'body' ).on( 'added_to_cart', function ( event, fragments, cart_hash, $button ) {
            $('body').on('click', function(){
        } );
    } );

    Source: stackoverflow-javascript

How to display woocommerce sale price or regular price if there is no sale price

I’m using a woocommerce product addons plugin where I would like to display the price of a product in my dropdown section of the addons.
Currently the code I have is this

$loop = 0;
$current_value = isset( $_POST['addon-' . sanitize_title( $addon['field-name'] ) ] ) ? wc_clean( $_POST[ 'addon-' . sanitize_title( $addon['field-name'] ) ] ) : '';
global $product;
<p class="form-row form-row-wide addon-wrap-<?php echo sanitize_title( $addon['field-name'] ); ?>">
    <select class="addon addon-select" name="addon-<?php echo sanitize_title( $addon['field-name'] ); ?>">

        <?php if ( ! isset( $addon['required'] ) ) : ?>
            <option value=""><?php _e('None', 'woocommerce-product-addons'); ?></option>
        <?php else : ?>
            <!--<option value=""><?php _e('Select an option...', 'woocommerce-product-addons'); ?></option>-->
        <?php endif; ?>

        <?php foreach ( $addon['options'] as $i => $option ) :
            $loop ++;
            $price = apply_filters( 'woocommerce_product_addons_option_price',
                $option['price'] > 0 ? ' + ' . wc_price( get_product_addon_price_for_display( $option['price'] ) ) . '' : '',
            <option data-raw-price="<?php echo esc_attr( $option['price'] ); ?>" data-price="<?php echo get_product_addon_price_for_display( $option['price'] ); ?>" value="<?php echo sanitize_title( $option['label'] ) . '-' . $loop; ?>" <?php selected( $current_value, sanitize_title( $option['label'] ) . '-' . $loop ); ?>><?php echo wptexturize( $option['label'] . ' (' ); echo balanceTags($product->get_price_html()) . $price ?>)</option>
        <?php endforeach; ?>


I’m using this echo


what this does though is display the $”sale price” $”price” but I just want to display just the sale price or just the product price if there is no sale price. Looking at the code below, how would I accomplish this?


Source: stackoverflow-php

WooCommerce – Can’t attach more than 3 pdf files to email

I would like to attach a few pdfs to my custom welcome email. Each pdf has the exact same size of about 2Mb. The email is sent if I don’t attach more than 3 pdfs.

 function attach_coupons_pdf_to_email($attachments, $status, $order)
    the_log("n " . $status .  " n");
    if (isset($status) && strcmp($status, "wc_welcome") == 0)
        array_push($attachments, trailingslashit(ABSPATH) . "coupons/gutschein-1488685005124.pdf");
        array_push($attachments, trailingslashit(ABSPATH) . "coupons/gutschein-1488704520421.pdf");
        array_push($attachments, trailingslashit(ABSPATH) . "coupons/gutschein-1488704521893.pdf");
      //  array_push($attachments, trailingslashit(ABSPATH) . "coupons/gutschein-1488704520454.pdf"); Doesn't send anymore
    return $attachments;

add_filter('woocommerce_email_attachments', 'attach_coupons_pdf_to_email', 2, 3);

I can’t believe that I can’t attach more than about 6Mb to an email. The maximum upload size of my webhoster is 200Mb and the memory_limit is 256Mb.

Do you know of any other restrictions concerning file attachments?

Source: stackoverflow-php

Manipulate woocommerce_order_get_items name

I tried to figure out how to remove the () if the orderline’s name has those parentes.

Here is the code:

$order = wc_get_order($order_id)
foreach ( $order->get_items() as $order_item ){
   //...enter code here

But i want to use hooks/filters since i don’t have access to the foreach above. I tried to add to functions.php so when get_items() calls, the filter will prepare the data array.

add_filter( 'woocommerce_order_get_items', 'filter_woocommerce_order_get_items', 10, 2 ); 
function filter_woocommerce_order_get_items($items, $instance){
    foreach ($items as $item){
        $item['name'] = strtr($item['name'], "", "()");

    return $items;

So, TL;DR:
Can i prepare data when $order->get_items() calls?

Source: stackoverflow-php

Extract URL from order note and add it only to order completed email in Woocommerce

I’ve searched around here and some posts helped me almost nailing this problem:

My parcel company adds an order note to the order including text and an tracking url for the package. And after that they put the order on completed.

This URL needs to be added to the order completed mail to the customer.

This code is working but only if I send the order completed mail manually:

add_action( 'woocommerce_email_before_order_table', 'woo_add_order_notes_to_email' );
function woo_add_order_notes_to_email() {
    global $woocommerce, $post;
    $args = array(
        'post_id'   => $post->ID,
        'status'    => 'approve',
        'type'      => 'order_note'
    $notes = get_comments( $args );

    if ( $notes ) {
        foreach( $notes as $note ) {
            $notecontent = $note->comment_content;

            if( preg_match('/[a-zA-Z]+://[0-9a-zA-Z;./?:@=_#&%~,+$-]+/', $notecontent, $matches) != 0 ) {

                echo '<p>You can follow your order via this link: <a href = "' . $matches[0] . '">' . $matches[0] . '</a></p>';




I hope you guys can help me out.

Source: stackoverflow-php

Woocommerce caches cart (Combined with W3 Total Cache)

I’ve experienced a weird issue on a site using WordPress + Woocommerce recenty. The site uses W3 Total Cache for page cache and browser cache and although i’ve set the woocommerce_cart_items cookie in the rejected list for page cache, the front page is beeing cached .
As a result i’ve issues with the cart ,empty on front page as is served as static, in other shop pages are regenerated and everything is fine.
To my understanding, pages are supposed to be cached if the cookie woocommerce_cart_items is missing ( so we have a guest customer browsing ), and after a product is added in the cart, woocommerce created a session and the cookie is stored so next time the frontpage is called it’s beeing regenerated and not served as static, is that right?
Is there a way to ensure this functionality?

Is this affected depending on the caching option selected (disk, apcu …)? if i decide to use redis for page cache , which is the best way to exclude such things from beeing cached?

Source: stackoverflow-php