skip to Main Content

I am trying to get woocommerce sale price meta field from the plugin woocommerce price based on country and then copy it to another custom field. The code seems to create an error? Am I getting the price in the wrong way?

if( ( $value = $product->get_meta( 'canada_sale_price' ) && ! $product->get_meta( 'customfieldprice' ) ){
    $product->update_meta_data( 'customfieldprice', $value );
}

I tried also using the native regular and sale price from WooCommerce but no success…

if( ( $value = $product->get_price() || $value = $product->get_regular_price() ) ) { update_post_meta( $product_id, 'customfieldprice', $value ); }

2

Answers


  1. Chosen as BEST ANSWER

    I have modified my code with recommendations above and it works well now.

    For anyone looking for the solution, here is the code:

    /**
     * Copy WooCommerce product sales price to custom meta field.
     * When a product is created or updated in the backend.
     *
     * @param int $post_id Post ID.
     *
     * @return void
     */
    function vh_wc_copy_product_sales_price_into_custom_field( $post_id ) {
        // Check & Validate the woocommerce meta nonce.
        if ( ! ( isset( $_POST['woocommerce_meta_nonce'] ) || wp_verify_nonce( sanitize_key( $_POST['woocommerce_meta_nonce'] ), 'woocommerce_save_data' ) ) ) {
            return;
        }
    
        $sale_price = isset( $_POST['_price'] ) ? wc_clean( wp_unslash( $_POST['_price'] ) ) : null;
        
        update_post_meta( $post_id, 'codisto_product_price_cad', wc_format_decimal( $sale_price ) );
    }
    add_action( 'woocommerce_process_product_meta', 'vh_wc_copy_product_sales_price_into_custom_field', 99 );
    
    add_action( 'save_post', 'add_value_on_update_product', 10, 3);
    
    function add_value_on_update_product($post_id, $post, $update){
        if ($post->post_status != 'publish' || $post->post_type != 'product') {
            return;
        }
        
        // Check & Validate the woocommerce meta nonce.
        
        if ( ! ( isset( $_POST['woocommerce_meta_nonce'] ) || wp_verify_nonce( sanitize_key( $_POST['woocommerce_meta_nonce'] ), 'woocommerce_save_data' ) ) ) {
            
            return;
        }
    
        if (!$product = wc_get_product( $post )) {
            return;
        }
        
        $sale_price = isset( $_POST['_canada_sale_price'] ) ? wc_clean( wp_unslash( $_POST['_canada_sale_price'] ) ) : null; 
        update_post_meta( $post_id, 'codisto_product_price_cad', wc_format_decimal( $sale_price ) ); 
       
    }
    

  2. You can use woocommerce_process_product_meta action to perform your copy process and can get the sale price from $_POST and validate and format it then you can store it in your custom meta key.

    NOTE: I have used your_custom_field_meta_key in the example, you can change it to your desired key name.

    /**
     * Copy WooCommerce product sales price to custom meta field.
     * When a product is created or updated in the backend.
     *
     * @param int $post_id Post ID.
     *
     * @return void
     */
    function vh_wc_copy_product_sales_price_into_custom_field( $post_id ) {
        // Check & Validate the woocommerce meta nonce.
        if ( ! ( isset( $_POST['woocommerce_meta_nonce'] ) || wp_verify_nonce( sanitize_key( $_POST['woocommerce_meta_nonce'] ), 'woocommerce_save_data' ) ) ) {
            return;
        }
    
        $sale_price = isset( $_POST['_sale_price'] ) ? wc_clean( wp_unslash( $_POST['_sale_price'] ) ) : null;
    
        update_post_meta( $post_id, 'your_custom_field_meta_key', wc_format_decimal( $sale_price ) );
    }
    add_action( 'woocommerce_process_product_meta', 'vh_wc_copy_product_sales_price_into_custom_field', 99 );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search