Posts

Aelia - News badge

WooCommerce Tips & Tricks – Get all the categories to which a product belongs

As active contributors of several communities, such as the Advanced WooCommerce and WooCommerce Help & Share groups on Facebook, we came across a question that seem to be quite frequent.

How to get all of a product’s categories

This operation is simple, it just requires a bit more work than one would expect. It’s very easy to fetch the categories to which a product is assigned directly, but a product may also belong to parent categories (a parent category is a category to which a subcategory belongs). The screenshot below explains the concept.

Product category hierarchy

In this example, the product belongs directly to “Subcategory A1” and “Some other category”. It also belongs indirectly to “Category A”, as “Subcategory A1” is a child of that category.

In the above example, the product belongs to the following categories:

  • Directly to Subcategory A1 and Some other category, as it’s assigned directly to them.
  • Indirectly to Category A, which is the parent of Subcategory A1.

Now that the concepts are clear, let’s get coding.

Step 1 – Get the direct categories of a product

This is the easiest part. It’s just a matter to find all the “category” terms associated to a product. Our function will look like this:

function aelia_get_product_categories($product, $return_raw_categories = false) {
  $result = array();
  // Get all the categories to which a product is assigned
  $categories = wp_get_post_terms($product->id, 'product_cat');
  
  // The $categories array contains a list of objects. Most likely, we would 
  // like to have categorys slug as a keys, and their names as values. The#
  // wp_list_pluck() function is perfect for this
  $categories = wp_list_pluck($categories, 'name', 'slug');
  return $categories;
}

The result of this function, once applied to our example product, will be the following:

array(
  'subcategory-a1' => 'Subcategory A1',
  'some-other-category' => 'Some other category',
)

All good, we have the categories to which the product is assigned directly. Now we need to get all the parent categories.

Step 2 – Get the parent category (or categories) of a given category

To keep things tidy, we will create a second function to get the parent categories of a category. This requires a similar approach to the one used for the products.

function aelia_get_parent_categories($category_id) {
  $parent_categories = array();
  // This will retrieve the IDs of all the parent categories 
  // of a category, all the way to the top level
  $parent_categories_ids = get_ancestors($category_id, 'product_cat');
  
  foreach($parent_categories_ids as $category_id) {
    // Now we retrieve the details of each category, using its
    // ID, and extract its name
    $category = get_term_by('id', $category_id, 'product_cat');
    $parent_categories[$category->slug] = $category->name;
  }
  return $parent_categories;
}

The above will return the following result for Subcategory A1:

array(
  'category-a' => 'Category A'
)

As before, we have a list with category slugs as keys, and category names as values. Time to finish the job.

Step 3 – Putting the pieces together

Now that we can get both the direct categories of a product and their parent categories, we can alter the first function to call the second and give us a result that includes all the categories. The modified function will look as follows:

function aelia_get_product_categories($product, $return_raw_categories = false) {
  $result = array();
  $categories = wp_get_post_terms($product->id, 'product_cat');

  if(is_array($categories) && !$return_raw_categories) {
    $parent_categories = array();
    // Retrieve the parent categories of each category to which
    // the product is assigned directly
    foreach($categories as $category) {
      // Using array_merge(), we keep a list of parent categories
      // that doesn't include duplicates
      $parent_categories = array_merge($parent_categories, aelia_get_parent_categories($category->term_id));
    }
    // When we have the full list of parent categories, we can merge it with
    // the list of the product's direct categories, producing a single list
    $categories = array_merge($parent_categories, wp_list_pluck($categories, 'name', 'slug'));
  }
  return $categories;
}

As you will probably have guessed, the new function will return the following result:

array(
  'subcategory-a1' => 'Subcategory A1',
  'some-other-category' => 'Some other category',
  'category-a' => 'Category A',
)

That is, a list of all the direct and indirect categories to which a product belongs. Mission accomplished!

For your convenience, you can find the complete code here: WooCommerce – WooCommerce – Get product categories, including parent categories (Pastebin).

Need help?

Should you need help implementing the solution, or if you would need to have the category search functions implemented as part of a more complex custom project, please feel free to contact us. We will review your specifications and give you a quote for your customisation.

The Aelia Team

Aelia - News badge

Wholesale Pricing plugin now supports our Currency Switcher for WooCommerce

The Wholesale Plugin joins the multi-currency family

We have been informed by our colleagues at Rymera Web Co that their Wholesale Pricing plugin now includes native support for our popular Currency Switcher for WooCommerce. This means that you will be able to take advantage of the multi-currency features provided by our plugin when setting wholesale prices, making your pricing strategy more flexible than ever.

Your wholesale clients will be pleased to see prices in their own currency, and you will be able to give them tailored offers. Now, that’s fantastic news!

Would you like to help? You can!

If you are a customer, and would like to enjoy the benefits of a multi-currency shop, but you use one or more incompatible plugins, simply contact the plugins’ authors. Let them know how important it is to support multiple currencies, in a global market, and ask them to add support for it in their products. Tell them that they can contact us at any time, and we will help them getting started in no time.

If you are a plugin developer, and would like to make your plugin multi-currency aware, simply get in touch. We will be happy to provide you with instructions, suggestions and examples to get you started. It’s easier than it seems!

Acknowledgements

We would like to thank the Rymera Web Co team for the effort they put in developing the integration with our Currency Switcher for WooCommerce. Having their product join the ever growing family of multi-currency plugins surely involved a lot of work, and it will contribute to strengthen our cause. We are steadily growing, and soon having multiple currencies will become the standard.

Thanks to you all, customers and developers, for your invaluable support and efforts!

The Aelia Team

Aelia - News badge

WooCommerce Tips & Tricks – Only allow specific product combinations in cart

This post was written in December 2015. Based on our tests, the code works with with WooCommerce 2.5 and 2.6. Please keep in mind that the code example are provided “as is”, without explicit or implicit warranties. You might have to adjust the code to make it work with your specific configuration.

Update – 01 March 2018

You can find a link to the code for WooCommerce 3.x at the bottom of the article. 

A member of the Advanced WooCommerce group on Facebook presented an interesting challenge. She needed to allow customers to purchase any products freely, except in one case. She had a specific product (let’s calls it Product X) that had to be purchased “alone”, without any other product being present in the cart. In short:

  • If Product X is in the cart, that must be the only product in the cart.
  • If Product X is not in the cart, any other product can be added to it and purchased at the same time.

Our friend Rodolfo, from BusinessBloomer, posted a solution that he adapted from his solution to allow only one product to the cart. It works, but in our opinion, that approach presented a few limitations:

  1. It works by emptying the cart when Product X is added after the other products. If a customer adds Product X to the cart, then he can add other products, and they will stay there.
  2. It doesn’t allow to have combinations of products (e.g. Product X and Product Y allowed together).
  3. It doesn’t make clear to the customer that other products cannot be purchased together with Product X (all products retain their “Add to cart” button, even if they should not).
  4. It empties the cart explicitly. We try to avoid this type of calls whenever possible, and rely on WooCommerce’s internal logic to decide what items should be removed, and when.

Our approach

Taking advantage of the experience gained with the development of our Prices by Country plugin, we prepared a different solution, which, in our opinion, is more flexible and user friendly. It brings the following advantages:

  • It covers the requirement described above, where a specific product (e.g. Product X) must be the only one in the cart.
  • It also allows to have more than one product allowed in the cart (e.g. Product X and Product Y), while excluding all others.
  • It clearly informs the customers that some products can’t be purchased anymore.

You can find it below, described step by step. The code can be added to the theme’s functions.php, or packaged in a plugin, if needed. It has been tested with WooCommerce up to version 2.5.

Step 1 – Keep track of what’s in the cart

The first thing to do is to determine what is in the cart. The content of the cart will dictate what else can be added to it. We do this operation only once per page load, for better performance.

/**
 * Retrieves the cart contents. We can't just call WC_Cart::get_cart(), because
 * such method runs multiple actions and filters, which we don't want to trigger
 * at this stage.
 *
 * @author Aelia <support@aelia.co>
 */
