0

My goal is in cart item i have to show one link with each product.

- on cart page - on checkout - on order summary in back-end

and when I click on link it open the link target website or file

right now I can save the custom title or text on each cart item now issue when i try to pass link tag it show nothing but text without link

I used this link to archive one goal to show text but now I need link instead of text.

link to archive custom text on each item

0

You can use below code for your requirement. it will add website/file url to cart page, checkout page and order summary in back-end.

You need to create one custom field for website/file url in product page. https://prnt.sc/pa7fyg

that url/link will be display at:

You can change text/label as per your need.

  /*
    # add link filed above add to cart button
    */

function output_web_file_url_field() {
    global $product;
     $id = $product->get_id();
     $web_file_url = get_post_meta($id,"file_website_url",true);
    if(!empty($web_file_url)){
    ?>
    <div class="web-file-url-field">
        <a href="<?php echo $web_file_url;?>"  target="_blank"/>Website/file</a>
        <input type="hidden" name="web-file-url" value="<?php echo $web_file_url;?>">
    </div>
    <?php
   }
}



add_action( 'woocommerce_before_add_to_cart_button', 'output_web_file_url_field', 10 );

/*
# Add Link to cart item.
*/
function web_file_url_to_cart_item( $cart_item_data, $product_id, $variation_id ) {
    $web_file_url = filter_input( INPUT_POST, 'web-file-url' );

    if ( empty( $web_file_url ) ) {
        return $cart_item_data;
    }

    $cart_item_data['web-file-url'] = $web_file_url;

    return $cart_item_data;
}

add_filter( 'woocommerce_add_cart_item_data', 'web_file_url_to_cart_item', 10, 3 );

/*
# Display link in cart
*/
function display_web_file_url_cart( $item_data, $cart_item ) {
    if ( empty( $cart_item['web-file-url'] ) ) {
        return $item_data;
    }

    $item_data[] = array(
        'key'     => __( 'Website/file', 'iconic' ),
        'value'   => sprintf( __( '<a href="%s" target="_blank">Link</a>'), $cart_item['web-file-url']),
        'display' => '',
    );

    return $item_data;
}
add_filter( 'woocommerce_get_item_data', 'display_web_file_url_cart', 10, 2 );

/*
# Add Link to order.
*/
function add_web_file_url_to_order_items( $item, $cart_item_key, $values, $order ) {
    if ( empty( $values['web-file-url'] ) ) {
        return;
    }
    $item->add_meta_data( __( 'Website_file_url', 'iconic' ), sprintf( __( '<a href="%s" target="_blank">Link</a>'), $values['web-file-url']) );
}
add_action( 'woocommerce_checkout_create_order_line_item', 'add_web_file_url_to_order_items', 10, 4 );

I have tested code and it is working. let me know if this works for you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.