Add characters before and after product prices in WooCommerce – Learn WordPress from a to z

Tutorials 0 lượt xem

There are times when you want to add special characters before or after the product price. These content are often stimulating words, capable of attracting customers to buy right away. Or sometimes it caters to a certain incentive program.

Add characters before and after the product price

To be able to add characters to the price of a product, you can use the following code to add it to the function.php file .

/*Add metabox to product*/
add_action( 'woocommerce_product_options_general_product_data', 'hocwp_presuffix_products' );
function hocwp_presuffix_products() {
//Add metabox prefix to product
woocommerce_wp_text_input( array(
'id' => '_product_prefix',
'label' => 'Prefix',
'description' => 'Add prefix to price. Leave blank to default.',
'desc_tip' => 'true',
) );
//Add metabox suffix to product
woocommerce_wp_text_input( array(
'id' => '_product_suffix',
'label' => 'Suffix',
'description' => 'Add suffix to price. Leave blank to default.',
'desc_tip' => 'true',
) );
}

/*Save metabox prefix and suffix*/
add_action( 'woocommerce_process_product_meta', 'hocwp_presuffix_products_save' );
function hocwp_presuffix_products_save( $post_id ) {
if ( ! empty( $_POST['_product_prefix'] ) ) {
update_post_meta( $post_id, '_product_prefix', esc_attr( $_POST['_product_prefix'] ) );
}
if ( ! empty( $_POST['_product_suffix'] ) ) {
update_post_meta( $post_id, '_product_suffix', esc_attr( $_POST['_product_suffix'] ) );
}
}

/*Add to price html*/
add_filter( 'woocommerce_get_price_html', 'bbloomer_price_prefix_suffix', 100, 2 );
function bbloomer_price_prefix_suffix( $price, $product ){
$prefix = get_option( 'hocwp_woocommerce_price_prefix');
$suffix = get_option( 'hocwp_woocommerce_price_suffix');

$prefix_product = get_post_meta($product->get_ID(), '_product_prefix', true);
$suffix_product = get_post_meta($product->get_ID(), '_product_suffix', true);

if($prefix_product) $prefix = $prefix_product;
if($suffix_product) $suffix = $suffix_product;

$prefix = ($prefix)?'<span class="hocwp_woocommerce_price_prefix">'.$prefix.'</span>':'';
$suffix = ($suffix)?'<span class="hocwp_woocommerce_price_suffix">'.$suffix.'</span>':'';

$price = $prefix.$price.$suffix;
return apply_filters( 'woocommerce_get_price', $price );
}
<div>
<span>1</span><span>2</span><span>3</span><span>4</span><span>5</span><span>6</span><span>7</span><span>8</span><span>9</span><span>10</span><span>11</span><span>12</span><span>13</span><span>14</span><span>15</span><span>16</span><span>17</span><span>18</span><span>19</span><span>20</span><span>21</span><span>22</span><span>23</span><span>24</span><span>25</span><span>26</span><span>27</span><span>28</span><span>29</span><span>30</span><span>31</span><span>32</span><span>33</span><span>34</span><span>35</span><span>36</span><span>37</span><span>38</span><span>39</span><span>40</span><span>41</span><span>42</span><span>43</span><span>44</span><span>45</span><span>46</span><span>47</span><span>48</span>
</div>

The special feature of this code is that it will add two input fields  Prefix and Suffix  to the product data. So you can choose which products have content added and which don’t, and can also use different content.

And this is the result of the above code when displayed outside the website.

summary

Adding characters before and after the price of the product is one of the ways to stimulate customers to take action to buy the product. We can do this quite easily with php code. Good luck.

Bài viết liên quan