function aelia_get_cart_contents() {
  $cart_contents = array();
  /**
   * Load the cart object. This defaults to the persistant cart if null.
   */
  $cart = WC()->session->get( 'cart', null );

  if ( is_null( $cart ) && ( $saved_cart = get_user_meta( get_current_user_id(), '_woocommerce_persistent_cart', true ) ) ) {
    $cart = $saved_cart['cart'];
  } elseif ( is_null( $cart ) ) {
    $cart = array();
  }

  if ( is_array( $cart ) ) {
    foreach ( $cart as $key => $values ) {
      $_product = wc_get_product( $values['variation_id'] ? $values['variation_id'] : $values['product_id'] );

      if ( ! empty( $_product ) && $_product->exists() && $values['quantity'] > 0 ) {
        if ( $_product->is_purchasable() ) {
          // Put session data into array. Run through filter so other plugins can load their own session data
          $session_data = array_merge( $values, array( 'data' => $_product ) );
          $cart_contents[ $key ] = apply_filters( 'woocommerce_get_cart_item_from_session', $session_data, $values, $key );
        }
      }
    }
  }
  return $cart_contents;
}

// Step 1 - Keep track of cart contents
add_action('wp_loaded', function() {
  // If there is no session, then we don't have a cart and we should not take
  // any action
  if(!is_object(WC()->session)) {
    return;
  }

  // This variable must be global, we will need it later. If this code were
  // packaged as a plugin, a property could be used instead
  global $allowed_cart_items;
  // We decided that products with ID 737 and 832 can go together. If any of them
  // is in the cart, all other products cannot be added to it
  global $restricted_cart_items;
  $restricted_cart_items = array(
    737,
    832,
  );

  // "Snoop" into the cart contents, without actually loading the whole cart
  foreach(aelia_get_cart_contents() as $item) {
    if(in_array($item['data']->id, $restricted_cart_items)) {
      $allowed_cart_items[] = $item['data']->id;

      // If you need to allow MULTIPLE restricted items in the cart, comment
      // the line below
      break;
    }
  }
});

Step 2 – Prevent disallowed product combinations

Now that we know what’s in the cart, we can prevent some products from being added to it if any of the “restricted” products are present. Emptying the cart would not work, as we would risk to throw away one of the allowed products. Instead, we simply make the disallowed products unavailable. This will have several effects:

  • If any of the disallowed products is already in the cart, WooCommerce will remove it.
  • The Add to Cart button will be replaced by a Read More button on the disallowed products. Customers won’t be able to add the products back, and will instead get a note explaining that they cannot be purchased.
// Step 2 - Make disallowed products "not purchasable"
add_filter('woocommerce_is_purchasable', function($is_purchasable, $product) {
  global $restricted_cart_items;
  global $allowed_cart_items;

  // If any of the restricted products is in the cart, any other must be made
  // "not purchasable"
  if(!empty($allowed_cart_items)) {
    // To allow MULTIPLE products from the restricted ones, use the line below
    //$is_purchasable = in_array($product->id, $allowed_cart_items) || in_array($product->id, $restricted_cart_items);

    // To allow a SINGLE  products from the restricted ones, use the line below
    $is_purchasable = in_array($product->id, $allowed_cart_items);
  }
  return $is_purchasable;
}, 10, 2);

At this stage, we have the code that fulfils the original requirements. However, we need one extra step to make it more elegant.

Step 3 – Explain customers why some products cannot be purchased anymore

As we have seen, the code in step 2 prevents some products from being added to the cart if Product X and/or Product Y are already present, but it doesn’t explain customers why. We just need to show them a message with some information about the restrictions, to make things clearer.

// Step 3 - Explain customers why they can't add some products to the cart
add_filter('woocommerce_get_price_html', function($price_html, $product) {
  if(!$product->is_purchasable() && is_product()) {
    $price_html .= '<p>' . __('This product cannot be purchased together with "Product X" or "Product Y". If you wish to buy this product, please remove the other products from the cart.', 'woocommerce') . '</p>';
  }
  return $price_html;
}, 10, 2);

Step 4 – Combining the code

For the snippets above to work together, we must combine them in the correct order. More specifically, the code from step 2 should go inside the code from step 1. Here’s the complete code, ready to be pasted in the functions.php file: http://pastebin.com/BRU1BP2E.

Update – 01 March 2018

