最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugin development - In woocommerce cart item we can add custom text but how can i add custom link?

programmeradmin2浏览0评论
Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 4 years ago.

Improve this question

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

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 4 years ago.

Improve this question

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

Share Improve this question asked Sep 24, 2019 at 6:46 Zaheer AhmadZaheer Ahmad 115 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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:

  • in product detail page above add to cart button. https://prnt.sc/pa7h9s
  • Cart page https://prnt.sc/pa7hxb
  • Checkout page https://prnt.sc/pa7j43
  • order summary frontend /backend https://prnt.sc/pa7jsp https://prnt.sc/pa7k70

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!

发布评论

评论列表(0)

  1. 暂无评论