Add product specific messages on a WooCommerce 'Thank You' page. - wordpress

I have a couple products that require additional information once they've been purchased. How can I display a simple message if a specific product has been purchased? Additionally, I'd like to display a simple message if a a product within the order belongs to a specific product category.

This should get you a message if an item from a specific product category is purchased:
function so_28348735_category_based_thank_you_message ( $order_id ){
$order = wc_get_order( $order_id );
$show = false;
foreach( $order->get_items() as $item ) {
// check if a product is in specific category
if ( has_term( 'your_product_category', 'product_cat', $item['product_id'] ) ) {
$show = true;
continue;
}
}
if( $show ){
echo 'your custom message';
}
}
add_action( 'woocommerce_thankyou', 'so_28348735_category_based_thank_you_message' );
Untested though, so your mileage may vary.

Have a look here.
This snippet will enable you to redirect users to a specific page if product X was purchased.
You can have multiple if-statements here if you have more than one product you'd like to have a custom redirection page for.

Related

In-store only products WooCommerce (WordPress)

I'm using WordPress with WooCommerce to sell physical products to customers online.
I would like to have either specific products or rather a specific category of products as "available in-store only" thus not displaying the "add to cart" button for those products, forcing the customers to get in touch with me or to physically come into my store to buy those products.
Some of these products are using variations (colors, options, etc) with different prices so I need to keep those products as "variable products".
Would love to have ideas / solutions to this as I can't find any. Thanks!
You can check for category slug or id, and filter woocommerce_is_purchasable like so:
function BN_restrict_store_only( $purchasable, $product ){
//The category by slug
$pickup_prod_cat_slug = 'available-in-store-only'; // slug
//The category by id
$pickup_prod_cat_ids ='681'; // the id of the product cat
// For variations (
if ( $product->is_type('variation') ) {
$parent = wc_get_product( $product->get_parent_id() );
$product_id = $parent->get_id();
if ( has_term ( array( $pickup_prod_cat_slug, $pickup_prod_cat_ids), 'product_cat', $product_id )) {
$purchasable = false;
}
}
// For simple and other product types
else {
$product_id = $product->get_id();
if ( has_term ( array( $pickup_prod_cat_slug, $pickup_prod_cat_ids), 'product_cat', $product_id )) {
$purchasable = false;
}
}
return $purchasable;
}
add_filter( 'woocommerce_is_purchasable', 'BN_restrict_store_only', 10, 2 );
add_filter( 'woocommerce_variation_is_purchasable', 'BN_restrict_store_only', 10, 2 );
Works both with simple products and variations.
You have to make a category with the slug "available-in-store-only" or get the id/slug of whatever product cat you want to use.
File goes in the functions.php of your child theme, or CodeSnippets.

WooCommerce Multivendor Marketplace get vendor id from product id

I am using WooCommerce Multivendor Marketplace plugin in Wordpress for multivendor site. I want to restrict a user so they can add only one vendor's product in their cart. I am writing a custom function to get vendor id.
I'm trying to use the get_wcfm_product_vendors function but it isn't working; I think that it might not be supported. I've search for other possible solutions but I could not find any. Is there any other method I can use to get vendor id for a product?
You may try WCFM single vendor checkout function, add this code to your child theme :
add_action( 'woocommerce_add_to_cart_validation', function( $is_allow, $product_id, $quantity ) {
$product = get_post( $product_id );
$product_author = $product->post_author;
//Iterating through each cart item
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$cart_product_id = $cart_item['product_id'];
$cart_product = get_post( $cart_product_id );
$cart_product_author = $cart_product->post_author;
if( $cart_product_author != $product_author ) {
$is_allow = false;
break;
}
}
if( !$is_allow ){
// We display an error message
wc_clear_notices();
wc_add_notice( __( "Well, you already have some item in your cart. First checkout with those and then purchase other items!", "wcfm" ), 'error' );
}
return $is_allow;
}, 50, 3 );
https://docs.wclovers.com/tweaks/#single-vendor-checkout

Create a Woocommerce product sold in units of gram

