Advertisement
gabetu

Voxel extract regular product prices

Apr 9th, 2025 (edited)
456
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.60 KB | None | 0 0
  1. // please do not make it public yet
  2.  
  3. function extract_and_save_product_amounts( $event ) {
  4.     $post = $event->post;
  5.  
  6.     // Ensure the post type is 'products'
  7.     if ( $post->post_type->get_key() !== 'products' ) {
  8.         return;
  9.     }
  10.  
  11.     $post_id = $post->get_id(); // Use get_id() for Voxel compatibility
  12.  
  13.     // Get the 'product' custom field.
  14.     $product_data = maybe_unserialize( get_post_meta( $post_id, 'product', true ) );
  15.  
  16.     // Debugging: Log the retrieved product data.
  17.     error_log( "Extract Function triggered for post ID: $post_id" );
  18.     error_log( "Raw product data: " . print_r( $product_data, true ) );
  19.  
  20.     // Check if the custom field exists and is not empty.
  21.     if ( ! empty( $product_data ) ) {
  22.         // Decode the JSON data.
  23.         $product_array = json_decode( $product_data, true );
  24.  
  25.         // Validate structure and extract amounts.
  26.         if ( is_array( $product_array ) && isset( $product_array['base_price']['amount'] ) ) {
  27.             // Convert and round amount
  28.             $amount = number_format(floatval($product_array['base_price']['amount']), 2, '.', '');
  29.  
  30.             // Update product_amount meta
  31.             delete_post_meta( $post_id, 'product_amount' );
  32.             update_post_meta( $post_id, 'product_amount', $amount );
  33.             error_log( "Updated product_amount to: $amount" );
  34.  
  35.             // Handle product_discount_amount
  36.             if ( isset($product_array['base_price']['discount_amount']) && $product_array['base_price']['discount_amount'] !== '' ) {
  37.                 $discount_amount = number_format(floatval($product_array['base_price']['discount_amount']), 2, '.', '');
  38.                 delete_post_meta( $post_id, 'product_discount_amount' );
  39.                 update_post_meta( $post_id, 'product_discount_amount', $discount_amount );
  40.                 error_log( "Updated product_discount_amount to: $discount_amount" );
  41.             } else {
  42.                 // Delete the meta field if discount_amount is missing or empty
  43.                 delete_post_meta( $post_id, 'product_discount_amount' );
  44.                 error_log( "Deleted product_discount_amount for post ID: $post_id" );
  45.             }
  46.  
  47.         } else {
  48.             error_log( "Invalid JSON structure or missing 'amount' for post ID: $post_id" );
  49.         }
  50.     } else {
  51.         error_log( "No product data found for post ID: $post_id" );
  52.     }
  53. }
  54.  
  55. // Hook into Voxel events
  56. add_action( 'voxel/app-events/post-types/products/post:submitted', 'extract_and_save_product_amounts' );
  57. add_action( 'voxel/app-events/post-types/products/post:updated', 'extract_and_save_product_amounts' );
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement