Advertisement
Synagila

Workaround for WooCommerce Vendors Pro store shipping fields

Jun 24th, 2016
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.68 KB | None | 0 0
  1. /**
  2.  * Fix issues with inconsistent store shipping address meta fields in WCV Pro
  3.  * Using the _wcv_shipping meta field array only
  4.  */
  5. add_filter( 'get_user_metadata', function( $value, $object_id, $meta_key, $single ) {
  6.  
  7.   // Get values from _wcv_shipping shipping_address array
  8.   if ( preg_match ( '/^_wcv_shipping_(.+)$/', $meta_key, $matches ) && in_array( $matches[ 1 ], array( 'from', 'address1', 'address2', 'city', 'state', 'country', 'postcode' ) ) ) {
  9.  
  10.     $shipping_data = get_user_meta($object_id, '_wcv_shipping', true);
  11.  
  12.     if ($matches[ 1 ] == 'from' )
  13.       return array( @$shipping_data[ 'shipping_from' ] );
  14.     else
  15.       return array( @$shipping_data[ 'shipping_address' ][ $matches[ 1 ] ] );
  16.   }
  17.  
  18.   return $value;
  19. }, 10, 4 );
  20.  
  21. add_action( 'update_user_meta', function( $meta_id, $object_id, $meta_key, $_meta_value ) {
  22.  
  23.   // Store values in _wcv_shipping array and delete actual meta field value
  24.   if ( preg_match ( '/^_wcv_shipping_(.+)$/', $meta_key, $matches ) && in_array( $matches[ 1 ], array( 'from', 'address1', 'address2', 'city', 'state', 'country', 'postcode' ) ) ) {
  25.  
  26.     $shipping_data = get_user_meta($object_id, '_wcv_shipping', true);
  27.  
  28.     if ( empty( $shipping_data ) )
  29.       $shipping_data = array();
  30.  
  31.     if ($matches[ 1 ] == 'from' )
  32.       $shipping_data[ 'shipping_from' ] = $_meta_value;
  33.     else
  34.       $shipping_data[ 'shipping_address' ][ $matches[ 1 ] ] = $_meta_value;
  35.  
  36.     update_user_meta( $object_id, '_wcv_shipping', $shipping_data);
  37.     delete_user_meta( $object_id, $meta_key );
  38.   }
  39. }, 10, 4 );
  40.  
  41. add_filter( 'wcv_custom_user_fields', function ( $fields ) {
  42.  
  43.   // Fix "store state"
  44.  
  45.   $store_address = array();
  46.   foreach( $fields[ 'store_address' ][ 'fields' ] as $k => $v ) {
  47.  
  48.     if ( $k == '_wcv_shipping_state' )
  49.       $k = '_wcv_store_state';
  50.  
  51.     $store_address[ $k ] = $v;
  52.   }
  53.   $fields[ 'store_address' ][ 'fields' ] = $store_address;
  54.  
  55.   // Add "Shipping from" before shipping address
  56.  
  57.   $fields2 = array();
  58.   foreach( $fields as $k => $v ) {
  59.  
  60.     if ($k == 'shipping_address')
  61.       $fields2[ 'shipping_options' ] = array(
  62.         'title' => __( "Shipping", 'wcvendors-pro' ),
  63.         'fields' => array(
  64.           '_wcv_shipping_from' => array(
  65.             'label'       => __( 'Shipping from', 'wcvendors-pro' ),
  66.             'description' => __( 'Where products will be shipped from.', 'wcvendors-pro' ),
  67.             //'class'       => 'js_field-country',
  68.             'type'        => 'select',
  69.             'options'     => array(
  70.               'store_address' => __( 'Store Address', 'wcvendors-pro' ),
  71.               'other'         => __( 'Store Shipping Address', 'wcvendors-pro' ),
  72.              )
  73.           ),
  74.         )
  75.       );
  76.  
  77.     $fields2[ $k ] = $v;
  78.   }
  79.  
  80.   unset ( $fields2[ 'shipping_address' ][ 'fields' ][ '_wcv_store_phone' ] );
  81.  
  82.   return $fields2;
  83.  
  84. } );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement