Advertisement
eappereira

Custom Data WooCommerce

Nov 20th, 2015
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.69 KB | None | 0 0
  1. //Adiciona os custom
  2. add_action('add_user_custom_data_options', 'custom_data_options_callback');
  3. add_action('nopriv_add_user_custom_data_options', 'custom_data_options_callback');
  4. function custom_data_options_callback()
  5. {
  6.       //Custom data - Via AJAX post
  7.       $product_id = $_POST['id']; //produto ID
  8.       $custom_data_values =  $_POST['user_data']; //Usa o custom via AJAX
  9.       session_start();
  10.       $_SESSION['custom_data'] = $custom_data_values;
  11.       die();
  12. }
  13.  
  14.  
  15. //Adiciona os custom no WOO
  16.  
  17. add_filter('woocommerce_add_cart_item_data','add_custom_item_data',1,2);
  18. if(!function_exists('add_item_data'))
  19. {
  20.     function add_custom_item_data($cart_item_data,$product_id)
  21.     {
  22.         global $woocommerce;
  23.         session_start();    
  24.         if (isset($_SESSION['custom_data'])) {
  25.             $option = $_SESSION['custom_data'];      
  26.             $new_value = array('custom_data_value' => $option);
  27.         }
  28.         if(empty($option))
  29.             return $cart_item_data;
  30.         else
  31.         {    
  32.             if(empty($cart_item_data))
  33.                 return $new_value;
  34.             else
  35.                 return array_merge($cart_item_data,$new_value);
  36.         }
  37.         unset($_SESSION['custom_data']);
  38.        
  39.     }
  40. }
  41.  
  42.  
  43. //Extrai os custom e passa para o carrinhoa
  44.  
  45. dd_filter('woocommerce_get_cart_item_from_session', 'get_cart_items_from_session', 1, 3 );
  46. if(!function_exists('get_cart_items_from_session'))
  47. {
  48.     function get_cart_items_from_session($item,$values,$key)
  49.     {
  50.         if (array_key_exists( 'custom_data_value', $values ) )
  51.         {
  52.         $item['custom_data_value'] = $values['custom_data_value'];
  53.         }      
  54.         return $item;
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement