我有在网上找到的代码,我稍微修改了一下,以更好地匹配我的主题。我似乎无法解决的是如何链接到实际产品。
换句话说,产品名称应该可以点击(链接)到产品页面。这发生在购物车页面,但不是在结账。
我正在使用的代码。
add_filter( 'woocommerce_cart_item_name', 'product_thumbnail_on_checkout_order_review', 20, 3 );
function product_thumbnail_on_checkout_order_review( $product_name, $cart_item, $cart_item_key ){
if (is_checkout()){
$thumbnail = $cart_item['data']->get_image(array( 80, 80));
$image_html = '<div class="product-item-thumbnail">'.$thumbnail.'</div> ';
$product_name = $image_html . $product_name;
}
return $product_name;
}
我试图通过查看购物车模板来解决这个问题,在那里我发现了这个。
$product_permalink = apply_filters( 'woocommerce_cart_item_permalink', $_product->is_visible() ? $_product->get_permalink( $cart_item ) : '', $cart_item, $cart_item_key );
然后我把它添加到代码中,变成了这个。
add_filter( 'woocommerce_cart_item_name', 'product_thumbnail_on_checkout_order_review', 20, 3 );
function product_thumbnail_on_checkout_order_review( $product_name, $cart_item, $cart_item_key ){
if (is_checkout()){
$thumbnail = $cart_item['data']->get_image(array( 80, 80));
$image_html = '<div class="product-item-thumbnail">'.$thumbnail.'</div> ';
$product_name_link = $product_permalink = apply_filters( 'woocommerce_cart_item_permalink', $_product->is_visible() ? $_product->get_permalink( $cart_item ) : '', $cart_item, $cart_item_key );
$product_name = $image_html . $product_name_link;
}
return $product_name;
}
但这给了我一个错误,现在我被卡住了。
Notice: Undefined variable: _product
_product
没有被定义,所以才会出现通知
这应该足够了,在代码中添加注释和解释。
function product_thumbnail_on_checkout_order_review( $product_name, $cart_item, $cart_item_key ) {
// Returns true on the checkout page.
if ( is_checkout() ) {
// Get product
$product = $cart_item['data'];
// Get image - thumbnail
$thumbnail = $product->get_image(array( 80, 80));
// Output
$image_html = '<div class="product-item-thumbnail">' . $thumbnail . '</div>';
// Product name + link
$product_name_link = '<a href="' . $product->get_permalink() . '">' . $product_name . '</a>';
// Output
$product_name = $image_html . $product_name_link;
}
return $product_name;
}
add_filter( 'woocommerce_cart_item_name', 'product_thumbnail_on_checkout_order_review', 20, 3 );