Tagged: 

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #1348819

    I know this is not about the enfold theme but I wish to makes sure that adding the below Custom Code will not cause theme conflicts.
    We are trying to enable the ability to add the word “New” to newly added products on the Woocommerce Shop / Enfold theme.
    Here is the code suggested;
    =======================================================================
    add_action( ‘woocommerce_before_shop_loop_item_title’, ‘quadlayers_new_product_badge’, 3 );

    function quadlayers_new_product_badge() {
    global $product;
    $newness_days = 10;
    $created = strtotime( $product->get_date_created() );
    if ( ( time() – ( 60 * 60 * 24 * $newness_days ) ) < $created ) {
    echo ‘<span class=”itsnew onsale”>’ . esc_html__( ‘New!’, ‘woocommerce’ ) . ‘</span>’;
    }
    }
    =======================================================================
    Do you see any problems or have a better suggestion from Enfold to add the “New” product icon?

    Thanks
    Avery

    • This topic was modified 2 years, 7 months ago by So Evolve.
    #1348847

    Hi Avery,

    I think the easiest way to check the code would be to actually add it to your site. If it should not work or cause problems, then remove it again. I don’t think that the code in question should clash with anything in the theme.

    Best regards,
    Rikard

    #1348855

    FYI – We ran a test on a small site we use in-house and the code worked perfectly without conflict (good advise from you about testing)

    Here is the full post with details for any other enfold users:

    2. Display New Products Programmatically

    Let’s say you want the products to be displayed in multiple places on your website. Then, it can be very tedious to open all the required sections of your website and add the block or the shortcode to them. Instead, you can just add a code to your theme files to display a “New” badge on the new products of your WooCommerce store.

    But before moving forward, please make sure to backup your website and create a child theme using one of the child theme plugins. This will ensure that all the changes that you make to the core theme files are not affected when you update the WordPress theme.
    2.1. Open the Theme Function File

    We will add the code snippet to the functions.php file of your theme. So, go to Theme > Theme File Editor from your WordPress dashboard after activating the child theme.

    Then, you will need to open the functions.php file from the theme files on the right side of your screen.

    theme file editor display new products in woocommerce
    2.2. Add the Code to the Theme Files

    After you open the theme functions file, scroll down to the bottom of the file and paste the following code snippet here.

    add_action( ‘woocommerce_before_shop_loop_item_title’, ‘quadlayers_new_product_badge’, 3 );

    function quadlayers_new_product_badge() {
    global $product;
    $newness_days = 10;
    $created = strtotime( $product->get_date_created() );
    if ( ( time() – ( 60 * 60 * 24 * $newness_days ) ) < $created ) {
    echo ‘<span class=”itsnew onsale”>’ . esc_html__( ‘New!’, ‘woocommerce’ ) . ‘</span>’;
    }
    }

    The above code will add the new badge to the products that have been added to your website in the last 10 days. You can adjust the number of days according to the needs of your website. They can be changed depending on the number of days that you consider your recently added products to be new.

    After adding the code, click on Update File.

    You will be able to see that the new badge is added to the new products of your WooCommerce store when you preview them. The quickest way to view them is from the shop page itself.

    #1348857

    Thanks as always for your great support on the enfold theme.
    Mark as closed ticket.

    Avery

    #1348893

    Hi,

    Thanks for the update, I’ll go ahead and close this thread for now then. Please open a new thread if you should have any further questions or problems.

    Best regards,
    Rikard

Viewing 5 posts - 1 through 5 (of 5 total)
  • The topic ‘Quick Help’ is closed to new replies.