We prepared an example of how the code can be adapted for WooCommerce 3.x. You can find the code here: https://pastebin.com/tRbJKt37.

Step 5, 6, 7, etc – Improvements

The above solution is fully functional, but it would be possible to make it more elegant and flexible. Further improvements to the code could include the following:

  • Packaging the code as a plugin. This will help avoiding global variables and could make the code tidier and easier to read.
  • Adding support for groups of restricted products (e.g. Product X and Y or Product A and B, etc).
  • Adding a dynamically generated message, showing exactly which restricted products are in the cart, instead of relying on static text.
  • Adding formatting to the message displayed to the customers.

Should you need assistance adapting the solution to your needs, or implementing any of the above optimisations, please feel free to contact us. We will review your specifications and provide you with a quote for your customisation.

The Aelia Team

Aelia - News badge

More great plugins now support our WooCommerce Currency Switcher

Support for our multi-currency solution is growing

We are delighted to inform you that, day by day, more developers are recognising the importance of adding support for multi-currency environments to their plugins. When we released our plugin, the WooCommerce Currency Switcher, in 2013, we were pioneers in this field. At the time, not only there wasn’t any solution to make WooCommerce truly multi-currency, but there was nobody interested in developing one, either.

At the time, we were told by many developers that supporting multiple currencies was “impossible“, that there was no way to make the shop work correctly with more than one currency, and that no merchant would ever be interested in having such feature on their site. We are proud to say that we proved them wrong.

The ever-growing popularity of our Currency Switcher, and our other internationalisation solutions, such as Prices by Country, Tax Display by Country, and EU VAT Assistant, is now attracting the interest of many developers, who regularly contact us to see how they can extend their solutions to support multi-currency environments. As always, we are happy to help our colleagues in their endeavours, and pleased to see that so many want to join our project.

Two (and a half) more plugins join the multi-currency world

As the title indicates, we are happy to announce that two (and a half) more plugins have now been updated to support out multi-currency solution:

  • WooCommerce Multiple Free Gift This plugin allows you to easily grant gifts to your customers, based on flexible, custom criteria. Rules such as “buy X, get Y free“, or “free gifts with any purchase” can be created easily, and applied to your customer’s cart automatically. We would like to thank its author, Ankit Pokhrel, for the good job he did, testing and extending his plugin together with our Currency Switcher to achieve maximum compatibility.
  • WooCommerce Role Based Price This is a great plugin for the merchant who is dealing with different types of customers. As the name indicates, the Role Based Price plugin allows to sell products at different prices, depending on the role held by a customer. Wholesalers, consumers, VIP clients, they can all get their own, dedicated price.The integration with our Currency Switcher now allows you to enter such prices ineach of the currencies you enabled, greatly increasing your pricing options.We would like to thank Varun Sridharan for the effort he put into adding multi-currency support to his plugin. He is a great example of dedication, and his excellent work reflects that.
  • WooCommerce Product Fees This is the “half” plugin that we mentioned at the beginning of the paragraph. The reason why we call it “half” will be clear shortly.The WooCommerce Product Fees plugin was released on the 25th August 2015 by Caleb Burks, a member of WooThemes Support Team and, less than 24 hours later, we had extended it with full support for multiple currencies. Our integration added support for per-currency product fees, with automatic conversions as a fallback, while still maintaining full backward compatibility with single-currency environments.The above took us less than two hours of work, which is a great indicator of how easy it is to interface with our multi-currency solution.Why “half” plugin?Unfortunately, the author of the WooCommerce Product Fees plugin informed that, although our integration worked perfectly, he deemed it not to be important enough to be included in the main release. He reckoned that it would add unnecessary complexity, therefore he decided to leave the integration out for the moment, and to review it at a later stage. We respectfully disagree with such decision, for two reasons:
    1. Our extension required very few changes in the original code, and it doesn’t make the plugin much more complex.
    2. It’s easier to “think multi-currency” from the beginning of a plugin’s development, rather than try to “retrofit” such feature in a more complex product months, or years later.

    Of course, we do not want to enforce our vision and goals on anybody. We believe in collaboration, and freedom of choice is of utmost importance to us. We limited ourselves to explaining in detail why we believe that our integration should be part of the Product Fees plugin, and we will be happy to help Caleb in implementing it in the future. In the meantime, if you would like how a multi-currency aware of Product Fees plugin could work, you can get it from our Github fork: https://github.com/aelia-co/WooCommerce-Product-Fees.

    Important: please note that we only extended the first version of the plugin, which might have now been updated by its author. If you download the latest version of the Product Fees plugin, you will probably have to re-apply our changes to it.