I would like to create a product in WooCommerce that is sold in units of gram.
The customer would enter the number of grams they want (in an input field) on the product page, and the price would be computed on the fly and added to the cart.
My question is: is this possible, and if so, can someone give me just a "big picture" idea of how I would implement it?
I don't need line-by-line code, just hoping someone with more knowledge of the structure of Woo can guide me on how to best attack the problem.
I already have parts of it worked out:
I can decide that the price entered for the product is the price per
100 grams, so that is how the seller will enter the price.
Then I can
write a little bit of Javascript to compute the price on the fly and
display it on the page as the user types the amount they want. No
problem.
But... I think every discrete product in Woo needs to have its own price.. So for example, if a customer wants 123g of a product, it seems like I might have to create a variation on the fly for that specific price/amount, and then add that to the cart. Which (judging by this) looks non-trivial and a little hacky. Is there a better way to do this?
WooCommerce has an option to show the weights as grams.
The following code will display the KG weights as grams on the WooCommerce templates :
// Convert the product weight
function ag_woocommerce_product_get_weight( $weight ) {
// Only convert if we have a weight
if ($weight) {
// The weight is in KGS, and we want grams, to multiple by 1000
$weight = $weight * 1000;
}
return $weight;
};
// add the filter
add_filter( 'woocommerce_product_get_weight', 'ag_woocommerce_product_get_weight', 10, 1 );
Hope this might help. Cheers!
There is a free plugin for WooCommerce that allows you to input a unit of measure (UOM) for each product:
https://wordpress.org/plugins/woocommerce-unit-of-measure/
I found this plugin that does pretty much exactly what I need-- https://woocommerce.com/products/measurement-price-calculator/
It's easier and quicker to give you that real example, than explain step by step… You will see which hooks are used for all steps or tasks.
You dont need variable products or generate a variation on the fly.
You just need to set on each simple product the price for one gram (or any other base). Now in this code, you can target those products with:
an array of product Ids
or by product categories (or even product tags).
Your concern is about the way to pass the data in the cart, to update the final price for each product and display the chosen grams amount in cart, checkout and in the order.
So in each product you will only set the price by gram… (or you can also make changes in the code and set the product price for 100 grs or even any other base).
The code:
// Add a product custom field "grams_quantity" that will update the displayed price
add_action('woocommerce_before_add_to_cart_button', 'special_product_by_grams', 25);
function special_product_by_grams(){
global $product;
// HERE Define the special product IDs sold by grams
$targeted_product_ids = array(37);
// or HERE Define a product categories (ids, slugs or names)
$categories = array('sold-by-gram');
// Only for products sold by gram
$product_id = $product->get_id();
if ( ! ( in_array( $product_id, $targeted_product_ids ) || has_term( $categories, 'product_cat', $product_id ) ) ) return;
?>
<div class="grams-field">
<label for="grams_quantity"><?php _e('Grams: ','woocoomerce'); ?><span></span><br>
<input type="number" step="1" name="grams_quantity" class="grams_quantity" id="grams_quantity" value="1">
</label>
</div><br>
<script type="text/javascript">
(function($){
// variables initialization
var priceByGram = <?php echo wc_get_price_to_display( $product ); ?>,
currencySymbol = $(".woocommerce-Price-currencySymbol").html(),
updatedPrice;
// On live event: imput number fields
$('input#grams_quantity').on( "click blur", function(){
updatedPrice = ($(this).val() * priceByGram).toFixed(2);
$(".woocommerce-Price-amount.amount").html('<span class="woocommerce-Price-amount amount">'+updatedPrice+' '+currencySymbol+'</span>');
console.log("event"); // <== To be removed
});
})(jQuery);
</script>
<?php
}
// Save the "grams_quantity" custom product field data in Cart item
add_filter( 'woocommerce_add_cart_item_data', 'save_in_cart_the_custom_product_field', 10, 2 );
function save_in_cart_the_custom_product_field( $cart_item_data, $product_id ) {
if( isset( $_POST['grams_quantity'] ) ) {
$cart_item_data[ 'grams_quantity' ] = $_POST['grams_quantity'];
// When add to cart action make an unique line item
$cart_item_data['unique_key'] = md5( microtime().rand() );
WC()->session->set( 'custom_data', $_POST['grams_quantity'] );
}
return $cart_item_data;
}
// Update product price by grams in cart and checkout
add_filter( 'woocommerce_before_calculate_totals', 'update_prices_by_gram', 10, 1 );
function update_prices_by_gram( $cart_object ) {
// HERE Define the special product IDs sold by grams
$targeted_product_ids = array(37);
// or HERE Define a product categories (ids, slugs or names)
$categories = array('sold-by-gram');
foreach ( $cart_object->get_cart() as $cart_item ) {
// Only for products sold by gram
$product_id = $cart_item['product_id'];
if ( in_array( $product_id, $targeted_product_ids ) || has_term( $categories, 'product_cat', $product_id ) ){
// Get an instance of the WC_Product object and the
$product = $cart_item['data'];
$grams = $cart_item['grams_quantity'];
// Method is_on_sale() manage everything (dates…)
$product->set_price( $product->get_price() * $grams);
}
}
}
// Render "grams_quantity" the custom product field in cart and checkout
add_filter( 'woocommerce_get_item_data', 'render_product_custom_field_meta_on_cart_and_checkout', 10, 2 );
function render_product_custom_field_meta_on_cart_and_checkout( $cart_data, $cart_item ) {
$custom_items = array();
if( !empty( $cart_data ) )
$custom_items = $cart_data;
if( isset( $cart_item['grams_quantity'] ) )
$custom_items[] = array(
'name' => __( 'Grams', 'woocommerce' ),
'value' => sanitize_text_field( $cart_item['grams_quantity'] ),
'display' => sanitize_text_field( $cart_item['grams_quantity'] ),
);
return $custom_items;
}
// Save "grams_quantity" to the order items meta data
add_action('woocommerce_add_order_item_meta','add_product_custom_fiel_to_order_item_meta', 1, 3 );
function add_product_custom_fiel_to_order_item_meta( $item_id, $item_values, $item_key ) {
if( isset( $item_values['grams_quantity'] ) )
wc_update_order_item_meta( $item_id, 'Grams', sanitize_text_field( $item_values['grams_quantity'] ) );
}
Code goes in function.php file of your active child theme (or active theme) or in any plugin file.
Tested and works.

