skip to Main Content

Im currently using a plugin to prepend the word "from" before some of my onsite prices, but im trying to hardcode this so i can remove the plugin as it slows down my site. I feel like im close, but ive looked everywhere and cant seem to find how to prepend an advanced custom field on the price field.

The code im using in my functions is

/**
 * Display before_price custom field on frontend test (kinda working)
 * @since 1.0.0
 */
add_action( 'woocommerce_single_product_summary', 'shoptimizer_before_price', 10 );
function shoptimizer_before_price($price) { ?>
 
<?php if(get_field('before_price')) { ?>
    <div class="cg-before_price"><?php the_field('before_price'); ?></div>
<?php }
}

but as you can see on this page, its displayed after the price instead of before. any pointers please team?

https://www.subscriptionboxaustralia.com/product/beauty-subscription-boxes/boxy-charm-storks/

Thanks

2

Answers


  1. PHP Snippet: Add Prefix to WooCommerce Prices

    add_filter( 'woocommerce_get_price_html', 'bbloomer_add_price_prefix', 99, 2 );
      
    function bbloomer_add_price_prefix( $price, $product ){
        $price = 'Prefix here ' . $price;
        return $price;
    }
    

    Reference: https://www.businessbloomer.com/woocommerce-add-prefix-suffix-product-prices/

    Login or Signup to reply.
  2. I had a similar problem. Maybe this will help:

    /**
     * Original code by https://bloginbox.com
     * Create and show prefix and suffix fields in product settings
     */
    
    function wcps_create_custom_field() {
        $args = array(
        'id' => 'custom_prefix_for_price',
        'label' => __( 'Prefix for price', 'wc-prefix-suffix' ),
        'class' => 'prefix-custom-field',
        'desc_tip' => true,
        'description' => __( 'Add price prefix here', 'wc-prefix-suffix' ),
        );
        woocommerce_wp_text_input( $args );
        
        $args = array(
        'id' => 'custom_suffix_for_price',
        'label' => __( 'Suffix for price', 'wc-prefix-suffix' ),
        'class' => 'suffix-custom-field',
        'desc_tip' => true,
        'description' => __( 'Add price suffix here.', 'wc-prefix-suffix' ),
        );
        woocommerce_wp_text_input( $args );
    }
    
    add_action( 'woocommerce_product_options_general_product_data', 'wcps_create_custom_field' );
    
    /**
     * Save custom fields to database.
     */
    
    function wcps_save_custom_field( $post_id ) {
     $product = wc_get_product( $post_id );
     $prefix = isset( $_POST['custom_prefix_for_price'] ) ? $_POST['custom_prefix_for_price'] : '';
     $suffix = isset( $_POST['custom_suffix_for_price']) ? $_POST['custom_suffix_for_price'] : '';
     $product->update_meta_data( 'custom_prefix_for_price', sanitize_text_field( $prefix ) );
     $product->update_meta_data( 'custom_suffix_for_price', sanitize_text_field( $suffix ) ); 
     $product->save();
    }
    add_action( 'woocommerce_process_product_meta', 'wcps_save_custom_field' );
    
    /**
     * Connect to filter and add prefix and suffix to price
     */
    
    add_filter( 'woocommerce_get_price_html', 'wcps_add_price_suffix', 99, 4 );
    function wcps_add_price_suffix( $price, $product ){
        
        global $post;
         $product = wc_get_product( $post->ID );
         $prefix = $product->get_meta( 'custom_prefix_for_price' );
         $suffix = $product->get_meta( 'custom_suffix_for_price' );
     // Check if product has prefix or suffix before adding it to price
         if( $prefix || $suffix )  {
            $price = $prefix . ' ' . $price . ' ' . $suffix;
            return $price; // Return price with prefix and suffix
        }
        else {
            return $price; // If no prefix or suffix is found we will return just the price.
        }
    }
    

    Reference: https://bloginbox.com/how-to-add-prefix-and-suffix-to-woocommerce-prices/

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search