EzDevInfo.com

woocommerce interview questions

Top woocommerce frequently asked interview questions

Woocommerce location of checkout template

I'm trying to edit the checkout page, but can't find the template. What's the location inside plugin, of the file that has checkout fields?


Source: (StackOverflow)

Woocommerce products showing “Out of stock” message when not actually out of stock

Having an issue with a client website where products are randomly displaying

"This product is currently out of stock and unavailable."

The thing is that we don't have inventory tracking turned on, all products should be in stock all the time.

When I go into the WordPress admin and click the update button for the product (without changing anything) and then the message goes away and the "Add to Cart" button displays as it should. Unfortunately, the issue repeats itself on these items in the future and to fix it I have to update the product again (also clicking update in the WooCommerce settings temporarily solves the problem).

I need to find a way to fix this problem permanently so it doesn't revert itself.

Site Details:

  • Using WordPress 3.9.1
  • WooCommerce 2.1.12
  • PHP 5.3.10 (also tried locally w/ 5.5.10)

What I've tried

  • Removing all plugins but WooCommerce
  • Changing themes (tried all standard WordPress themes and a few WooThemes themes to no avail)
  • Updated WooCommerce (it was v 2.1.5)
  • Manually inserted to wp_postmeta adding the meta key _manage_stock and set it to no (as observed in some of the working products we have)
  • Manually inserted to wp_postmeta adding the meta key '_stock' and set it to 0 (as observed in some of the working products we have)
  • Compared the postmeta of the product before and after clicking the update button to observe if anything had changed that would cause the problem...nothing
  • No caching is turned on
  • Used XDebug to track the $available_variations variable in variable.php and class-wc-ajax.php and class-wc-product-variable.php.

When debugging I noticed that in products that were working the $available_variations variable contained 2 arrays filled with product information, however on the products that didn't, it was an empty array (which results in the out of stock message being displayed as served by variable.php). Clicking the update button and then tracking the variable shows the information correctly.

Honestly at this point I'm at a loss. I think the issue is somewhere in either of the classes, but I can't tell.

Has anyone else had issues with this? Any way around this issue so I don't have to keep updating products for this client.

Link to product that is out of stock (as of posting) http://highlandsministriesonline.org/product/basement-tapes/resentment-in-the-church-135/

Link to product that is displaying correctly http://highlandsministriesonline.org/product/basement-tapes/bt0111-church-at-corinth-i/

Error Log I added an error log to the product variation class. Heres the result:

For products with the "out of stock" message, nothing displays in the log.

An example of a product that function correctly:

WC_Product_Variation Object
(
    [variation_id] => 1310
    [parent] => WC_Product_Variable Object
        (
            [children] => Array
                (
                    [0] => 1311
                    [1] => 1310
                )

            [total_stock] => 
            [id] => 1308
            [post] => WP_Post Object
                (
                    [ID] => 1308
                    [post_author] => 1
                    [post_date] => 2012-04-03 14:29:42
                    [post_date_gmt] => 2012-04-03 18:29:42
                    [post_content] => This audio presentation on homeschooling is a Basement Tape conversation on homeschooling.
<h2>What are The Basement Tapes?</h2>
<em>The Basement Tapes</em> are conversations between Dr. R.C. Sproul Jr., Rev. Laurence Windham, their friends and special guests, on subjects related to the “good life,” a simple, separate, and deliberate life, lived out for the glory of God and for the building of His Kingdom.

You might even say that the Basement Tapes serve to help us develop a Christian worldview. Started in 2002, we now have over #140 conversations on all areas of life that we are seeking to think biblically about so that we might take dominion over them for the King and His kingdom.

<strong>Format: Available in both CD and MP3</strong>

<strong>Length: Sixty minutes</strong>
                    [post_title] => Basement Tape #109: Homeschooling Redux
                    [post_excerpt] => It's been 108 weeks since we last talked about the subject of homeschooling our children. Our tykes have grown into teenagers. What have we learned in the past nine years? What has changed? What remains the same is that homeschooling means discipleship; passing down your most important convictions to your children and teaching them our holy religion.
                    [post_status] => publish
                    [comment_status] => open
                    [ping_status] => closed
                    [post_password] => 
                    [post_name] => bt0109-homeschooling-reducks
                    [to_ping] => 
                    [pinged] => 
                    [post_modified] => 2014-07-07 11:08:46
                    [post_modified_gmt] => 2014-07-07 15:08:46
                    [post_content_filtered] => 
                    [post_parent] => 0
                    [guid] => http://50.57.190.158/?post_type=product&#038;p=1308
                    [menu_order] => 0
                    [post_type] => product
                    [post_mime_type] => 
                    [comment_count] => 0
                    [filter] => raw
                )

            [product_type] => variable
        )

    [variation_data] => Array
        (
            [attribute_pa_audio-options] => mp3-download
        )

    [variation_has_length] => 
    [variation_has_width] => 
    [variation_has_height] => 
    [variation_has_weight] => 
    [variation_has_stock] => 
    [variation_has_sku] => 1
    [variation_shipping_class] => 
    [variation_shipping_class_id] => 
    [variation_has_tax_class] => 
    [variation_has_downloadable_files] => 1
    [id] => 1308
    [post] => WP_Post Object
        (
            [ID] => 1308
            [post_author] => 1
            [post_date] => 2012-04-03 14:29:42
            [post_date_gmt] => 2012-04-03 18:29:42
            [post_content] => This audio presentation on homeschooling is a Basement Tape conversation on homeschooling.
<h2>What are The Basement Tapes?</h2>
<em>The Basement Tapes</em> are conversations between Dr. R.C. Sproul Jr., Rev. Laurence Windham, their friends and special guests, on subjects related to the “good life,” a simple, separate, and deliberate life, lived out for the glory of God and for the building of His Kingdom.

You might even say that the Basement Tapes serve to help us develop a Christian worldview. Started in 2002, we now have over #140 conversations on all areas of life that we are seeking to think biblically about so that we might take dominion over them for the King and His kingdom.

<strong>Format: Available in both CD and MP3</strong>

<strong>Length: Sixty minutes</strong>
            [post_title] => Basement Tape #109: Homeschooling Redux
            [post_excerpt] => It's been 108 weeks since we last talked about the subject of homeschooling our children. Our tykes have grown into teenagers. What have we learned in the past nine years? What has changed? What remains the same is that homeschooling means discipleship; passing down your most important convictions to your children and teaching them our holy religion.
            [post_status] => publish
            [comment_status] => open
            [ping_status] => closed
            [post_password] => 
            [post_name] => bt0109-homeschooling-reducks
            [to_ping] => 
            [pinged] => 
            [post_modified] => 2014-07-07 11:08:46
            [post_modified_gmt] => 2014-07-07 15:08:46
            [post_content_filtered] => 
            [post_parent] => 0
            [guid] => http://50.57.190.158/?post_type=product&#038;p=1308
            [menu_order] => 0
            [post_type] => product
            [post_mime_type] => 
            [comment_count] => 0
            [filter] => raw
        )

    [product_type] => variation
    [product_custom_fields] => Array
        (
            [_sku] => Array
                (
                    [0] => BT0109MP3
                )

            [_price] => Array
                (
                    [0] => 3
                )

            [_sale_price] => Array
                (
                    [0] => 
                )

            [_weight] => Array
                (
                    [0] => 
                )

            [_length] => Array
                (
                    [0] => 
                )

            [_width] => Array
                (
                    [0] => 
                )

            [_height] => Array
                (
                    [0] => 
                )

            [_stock] => Array
                (
                    [0] => 
                )

            [_thumbnail_id] => Array
                (
                    [0] => 0
                )

            [_virtual] => Array
                (
                    [0] => yes
                )

            [_downloadable] => Array
                (
                    [0] => yes
                )

            [_download_limit] => Array
                (
                    [0] => 
                )

            [_downloadable_files] => Array
                (
                    [0] => a:1:{s:32:"a0a87d43e69ed9de646ba479c252c9fb";a:2:{s:4:"name";s:29:"109-Homeschooling-Reducks.mp3";s:4:"file";s:113:"http://highlandsministriesonline.org/wp-content/uploads/woocommerce_uploads/2012/04/109-Homeschooling-Reducks.mp3";}}
                )

            [_regular_price] => Array
                (
                    [0] => 3
                )

            [_sale_price_dates_from] => Array
                (
                    [0] => 
                )

            [_sale_price_dates_to] => Array
                (
                    [0] => 
                )

            [_download_expiry] => Array
                (
                    [0] => 
                )

            [attribute_pa_audio-options] => Array
                (
                    [0] => mp3-download
                )

        )

    [sku] => BT0109MP3
    [downloadable_files] => a:1:{s:32:"a0a87d43e69ed9de646ba479c252c9fb";a:2:{s:4:"name";s:29:"109-Homeschooling-Reducks.mp3";s:4:"file";s:113:"http://highlandsministriesonline.org/wp-content/uploads/woocommerce_uploads/2012/04/109-Homeschooling-Reducks.mp3";}}
    [downloadable] => yes
    [virtual] => yes
    [sale_price_dates_from] => 
    [sale_price_dates_to] => 
    [price] => 3
    [regular_price] => 3
    [sale_price] => 
    [total_stock] => 0
    [dimensions] => 
)

