Advertisement
Guest User

Cart Product Feed Additional Product Fields

a guest
Jul 24th, 2016
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.95 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Cart Product Feed Additional Product Fields
  4. Plugin URI:
  5. Description: Plugin to add custom fields to products in WooCommerce
  6. Author: ShoppingCartProductFeed; Remi Corson & Harsha Venkatesh
  7. Version: 1.1.3
  8. Author URI:
  9. */
  10.  
  11. //Simple Product Hooks
  12. // Display Fields
  13. add_action( 'woocommerce_product_options_general_product_data', 'cpf_custom_general_fields' );
  14.  
  15. // Save Fields
  16. add_action( 'woocommerce_process_product_meta', 'cpf_save_custom_general_fields' );
  17.  
  18. //Variable Product Hooks
  19. //Display Fields (both actions below should work)
  20. add_action( 'woocommerce_product_after_variable_attributes', 'cpf_custom_variable_fields', 10, 3 );
  21. //add_action( 'woocommerce_variation_options', 'cpf_custom_variable_fields', 10, 3 );
  22.  
  23. //Save variation fields
  24. //add_action( 'woocommerce_process_product_meta_variable', 'cpf_save_custom_variable_fields', 10, 1 );
  25. //As of WooCommerce 2.4.4:
  26. //woocommerce_process_product_meta_variable no longer works, and it must be changed to woocommerce_save_product_variation
  27. add_action( 'woocommerce_save_product_variation', 'cpf_save_custom_variable_fields', 10, 1 );
  28.  
  29. function cpf_custom_general_fields()
  30. {  
  31.     global $woocommerce, $post;
  32.  
  33.     echo '<div id="cpf_attr" class="options_group">';
  34.     //ob_start();
  35.    
  36.     //Brand field
  37.     woocommerce_wp_text_input(
  38.         array( 
  39.             'id'          => '_cpf_brand',
  40.             'label'       => __( 'Brand', 'woocommerce' ),
  41.             'desc_tip'    => 'true',
  42.             //'type'        => 'text',
  43.             'value'       =>  get_post_meta( $post->ID, '_cpf_brand', true ),
  44.             'description' => __( 'Enter the product Brand here.', 'woocommerce' )              
  45.         )
  46.     );
  47.  
  48.     //  Valid field
  49.     woocommerce_wp_select(
  50.         array(
  51.             'id'          => '_cpf_valid',
  52.             'label'       => __( 'Valid', 'woocommerce' ),
  53.             'desc_tip'    => 'true',
  54.             'description' => __( 'Select False to exclude this entire product from the feed', 'woocommerce' ),
  55.             'value'       => get_post_meta( $post->ID, '_cpf_valid', true ),
  56.             'options'    => array(
  57.                 'true'   => __( 'True', 'woocommerce' ),
  58.                 'false'  => __( 'False', 'woocommerce' ),              
  59.             )
  60.         )
  61.     );
  62.  
  63.     echo '</div>';
  64.     echo '<div id="cpf_attr" class="options_group show_if_simple show_if_external">';
  65.     // woocommerce_wp_text_input
  66.     // (
  67.     //  array
  68.     //  (   'id'          => '_cpf_map',
  69.     //      'label'       => __( 'MAP', 'woocommerce' ),
  70.     //      'desc_tip'    => 'true',
  71.     //      'description' => __( 'Enter the supplier\'s MAP here.', 'woocommerce' ),
  72.     //      'type'        => 'number',
  73.     //       'custom_attributes' => array(
  74.     //                              'step'  => 'any',
  75.     //                              'min'   => '0'
  76.     //                          )
  77.     //  )
  78.     // );
  79.  
  80.     //MPN Field
  81.     woocommerce_wp_text_input(
  82.         array( 
  83.             'id'          => '_cpf_mpn',
  84.             'label'       => __( 'MPN', 'woocommerce' ),
  85.             'desc_tip'    => 'true',
  86.             'description' => __( 'Enter the manufacturer product number', 'woocommerce' ),
  87.         )
  88.     );
  89.    
  90.     //UPC Field
  91.     woocommerce_wp_text_input(
  92.         array( 
  93.             'id'          => '_cpf_upc',
  94.             'label'       => __( 'UPC', 'woocommerce' ),
  95.             'desc_tip'    => 'true',
  96.             'description' => __( 'Enter the product UPC here.', 'woocommerce' ),
  97.         )
  98.     );
  99.    
  100.     //UPC Field
  101.     woocommerce_wp_text_input(
  102.         array( 
  103.             'id'          => '_cpf_ean',
  104.             'label'       => __( 'EAN', 'woocommerce' ),
  105.             'desc_tip'    => 'true',
  106.             'description' => __( 'Enter the product EAN here.', 'woocommerce' ),
  107.         )
  108.     );
  109.  
  110.     // //Valid Field
  111.     // woocommerce_wp_select(
  112.     //      array(
  113.     //          'id'          => '_cpf_valid',
  114.     //          'label'       => __( 'Valid', 'woocommerce' ),
  115.     //          'desc_tip'    => 'true',
  116.     //          'description' => __( 'Select False to exclude this product/variation from the feed', 'woocommerce' ),
  117.     //          'options'     => array(
  118.     //              'true'    => __( 'True', 'woocommerce' ),
  119.     //              'false'   => __( 'False', 'woocommerce' ),             
  120.     //          )
  121.     //      )
  122.     // );
  123.    
  124.     // woocommerce_wp_text_input(
  125.     //  array( 
  126.     //      'id'          => '_cpf_valid',
  127.     //      'label'       => __( 'Valid', 'woocommerce' ),
  128.     //      'desc_tip'    => 'true',
  129.     //      'description' => __( 'Valid (True | False)', 'woocommerce' ),
  130.     //  )
  131.     // );
  132.  
  133.     // Cost Field
  134.     // woocommerce_wp_text_input
  135.     // (
  136.     //  array
  137.     //  (
  138.     //      'id'          => '_cpf_item_cost',
  139.     //      'label'       => __( 'Item Cost', 'woocommerce' ),
  140.     //      'desc_tip'    => 'true',
  141.     //      'description' => __( 'Enter the cost of this item.', 'woocommerce' ),
  142.     //      'type'        => 'number',
  143.     //              'custom_attributes' => array(
  144.     //                              'step'  => 'any',
  145.     //                              'min'   => '0'
  146.     //                          )
  147.     //  )
  148.     // );
  149.  
  150.     //Shipping Cost Field
  151.     // woocommerce_wp_text_input
  152.     // (
  153.     //  array
  154.     //  (
  155.     //      'id'          => '_cpf_shipping_cost',
  156.     //      'label'       => __( 'Shipping Cost', 'woocommerce' ),
  157.     //      'desc_tip'    => 'true',
  158.     //      'description' => __( 'Enter the cost of shipping charged to DN', 'woocommerce' ),
  159.     //      'type'        => 'number',
  160.     //      'custom_attributes' => array(
  161.     //                              'step'  => 'any',
  162.     //                              'min'   => '-1'
  163.     //                      )
  164.     //  )
  165.     // );
  166.  
  167.     // Notes about this prodcut
  168.     // woocommerce_wp_textarea_input
  169.     // (
  170.     //  array
  171.     //  (
  172.     //      'id'          => '_cpf_notes',
  173.     //      'label'       => __( 'Notes', 'woocommerce' ),
  174.     //      'placeholder' => '',
  175.     //      'description' => __( 'Enter any notes about this product.', 'woocommerce' ),
  176.     //  )
  177.     // );
  178.  
  179. //$additional_fields = ob_get_contents();
  180.    
  181.     //ob_end_clean();
  182.     echo '</div>'; 
  183. }
  184.  
  185. /**
  186.  * Save new fields for simple products
  187.  *
  188. */
  189. function cpf_save_custom_general_fields($post_id)
  190. {  
  191.     //$woocommerce_supplier_title   =   $_POST['_cpf_supplier_title'];
  192.     //$woocommerce_map          =   $_POST['_cpf_map'];
  193.     //$woocommerce_upc          =   $_POST['_cpf_upc'];
  194.     //$woocommerce_item_cost        =   $_POST['_cpf_item_cost'];
  195.     //$woocommerce_shipping_cost    =   $_POST['_cpf_shipping_cost'];
  196.     //$woocommerce_dropship_fee =   $_POST['_cpf_dropship_fee'];
  197.     //$woocommerce_notes            =   $_POST['_cpf_notes'];
  198.      
  199.     $woocommerce_brand = $_POST['_cpf_brand'];
  200.     $woocommerce_upc = $_POST['_cpf_upc'];   
  201.     $woocommerce_mpn = $_POST['_cpf_mpn'];
  202.     $woocommerce_ean = $_POST['_cpf_ean'];
  203.     $woocommerce_valid = $_POST['_cpf_valid'];
  204.  
  205.     if(isset($woocommerce_brand))
  206.         update_post_meta( $post_id, '_cpf_brand', esc_attr($woocommerce_brand));
  207.  
  208.     if(isset($woocommerce_mpn))
  209.         update_post_meta( $post_id, '_cpf_mpn', esc_attr($woocommerce_mpn));
  210.    
  211.     if(isset($woocommerce_upc))
  212.         update_post_meta( $post_id, '_cpf_upc', esc_attr($woocommerce_upc));
  213.  
  214.     if(isset($woocommerce_ean))
  215.         update_post_meta( $post_id, '_cpf_ean', esc_attr($woocommerce_ean));
  216.  
  217.     if(isset($woocommerce_valid))
  218.         update_post_meta( $post_id, '_cpf_valid', esc_attr($woocommerce_valid));
  219. }
  220.  
  221. /**
  222.  * Create new fields for variations
  223.  *
  224. */
  225. function cpf_custom_variable_fields( $loop, $variation_id, $variation ) {
  226.  
  227.         //Added <br>s to the labels to correct a spacing issue that put the labels on the wrong input boxes -2015-05:KH
  228.  
  229.         // Variation Brand field
  230.         woocommerce_wp_text_input(
  231.             array(
  232.                 'id'       => '_cpf_variable_brand['.$loop.']',
  233.                 'label'       => __( '<br>Brand', 'woocommerce' ),
  234.                 'placeholder' => 'Parent Brand',
  235.                 //'desc_tip'    => 'true',
  236.                 //'description' => __( 'Enter the product Brand here.', 'woocommerce' ),
  237.                 'value'       => get_post_meta($variation->ID, '_cpf_brand', true),
  238.                 'wrapper_class' => 'form-row-first',
  239.             )
  240.         );
  241.    
  242.         // Variation MPN field
  243.         woocommerce_wp_text_input(
  244.             array(
  245.                 'id'          => '_cpf_variable_mpn['.$loop.']',
  246.                 'label'       => __( '<br>MPN', 'woocommerce' ),
  247.                 'placeholder' => 'Manufacturer Product Number',
  248.                 //'desc_tip'    => 'true',
  249.                 //'description' => __( 'Enter the product UPC here.', 'woocommerce' ),
  250.                 'value'       => get_post_meta($variation->ID, '_cpf_mpn', true),
  251.                 'wrapper_class' => 'form-row-last',
  252.             )
  253.         );
  254.         // Variation UPC field
  255.         woocommerce_wp_text_input(
  256.             array(
  257.                 'id'          => '_cpf_variable_upc['.$loop.']',
  258.                 'label'       => __( '<br>UPC', 'woocommerce' ),
  259.                 'placeholder' => 'UPC',
  260.                 //'desc_tip'    => 'true',
  261.                 //'description' => __( 'Enter the product UPC here.', 'woocommerce' ),
  262.                 'value'       => get_post_meta($variation->ID, '_cpf_upc', true),
  263.                 'wrapper_class' => 'form-row-first',
  264.             )
  265.         );
  266.        
  267.         // Variation EAN field
  268.         woocommerce_wp_text_input(
  269.             array(
  270.                 'id'          => '_cpf_variable_ean['.$loop.']',
  271.                 'label'       => __( '<br>EAN', 'woocommerce' ),
  272.                 'placeholder' => 'EAN',
  273.                 //'desc_tip'    => 'true',
  274.                 //'description' => __( 'Enter the product EAN here.', 'woocommerce' ),
  275.                 'value'       => get_post_meta($variation->ID, '_cpf_ean', true),
  276.                 'wrapper_class' => 'form-row-last',
  277.             )
  278.         );
  279.  
  280.         //  Variation Valid field
  281.         woocommerce_wp_select(
  282.             array(
  283.                 'id'          => '_cpf_variable_valid['.$loop.']',
  284.                 'label'       => __( 'Valid<br>', 'woocommerce' ),
  285.                 'desc_tip'    => 'true',
  286.                 'description' => __( 'Select False to exclude this variation from the feed', 'woocommerce' ),
  287.                 'value'       => get_post_meta($variation->ID, '_cpf_valid', true),
  288.                 'wrapper_class' => 'form-row-first',
  289.                 'options'    => array(
  290.                     ''       => __( '', 'woocommerce'),
  291.                     'true'   => __( 'True', 'woocommerce' ),
  292.                     'false'  => __( 'False', 'woocommerce' ),              
  293.                 )
  294.             )
  295.         );
  296.    
  297.         //  Variation: Notes about this product
  298.         woocommerce_wp_textarea_input(
  299.             array(
  300.                 'id'          => '_cpf_variable_description['.$loop.']',
  301.                 'label'       => __( '<br>Description', 'woocommerce' ),
  302.                 //'placeholder' => '',
  303.                 'desc_tip'    => 'true',
  304.                 'wrapper_class' => 'form-row-full',
  305.                 'description' => __( 'Enter variant description (will override post-content)', 'woocommerce' ),
  306.                 'value'       => get_post_meta($variation->ID, '_cpf_description', true),
  307.             )
  308.         );
  309.        
  310.  
  311. }
  312.  
  313. /**
  314.  * Save new fields for variations
  315.  *
  316. */
  317. function cpf_save_custom_variable_fields( $post_id ) {
  318.    
  319.     if (isset( $_POST['variable_sku'] ) ) {
  320.  
  321.         $variable_sku          = $_POST['variable_sku'];
  322.         $variable_post_id      = $_POST['variable_post_id'];
  323.        
  324.         $max_loop = max( array_keys( $_POST['variable_post_id'] ) );
  325.  
  326.         for ( $i = 0; $i <= $max_loop; $i++ ) {
  327.  
  328.             if ( ! isset( $variable_post_id[ $i ] ) ) {
  329.               continue;
  330.             }
  331.  
  332.         // Brand Field
  333.         $_brand = $_POST['_cpf_variable_brand'];
  334.         //for ( $i = 0; $i < sizeof( $variable_sku ); $i++ ) :
  335.             $variation_id = (int) $variable_post_id[$i];
  336.             if ( isset( $_brand[$i] ) ) {
  337.                 update_post_meta( $variation_id, '_cpf_brand', stripslashes( $_brand[$i]));            
  338.             }
  339.         //endfor;
  340.        
  341.         // MPN Field
  342.         $_mpn = $_POST['_cpf_variable_mpn'];
  343.         //for ( $i = 0; $i < sizeof( $variable_sku ); $i++ ) :
  344.             $variation_id = (int) $variable_post_id[$i];
  345.             if ( isset( $_mpn[$i] ) ) {
  346.                 update_post_meta( $variation_id, '_cpf_mpn', stripslashes( $_mpn[$i]));
  347.             }
  348.         //endfor;
  349.        
  350.         // UPC Field
  351.         $_upc = $_POST['_cpf_variable_upc'];
  352.         //for ( $i = 0; $i < sizeof( $variable_sku ); $i++ ) :
  353.             $variation_id = (int) $variable_post_id[$i];
  354.             if ( isset( $_upc[$i] ) ) {
  355.                 update_post_meta( $variation_id, '_cpf_upc', stripslashes( $_upc[$i]));
  356.             }
  357.         //endfor;
  358.  
  359.         // EAN Field
  360.         $_ean = $_POST['_cpf_variable_ean'];
  361.         //for ( $i = 0; $i < sizeof( $variable_sku ); $i++ ) :
  362.             $variation_id = (int) $variable_post_id[$i];
  363.             if ( isset( $_ean[$i] ) ) {
  364.                 update_post_meta( $variation_id, '_cpf_ean', stripslashes( $_ean[$i]));
  365.             }
  366.  
  367.         // description Field
  368.         $_descr = $_POST['_cpf_variable_description'];
  369.         //for ( $i = 0; $i < sizeof( $variable_sku ); $i++ ) :
  370.             $variation_id = (int) $variable_post_id[$i];
  371.             if ( isset( $_descr[$i] ) ) {
  372.                 update_post_meta( $variation_id, '_cpf_description', stripslashes( $_descr[$i]));
  373.             }
  374.  
  375.         // Valid Field
  376.         $_valid = $_POST['_cpf_variable_valid'];
  377.         //for ( $i = 0; $i < sizeof( $variable_sku ); $i++ ) :
  378.             $variation_id = (int) $variable_post_id[$i];
  379.             if ( isset( $_valid[$i] ) ) {
  380.                 update_post_meta( $variation_id, '_cpf_valid', stripslashes( $_valid[$i]));
  381.                 //update_post_meta( $variation_id, '_cpf_valid', "aha");
  382.             }
  383.        
  384.         }
  385.        
  386.     }
  387. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement