• Resolved frbaldutz

    (@frbaldutz)


    Hi to everybody, I want to add to my website a fulfillment latency.
    I’ve search for a plugin or for a script along all web and I found a script in https://www.businessbloomer.com that shown how to add checkbox in backend and front end. But I need a textbox so I’ve tired to modify it, but someone was wrong. Someone can help me?

    // 1. Add FULFILLMENT LATENCY (General tab)

    add_action( ‘woocommerce_product_options_general_product_data’, ‘bbloomer_add_badge_checkbox_to_products1’ );

    function bbloomer_add_badge_checkbox_to_products1() {
    woocommerce_wp_text_input( array(
    ‘id’ => ‘fulfillment_latency’,
    ‘placeholder’ => ‘Fulfillment Latency’,
    ‘label’ => __(‘Fulfillment Latency’, ‘woocommerce’),
    ‘desc_tip’ => ‘true’
    )
    );
    }

    // —————————————–
    // 2. Save checkbox via custom field

    add_action( ‘save_post’, ‘bbloomer_save_badge_checkbox_to_post_meta1’ );

    function bbloomer_save_badge_checkbox_to_post_meta1( $product_id ) {
    if ( defined( ‘DOING_AUTOSAVE’ ) && DOING_AUTOSAVE )
    return;
    if ( isset( $_POST[‘fulfillment_latency’] ) ) {
    update_post_meta( $product_id, ‘fulfillment_latency’, $_POST[‘fulfillment_latency’] );
    } else delete_post_meta( $product_id, ‘fulfillment_latency’ );
    }

    // —————————————–
    // 3. Display badge @ single product page if checkbox checked
    add_action( ‘woocommerce_before_add_to_cart_quantity’, ‘bbloomer_display_badge_if_checkbox’, 16 );

    function bbloomer_display_badge_if_checkbox() {
    global $product;
    if ( get_post_meta( $product->get_id(), ‘fulfillment_latency’, true ) ) {
    echo ‘
    <div class=”woocommerce-message”>Shipping latency:’ .get_field(‘fulfillment_latency’). ‘business days</div>’ ;

    ‘;
    }
    }

Viewing 6 replies - 1 through 6 (of 6 total)
  • Hello @frbaldutz ,

    Okay, you were almost there. I found two issues, one extra '; and the get_field() function was not working.

    You can replace the third section of your code with this to complete the code –

    // —————————————–
    // 3. Display badge @ single product page if checkbox checked
    add_action( 'woocommerce_before_add_to_cart_quantity', 'bbloomer_display_badge_if_checkbox', 16 );
    
    function bbloomer_display_badge_if_checkbox() {
    	global $product;
    	if ( get_post_meta( $product->get_id(), 'fulfillment_latency', true ) ) {
    
    	echo '<div class=”woocommerce-message”>Shipping latency: ' . get_post_meta( $product->get_id(), 'fulfillment_latency')[0] . ' business days</div>' ;
    	}
    }

    I hope this helps.

    Thank you ??

    Thread Starter frbaldutz

    (@frbaldutz)

    Hi @rur165, it doesn’t work.

    I’ve tried to replace all the third parts, but it doesn’t work.

    Hello @frbaldutz ,

    Can you please enable debug following this guideline Debugging in WordPress and let me know what error you see when you use this code?

    I have tested your code with the changes I suggested and it worked as expected.

    Thank you ??

    Thread Starter frbaldutz

    (@frbaldutz)

    Hello (@rur165), i’ve cancel of function.php and i’ve insert in a new one the script. It doesn’t give me an error, but nothing appear in back end.

    It should appear the FulFillment Latency text box in my general tab in back end, but nothing appear.

    Probably a make some mistake. But where?
    Following the script i used

    // 1. Add FULFILLMENT LATENCY (General tab)

    add_action( ‘woocommerce_product_options_general_product_data’, ‘bbloomer_add_badge_checkbox_to_products1’ );

    function bbloomer_add_badge_checkbox_to_products1() {
    woocommerce_wp_text_input( array(
    ‘id’ => ‘fulfillment_latency’,
    ‘placeholder’ => ‘Fulfillment Latency’,
    ‘label’ => __(‘Fulfillment Latency’, ‘woocommerce’),
    ‘desc_tip’ => ‘true’
    )
    );
    }

    // —————————————–
    // 2. Save checkbox via custom field

    add_action( ‘save_post’, ‘bbloomer_save_badge_checkbox_to_post_meta1’ );

    function bbloomer_save_badge_checkbox_to_post_meta1( $product_id ) {
    if ( defined( ‘DOING_AUTOSAVE’ ) && DOING_AUTOSAVE )
    return;
    if ( isset( $_POST[‘fulfillment_latency’] ) ) {
    update_post_meta( $product_id, ‘fulfillment_latency’, $_POST[‘fulfillment_latency’] );
    } else delete_post_meta( $product_id, ‘fulfillment_latency’ );
    }

    // —————————————–
    // 3. Display badge @ single product page if checkbox checked
    add_action( ‘woocommerce_before_add_to_cart_quantity’, ‘bbloomer_display_badge_if_checkbox’, 16 );

    function bbloomer_display_badge_if_checkbox() {
    global $product;
    if ( get_post_meta( $product->get_id(), ‘fulfillment_latency’, true ) ) {

    echo ‘<div class=”woocommerce-message”>Shipping latency: ‘ . get_post_meta( $product->get_id(), ‘fulfillment_latency’)[0] . ‘ business days</div>’ ;
    }
    }

    Hello @frbaldutz ,

    I have copied your code from here and tested it on my site. It worked as expected –
    Product edit form – https://prnt.sc/yfy1h8
    Product view – https://prnt.sc/yfy2ti

    I will recommend you to try this code with a different theme. Also, if you are using a caching plugin make sure to clear your cache after each of your changes.

    If are still unsure why this is not working, I will recommend consulting with an expert to check your implementation.

    Thank you ??

    Hi there,

    We’ve not heard back from you in a while, so I’m marking this thread as resolved.

    Hopefully, you were able to find a solution to your problem! If you have further questions, please feel free to open a new topic.

    Thank you ??

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Fulfillment latency in single product page’ is closed to new replies.