Potential Next Steps My next thought if I cant get this resolved is to write a script that will go through and update each product on the website on a nightly basis (since the temporary fix seems to work for at least 24 ours). Though if I can save some of my time I'd rather not if someone else has been able to solve this issue permanently.

Update as of October 23,2014

This issue is still occurring despite attempting the answers below. I have created a logging plugin to hopefully help track the frequency of the out of stock messages and hopefully help correlate the message with any actions taken place on the website. It's currently customized to this specific website, but I'm working on a version to share with you all so that even if I can't find the answer, someone can.


Source: (StackOverflow)

Advertisements

get_post_meta in woocommerce email notifications

I'm trying to get some data from an order in a woocommerce email template, but get_post_meta just returns false. This code works on the thankyou page. I have spent too much time on this. Any help would be appreciated. Thanks!

global $post;

echo "test!!!<br />";
$x = get_post_meta( $order->id, 'attendee_data', true );
$y = get_post_meta( $order->id, 'attendee_test', true );
echo $order->id . '<br />';
echo $x;
echo $y;

I've attached a picture of the sql as well as an email.

SQL: http://i.stack.imgur.com/zUFBa.png

Email: http://i.stack.imgur.com/Uqtih.png

The whole email template:

<?php do_action('woocommerce_email_header', $email_heading); ?>

<p><?php _e( "Your order has been received and is now being processed. Your order details are shown below for your reference:", 'woocommerce' ); ?></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( $order->is_download_permitted(), true, ( $order->status=='processing' ) ? true : false ); ?>
    </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
global $post;

echo "test!!!<br />";
$x = get_post_meta( $order->id, 'attendee_data', true );
$y = get_post_meta( $order->id, 'attendee_test', true );
echo $order->id . '<br />';
echo $x;
echo $y;
foreach ( $x as $k => $p ) {
    echo $k ." ... ". $p;
}  ?>

<?php // attendee_order_details($order->get_order_number()) ?>

<?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' ); ?>

Source: (StackOverflow)

WooCommerce - get category for product page

For my WC product pages, I need to add a class to the body tag so that I can perform some custom styling. Here's the function I'm creating for this...

