woocommerce offtopic – how to update post meta before a product add to cart( including variation) and after product add to cart need to update post meta back to first

Question

I am using woocommerce deposit plugin. And we need to enable deposit only for backorder product. But the issue was when a deposit is enabled in variation product . For both variation that having stock > 0 and stock < 0 take the deposit amount. I need to make it only to take for backorder variation only. when asked on the support they said they do not support customization. but they say

There's not a hook specifically designed for that, but one way to go about doing that would be override at runtime the value of the _wc_deposits_enabled metadata. For example, using filter woocommerce_product_get__wc_deposit_enabled.

after that I have tried it

add_filter( 'woocommerce_product_get__wc_deposit_enabled', 'woo_custom_add_to_cart',10,2 );
    function woo_custom_add_to_cart() {
        
    
        $cart_items =  WC()->cart->get_cart();
        if($cart_items)
        {
         foreach( $cart_items as $cart_item ){
            
              
                $variation_id=$cart_item['variation_id'];
                
                $product_id=$cart_item['product_id'];
                
                if($variation_id)
                {
                
                $variation_obj = new WC_Product_variation($variation_id);
                $stock = $variation_obj->get_stock_quantity();
                
                $backordered = get_post_meta( $variation_id, '_backorders', true );  
                $data=get_post_meta($product_id,'_wc_deposit_enabled',true);
                //print_r($data.'tttt');
                 
                 
                 if(($backordered=="notify" || $backordered=="yes") &&  $stock > 0)
                 {
                     
                     update_post_meta( $product_id, '_wc_deposit_enabled','no');
                        
                        
                        //echo "backorder";
                        
                        
                        
                        
                        
                     
                        
                }else if(($backordered=="notify" || $backordered=="yes") && $stock <=0 ){
                    
                            update_post_meta( $product_id, '_wc_deposit_enabled','forced');
                            
                    }
                
                
                
                
             
             //add_filter( ' wc_deposits_cart_item_deposit_data', 'disable_deposite',10,2 );
             
         }
             
         }
     }
            
    
    
    }

is there any way that need to make product deposit post meta option to no when the product is in stock and deposite to allowed when stock < 0. is it can done by woocommerce_before_add_to_cart_button and woocommerce_after_add_to_cart_button

0
Amal 5 months 2021-06-02T07:22:57-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse