0

I am trying to add the permalink of product on woocommerce admin order page with this code:

function fpd_custom_order_item_values($_product) {

$url = $_product->permalink;

echo '<p>'.$url.'</p>';
}
add_action( 'woocommerce_before_order_itemmeta', 'fpd_custom_order_item_values', 100, 1);

However, this does not output the product permalink. When I replace $url = $_product->permalink; with $url = $_product->name;, the name gets echoed. What am I doing wrong here?

0

There's no permalink property of a product. Permalinks are not stored in the database, because they're generated based on a number of external factors. Therefore you need to use a function to retrieve the permalink. In WooCommerce the best way to do this is with:

function fpd_custom_order_item_values($_product) {
    $url = $_product->get_permalink();

    echo '<p>' . esc_url( $url ) . '</p>';
}
add_action( 'woocommerce_before_order_itemmeta', 'fpd_custom_order_item_values', 100, 1);

Also, make sure you're escaping your output. Since this is a URL, use esc_url().

  • This is working fine for orders with products but if an order contains products + flat rate shipping (which does not have a permalink), there's an error as shipping charge doesn't have any permalink. How to solve this? – dc09 Jun 18 at 16:22

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.