Wordpress variable, user created, woocommerce product

Basically I am interested in using woocommerce to sell a product . This product is a Print Order of a external printing service that I have implemented in a brand new plugin.
What I want now is after the order, is to be able to put that "order" in the buy cart, and buy it normally as just another woocommerce product.
The product has to be created on the fly, manually by a way of some function that I can use to create a product during a certain workflow point.
Can you help me to find a solution?
Using woocommerce or not!
What i understand from your requirement/comments is that you want to be able to dynamically create a product, which is bad idea. But here is my suggestion.
Lets say you have a simple product called "Print Job" with a price of $1 and with an id of 10. And i am supposing that your external printing service would send back a response with the order and the price of printing, lets say $64.
Once you recieve the response you can simply call add_product_to_cart(10), this will automatically add the print job product to your cart.
/************* functions.php ***************/
function add_product_to_cart($product_id) {
if ( ! is_admin() ) {
$found = false;
//check if product already in cart
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->id == $product_id )
$found = true;
}
// if product not found, add it
if ( ! $found )
WC()->cart->add_to_cart( $product_id );
} else {
// if no products in cart, add it
WC()->cart->add_to_cart( $product_id );
}
}
}
You also need to add this function in your functions.php, this function will override the price of the product i.e "Print Job" with the actual price of $64.
/******************* Functions.php ****************/
add_action( 'woocommerce_before_calculate_totals', 'override_printing_price' );
function override_printing_price( $cart_object ) {
$custom_price = 64;
foreach ( $cart_object->cart_contents as $key => $value ) {
$value['data']->price = $custom_price;
}
}

Prevent WooCommerce brand from being sellable

Hi there we have an online store running on WooCommerce and using the WooCommerce brands plugin (http://docs.woothemes.com/document/wc-brands/) but there is one brand that we are allowed show online with price but not allowed to actually sell.
Is there a function I can add for this particular brand to functions.php that will change the add to cart button in category or widget layout to "more info" and link to the product and then on the product page instead of the add to cart section just have a text message saying to call the store.
You can filter WooCommerce's is_purchasable method. Any item that returns false will not be able to be purchased.
function so_26378581_purchasable( $purchasable, $product ){
if ( has_term( 'your-brand', 'product_brand', $product->id ) ){
$purchasable = false;
}
return $purchasable;
}
add_action( 'woocommerce_is_purchasable', 'so_26378581_purchasable', 10, 2 );
This uses conditional logic to test whether the $product in question has the your-brand term in the product_brand taxonomy... via the has_term() function.
By the way, this is not the type of functionality that belongs in functions.php. Your theme should only be concerned with display/appearances. I would recommend that you make this its own plugin, or add to to a site specific snippets plugin.
to me it made no error message, but it did not work, as applied in my test subject and stand continues with two products of different brands.
I put this code:
function so_26378581_purchasable( $purchasable, $product ){
if ( has_term( 'product_brand', 'product_brand', $product->id ) ){
$purchasable = false;
}
return $purchasable;
}
add_action( 'woocommerce_is_purchasable', 'so_26378581_purchasable', 10, 2 );

Resources