Remove hidden product from pdf
-
I’ve setup my cart so that fi someone buys a specific product an additional product is added. I don’t however want this to show on the invoice/packing slip.
Is there some way to remove a product from those so the customer will not see it? The product has a lot of variations (appx 200) which I would also want to hide.
-
Hi @muttly,
This depends on how you are adding these “chained” products to your cart. Are you using a third party plugin for this? If so, which one?
I’m doing it via code. I am however using these 2 code snippets to hide them from the cart, cart count and emails. The customer doesn’t know about the product. I can include my code for adding the items if you want. But ideally just not including a hidden product would work well. It’s the only hidden product I have on the site and it’s use is solely for stock keeping.
https://businessbloomer.com/woocommerce-hide-hidden-products-cart-order-emails/
https://businessbloomer.com/woocommerce-exclude-hidden-products-from-mini-cart-counter/
This is the code I use to add the products to the cart….
/**
* Automatically adding the product to the cart.
*/add_action(‘template_redirect’, ‘pt_add_wallet_to_cart’);
function pt_add_wallet_to_cart(){// $item_key, $product_id ) {
$product_category_id = 508; // ticket category id
$free_product_id = 37913; // Ticket Wallet Stock product idif( is_cart() || is_checkout() ) {
//Remove all variations <–
foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
$p_id = $cart_item[‘product_id’];
$v_id = $cart_item[‘variation_id’];
if($p_id==$free_product_id){
if ($cart_item[‘variation_id’] == $v_id) {
//remove single product
WC()->cart->remove_cart_item($cart_item_key);
}
}
}
//–> Remove all varations//Add variations <–
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item){ // Loop through all products
$p_id = $cart_item[‘product_id’];
$terms = get_the_terms( $p_id, ‘product_cat’ );
$cat_found=false;
foreach ( $terms as $term ) { // Loop through all categories
$cat_id = $term->term_id;
if($cat_id==$product_category_id){ // If find product_category_id then break out
$cat_found=true;
break;
}
}
$quantity = $cart_item[‘quantity’]; // Get quantity of this item
$pa_tbs = $cart_item[‘variation’][‘attribute_pa_top-bar-style’]; // Get top bar style$pa_c = $cart_item[‘variation’][‘attribute_pa_colour’]; // Get top bar colour
$pa_c=str_replace(‘-nc’,”,$pa_c); // Remove -nc from colour attribute$pa_ht = $cart_item[‘variation’][‘attribute_pa_home-team’]; // Get home team
$pa_w = $cart_item[‘variation’][‘attribute_pa_wallet’]; // Get walletif($pa_tbs==”){
$pa_tbs=’youre-going-to’;
}
if($pa_c==”){
$pa_c=’white’;
}$taxonomy = ‘pa_top-bar-style’; // Get name rather than slug
$t = get_term_by(‘slug’, $pa_tbs, $taxonomy);
$pa_tbs_fv = $t->name;$taxonomy = ‘pa_colour’; // Get name rather than slug
$t = get_term_by(‘slug’, $pa_c, $taxonomy);
$pa_c_fv = $t->name;$free_variation=array( // Create array of names of attributes
‘Top Bar Style’ => $pa_tbs_fv,
‘Colour’ => $pa_c_fv,
);
$vx=get_variation_id_from_attributes($free_product_id,$pa_tbs,$pa_c); // Retrieve variation id
//echo ‘<br><br>cat_found: ‘.$cat_found.'<br>cat_id: ‘.$cat_id.'<br>p_id: ‘.$p_id.'<br>tbs: ‘.$pa_tbs.'<br>tbs_fv: ‘.$pa_tbs_fv.'<br>c: ‘.$pa_c.'<br>c_fv: ‘.$pa_c_fv.'<br>w: ‘.$pa_w.'<br>ht: ‘.$pa_ht.'<br>vx: ‘.$vx;
if($pa_w == ‘included’){ // If wallet is ‘included’ add product to cart
WC()->cart->add_to_cart($free_product_id, $quantity, $vx, $free_variation);
}
}
//–> Add variations
}
}function get_variation_id_from_attributes( $product_id, $topbarstyle, $colour ) {
$topbarstyle = strtolower($topbarstyle);
$colour = strtolower($colour);$variation_id = find_matching_product_variation_id ( $product_id, array(
‘attribute_pa_top-bar-style’ => $topbarstyle,
‘attribute_pa_colour’ => $colour
));return $variation_id;
}function find_matching_product_variation_id($product_id, $attributes){
return (new \WC_Product_Data_Store_CPT())->find_matching_product_variation(
new \WC_Product($product_id),
$attributes
);
}Hi @muttly,
I see. You can remove hidden products from your documents with the following code snippet:
add_filter( 'wpo_wcpdf_order_items_data', 'wpo_wcpdf_hide_hidden_products', 10, 3 ); function wpo_wcpdf_hide_hidden_products ( $items, $order, $document_type ) { foreach ($items as $item_id => $item) { if ( $product = wc_get_product( $item['product_id'] ) ) { if ( $product->get_catalog_visibility() == 'hidden' ) { unset($items[$item_id]); } } } return $items; }
Please note that when a hidden product gets removed from the catalogue the item will still be in your order(s). But since there now is no more product to check for visibility, the items connected to the removed hidden product will reappear when viewing your documents.
A solution for this could be in the next update of our Professional extension. In that update will be a feature that will let you store a copy of your PDFs on your server the first time they are generated. Each time a document is requested this copy will be loaded. So no matter how the order changes (or if products get removed) this won’t affect the PDF.
That is perfect, thank you very much for your help!!
- The topic ‘Remove hidden product from pdf’ is closed to new replies.