function my_add_woo_cat_class($classes) {

$wooCatIdForThisProduct = "?????"; //help!

// add 'class-name' to the $classes array
$classes[] = 'my-woo-cat-id-' . $wooCatIdForThisProduct;
// return the $classes array
return $classes;
}

//If we're showing a WC product page
if (is_product()) {
    // Add specific CSS class by filter
    add_filter('body_class','my_add_woo_cat_class');
}

...but how do I get the WooCommerce cat ID?


Source: (StackOverflow)

How do I programmatically apply a coupon in Woocommerce?

In Woocommerce I'm trying to find a way to apply a 10% discount to an entire customer's order if the weight in the cart is over 100 lbs. I'm part way to achieving this. For the next step, I'm looking for a way to programmatically apply a coupon code via action/hook through functions.php.

It appears that I can use the function woocommerce_ajax_apply_coupon to do this ( http://docs.woothemes.com/wc-apidocs/function-woocommerce_ajax_apply_coupon.html ) but I am unsure of how to use it.

So far I've modified cart.php to get the total weight of all the products in the cart, i've created a coupon that applies the discount (if entered manually) and I've added some code to functions.php to check the weight and display a message to the user.

EDIT: Partial code removed, completed code included in solution below.


Thanks for the guidance Freney. Here's the working end result which successfully applies the discount coupon when the condition is met and also removes it when it's no longer met:

/* Mod: 10% Discount for weight greater than 100 lbs 
Works with code added to child theme: woocommerce/cart/cart.php lines 13 - 14: which gets $total_weight of cart:
        global $total_weight;
        $total_weight = $woocommerce->cart->cart_contents_weight;
*/
add_action('woocommerce_before_cart_table', 'discount_when_weight_greater_than_100');
function discount_when_weight_greater_than_100( ) {
    global $woocommerce;
    global $total_weight;
    if( $total_weight > 100 ) {
        $coupon_code = '999';
        if (!$woocommerce->cart->add_discount( sanitize_text_field( $coupon_code ))) {
            $woocommerce->show_messages();
        }
        echo '<div class="woocommerce_message"><strong>Your order is over 100 lbs so a 10% Discount has been Applied!</strong> Your total order weight is <strong>' . $total_weight . '</strong> lbs.</div>';
    }
}

/* Mod: Remove 10% Discount for weight less than or equal to 100 lbs */
add_action('woocommerce_before_cart_table', 'remove_coupon_if_weight_100_or_less');
function remove_coupon_if_weight_100_or_less( ) {
    global $woocommerce;
    global $total_weight;
    if( $total_weight <= 100 ) {
        $coupon_code = '999';
        $woocommerce->cart->get_applied_coupons();
        if (!$woocommerce->cart->remove_coupons( sanitize_text_field( $coupon_code ))) {
            $woocommerce->show_messages();
        }
        $woocommerce->cart->calculate_totals();
    }
}

Source: (StackOverflow)

Woocommerce add to cart button redirect to checkout

I created an ecommerce using the plugin woocommerce. I am selling only a subscription so the "/cart/" page is useless. I'm trying to get rid of it so that when my customer click on "Add to cart" button, he ends up on the checkout page.


Source: (StackOverflow)

How to filter WooCommerce products by custom attribute

I'm trying to filter WooCommerce product archive by custom attributes.

For example, there are 5 products with attribute "color" containing "red", and another 3 with attribute "color" containing "blue".

How can I apply a filter to the products loop, so only the products containing "red" will be shown?

Thanks


Source: (StackOverflow)

Woocommerce getting custom attributes

I am trying to get products custom attribute values but i am miserably failing to do so. i tried to do

global $woocommerce, $post, $product;
$res = get_post_meta($product->id);
print_r(unserialize($res['_product_attributes'][0]));

and im getting

[pa_koostis] => Array
        (
            [name] => pa_koostis
            [value] => 
            [position] => 0
            [is_visible] => 1
            [is_variation] => 0
            [is_taxonomy] => 1
        )

i know that there is a value because it shows it in the attribute section but i just cant find a way to get it to show in my custom code.


Source: (StackOverflow)

Woocommerce - php to get order information

I'm trying to get the data associated with an order on the woocommerce plugin (wordpress). Currently, I have written my own plugin that contains the code:

<?php 
global $woocommerce;
$order = new WC_Order($order_id);
$order_shipping_total = $order->get_shipping();
echo $order_shipping_total;
?>

This is just to test it out, I don't believe this is working- BUT what I actually need is to get a list of the orders that have a certain order status, and then be able to access the fields(like first name) for each order in this list. How do I go about doing this? Also, what files do I include to make this work? The class-wc-order() file?


Source: (StackOverflow)

Changes on archive-product.php doesn't work

I'm trying to customize the standard woocommerce theme and so far that has worked well. I copied all template files from /plugins/woocommerce/templates to /mytheme/woocommerce and customized the files.

But when i'm change something in archive-product.php nothing happens? I even tried to customize this in the core files (/plugins/woocommerce/templates/archive-product.php) but i doesn't work.

I want to change the class of the h1 heading: <h1 class="page-title"><?php woocommerce_page_title(); ?></h1>.

So i looked up all woocommerce template files, the class page-title occurs only in this one file (to prevent editing the wrong file).

Edit:

In detail, i want to customize the theme used in this path: http://example.com/product-category/mycategory


Source: (StackOverflow)

How to disable the quantity field in the product detail page in woocommerce?

I need to hide the "quantity" field (where one enters the quantity before adding to cart) in Woocommerce on the product detail page, and only show the "add-to-cart"-button, which would then put the quantity of 1 in the cart. The reason is because I gather the quantity based on a Gravity Forms.


Source: (StackOverflow)

Woocommerce - add serial to products in orders

I am looking for a way to add a serial number to each sold product, in the order display in WooCommerce. What I need is a manual solution, where the S/N of the sent product is added to the order before the package is sent. I have tried to show it on the following picture:

Possibility of adding a serial

Originally, I thought about implementing it as a product meta (without really knowing how) but I realized that product metas can only be altered when the order is set on hold and not while processing.

Any ideas aboout how to proceed? Thanks already!


Source: (StackOverflow)

How to insert product in cart through rest api provided by woocommerce?

i am converting a woocommerce store into phonegap app. Now i am stuck. how to add product into cart and display cart into the phonegap app. So how to do it with the help of rest api.

Thanks in advance


Source: (StackOverflow)

Change total and tax_total Woocommerce

I create custom checkout page with custom calculation. How i cant change total and tax_total via Ajax (or refresh page if need).

I create custom page for ajax request and set this code

$ss = new WC_Session_Handler();
$ss->set('tax_total',9999999);
$ss->save_data();
$ss->set('total',9999999);
$ss->save_data(); 

var_dump(WC());

On this page i can see my changes, but 'checkout page' nothing happens (even after refresh). How can I change the arbitrary total or tax_total.


Source: (StackOverflow)

Update Woocommerce cart after adding a product (jQuery)

The Ajax seems to work just fine but the cart content won't refresh as expected. I want the contents of the cart to be refreshed once the "add to cart" button is clicked. As it is now, I have to refresh the page manually to see the added products.

I'm using this function to add a product to my woocommerce cart:

      function addToCart(p_id) {
            jQuery.ajax({
              type: 'POST',
              url: '/wp/?post_type=product&add-to-cart='+p_id,
              data: { 'product_id':  p_id,
              'quantity': amount},
              success: function(response, textStatus, jqXHR){
                    console.log("Product added");
                }/*,
              dataType: 'JSON'*/
            }); 
      }

    jQuery('#addToCart').click(function(e) {
      e.preventDefault();
      addToCart(prod_id["product_id"]);
      return false;
    });  

Is it possible to refresh only the cart after a product was added?


Source: (StackOverflow)