在 WooCommerce 的结帐页面上使产品名称可点击(链接)+ 缩略图
Make product name clickable (linked) + thumbnail on checkout page in WooCommerce
我在网上找到一些代码,我对其进行了轻微修改以更好地匹配我正在使用的主题。我似乎无法弄清楚如何 link 到实际产品。
换句话说;产品名称应该可以点击(linked)到产品页面。这发生在购物车页面上,而不是结帐时。
我使用的代码:
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 );
我在网上找到一些代码,我对其进行了轻微修改以更好地匹配我正在使用的主题。我似乎无法弄清楚如何 link 到实际产品。
换句话说;产品名称应该可以点击(linked)到产品页面。这发生在购物车页面上,而不是结帐时。
我使用的代码:
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 );