Programmatically Hide Woocommerce Product - wordpress

I want to conditionally hide a group of Woocommerce products on a category page depending on the current shopping cart contents. I have a category called boxes with four products. Two of them are also in the cardboard category and two are in the plastic category.
If product with ID 23 is in the cart already, I want to show plastic boxes. If it isn't, I want to hide them. I know how to check the cart contents, but once I have that answer, how do I hide products from the plastic category from that page?
add_action( 'woocommerce_before_shop_loop', 'my_before_shop_loop' );
function my_before_shop_loop() {
global $woocommerce;
$flag = 0;
foreach($woocommerce->cart->get_cart() as $key => $val ) {
$_product = $val['data'];
if ($_product->id == '23') {
$flag = 1;
}
}
if ($flag == 0) {
// hide products that are in the plastic category
// this is where I need help
}
}

The hook which you are using right now, is triggerred after products are fetch from database. You can filter the products from the query itself. In below code, you can pass products which you want to hide on front end.
function custom_pre_get_posts_query( $q ) {
// Do your cart logic here
// Get ids of products which you want to hide
$array_of_product_id = array();
$q->set( 'post__not_in', $array_of_product_id );
}
add_action( 'woocommerce_product_query', 'custom_pre_get_posts_query' );

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.

Auto-complete Paid Orders only for specific product IDs on Woocommerce

Is there any way I can auto complete orders only for specific product IDs on Woocommerce?
I used the code on this thread to auto complete orders.
I also read this thread but it excludes product ids from auto complete. And I am not able to make it work the other way around.
Since I have 20+ products in my shop and I want to use auto complete on only 2 of them, it would be great if I can specify the order ids which I want to auto complete.
Here is a way to autocomplete paid orders for specific product IDS:
add_action( 'woocommerce_payment_complete_order_status', 'wc_auto_complete_paid_order', 10, 3 );
function wc_auto_complete_paid_order( $status, $order_id, $order ) {
// Below the targeted product Ids
$targeted_ids = array(37, 53);
// Loop through order line items
foreach( $order->get_items() as $item ) {
if ( in_array( $item->get_product_id(), $targeted_ids ) || in_array( $item->get_variation_id(), $targeted_ids ) ) {
return 'completed';
}
}
return $status;
}
Code goes in functions.php file of the active child theme (or active theme).
WooCommerce: Auto complete paid orders
Exclude specific products on auto-complete orders process in Woocommerce

Woocommerce Applying Coupon on Specific product in Cart

I want to apply the discount coupon to the specific product in cart from checkout page.
Inside the cart:
product1 w/ upgrades
product2 w/o upgrades
when I apply the discount coupon I want that the discount will take effect only in product2's price. I tried to search in some forums but the coupon will take effect in the whole cart items.
You can try this piece of code:
function calculate_variation_addon_price( $cart_object ) {
global $isProcessed;
// Loop for all products in cart
foreach ( $cart_object->cart_contents as $key => $value ) {
// Your condition here for product specific i.e. id == 25
if((!isset($isProcessed) || empty($isProcessed))) {
$orgPrice = floatval( $value['data']->get_price() );
$cart_object->cart_contents[$key]['data']->set_price( $orgPrice * 0.5 );
}
}
$isProcessed = 1;
}
add_action( 'woocommerce_before_calculate_totals', 'calculate_variation_addon_price', 2, 1 );
While adding coupon from admin panel, find a tab there, called 'Usage Restriction'.
There you can add products or product categories or even exclude some for the coupon code to be applied.
You can so that using product id, sku or name
function filter_woocommerce_coupon_get_discount_amount( $discounting_amount, $price_to_discount , $cart_item, $single, $coupon ) {
// On backorder
if ( $cart_item['data']->get_id() !== 'product2_id' ) {
$discounting_amount = 0;
}
return $discounting_amount;
}
add_filter( 'woocommerce_coupon_get_discount_amount', 'filter_woocommerce_coupon_get_discount_amount', 10, 5 );
You can use in_array instead of !== for multiple products
You also replace get_id() by other properties or use custom attributs...

How do I disable shipping in some of my woocommerce products

I have a WooCommerce online shop that offers shipping to most products. Some of the products are for local pickup. I've tried setting a class on shipping zones with cost equal to zero and assigning the class on the products. But so far, the checkout still displays the shipping cost. Is there any way where some products will not have a shipping cost?
If you are searching for plugin solution, try WooCommerce Conditional Shipping and Payments. By using this plugin, you could add restrictions on certain product or product categories.
You've might want to look into the woocommerce_package_rates filter, which allows you to filter the set of shipping options that are available to the customer. An example would be something like this:
<?php
// add this snippet to functions.php:
add_filter( 'woocommerce_package_rates', function ( $rates, $package ) {
// examine $package for products. this could be a whitelist of specific
// products that you wish to be treated in a special manner...
$special_ids = array( 1, 2, 3, 4, 5 );
$special_product_present = false;
foreach ( $package['contents'] as $line_item ) {
if ( in_array( $line_item['product_id'], $special_ids ) ) {
$special_product_present = true;
}
}
$rates = array_filter( $rates, function ( $r ) use ( $special_product_present ) {
// do some logic here to return true (for rates that you wish to be displayed), or false.
// example: only allow shipping methods that start with "local"
if ( $special_product_present ) {
return preg_match( '/^local/', strtolower( $r->label ) );
} else {
return true;
}
} );
return $rates;
}, 10, 2 );
This blog post here shows some variations on that idea using this hook, including how to customize the available rates based on shopping cart value, customer's country, number of items in the cart, etc. And here's the source code: https://github.com/woocommerce/woocommerce/blob/v2.2.3/includes/class-wc-shipping.php#L366

Woocommerce customize the product name in the cart and order pages

Actually, I already got hook to customize the price in the cart page and all other page.
This is the hook to customize the product price
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );
function add_custom_price( $cart_object ) {
$custom_price = 10; // This will be your custome price
foreach ( $cart_object->cart_contents as $key => $value ) {
$value['data']->price = $custom_price;
}
}
Actually, I am using woocommerce plugin. I want a hook to customize the product name displayed in the cart page and all other pages next to cart page.
I want to customize the Product name , i want to add the some static attributes to product name displayed in the cart page, order page ,order details page and all the pages next to cart page.
Thanks in advance
I wanted to share this as I managed to figure out something for my needs. (I only ever have a single item in the order as I've customised WooCommerce for holiday bookings.)
<?php
add_action( 'woocommerce_product_title', 'add_custom_name' );
function add_custom_name( $title ) {
return 'test name';
}
?>
Hopefully someone can elaborate on this further or take what has been done with the custom price code an properly re-purpose it for product names.

Resources