Acknowledgements

We do understand that, despite one’s best effort, it’s not always viable to extend an existing product to work seamlessly in a multi-currency environment, and we appreciate all the effort put into it, no matter the final outcome. We would like to take the opportunity to extend our thanks to all the developers who worked on an integration, whether they decided to complete it or not. Thanks to all of you for your continued support and contributions!

The Aelia Team

Aelia - Product update badge

WooCommerce Subscriptions Integration – WC 2.4 fix (variations)

Subscriptions Integration Add-on for Currency Switcher for WooCommerce 2.0.x/2.1.x has been updated to version 1.2.9.150815. This update brings the following improvements and fixes:

  • Improved support for WooCommerce 2.4. Fixed issue caused by the logic used to handle variations in WooCommerce 2.4.3. The issue could cause Subscriptions to appear with a price of zero in some circumstances.
  • Fixed minor bugs in user interface. Removed notice messages from pricing interface for simple and variable subscriptions and fixed reference to text domain variable in variable subscriptions pricing interface.
  • Updated logic used to for requirements checking.
  • Updated logic used to check for updates.

Important: Subscriptions Integration Add-on requires Currency Switcher 3.3.11.140619 or later.

How to get latest version

You can download the plugin from the product page, free of charge. Support can be purchased separately.

Aelia - News badge

WooCommerce 2.4 – Dealing with the new price cache

WooCommerce 2.4 price cache – Pros and cons

If you wrote a WooCommerce plugin that works with product prices, and found out that it returns incorrect results when you process variable products, the cause could be the new price caching logic added in WooCommerce 2.4. This new logic, which is not extensively documented, was introduced to increase performance of sites with complex variable products (i.e. products with 20 variations or more).

To reduce processing and calculations, when WooCommerce retrieves the prices of a variable product , it stores them in a dedicated cache, and returns the stored data from that moment on, until the product is modified. When this happens, none of the hooks associated with product price calculations runs, and your code is will probably not run as it should. For example, if your plugin returns different prices depending on custom criteria, you will notice that this no longer happens, and that you will always see the same prices. Those prices come from the cache.

Two of our plugins were affected by this new caching mechanism: our multi-currency solution, the Currency Switcher, and our Prices by Country plugin. In both cases, we had to find a way to work around the limitation of having static prices shown to the customer, while still trying to keep the performance at a good level. Our plugins are now up to date, as we announced earlier, and we thought of sharing our approach, so that it can benefit other developers.

Solutions

Based on our tests, there are two solutions to this issue. The first is the best compromise between flexibility and performance, while the second can come useful if your code absolutely needs to get “raw”, live data.

1. Change the cache key for the prices, depending on your criteria

This solution allows to keep the price caching in place, while still keeping the data dynamic, choosing the correct one depending on arbitrary criteria. For example, suppose that a variable product should show two different set of prices:

  • Prices for wholesalers
  • Prices for the public

Your criteria, in this case, would be “customer is wholesaler“. You can then use it to ensure that the correct prices are loaded for the product. You can find an example showing how you can do this here: WooCommerce 2.4 – Price cache workaround – Dynamic key.

2. Disable the price cache entirely

This solution is no longer applicable as of WooCommerce 2.5

This solution will always give you access to live product prices, but it will disable price caching entirely. We would recommend not to use it, unless necessary. You can find the code here: WooCommerce 2.4 – Price cache workaround – Disable cache.

Conclusion

The new price caching system was unexpected and, although we understand why it was introduced, it can cause quite a bit of confusion. We hope that our examples will help you getting your product back on track, and add full compatibility with WooCommerce 2.4. If you have any questions, please feel free to contact us. You can also leave your feedback in the comments section, below.

The Aelia Team