[英]How save product id of 1st item added to cart in woocommerce for a visitor in empty cart page?
I am trying to display the product id which is added to cart at first in empty cart page( cart-empty.php
). 我试图显示最初在空购物车页面(
cart-empty.php
)中添加到购物车的产品ID。 Can anyone please help me with any solution. 谁能帮我任何解决方案。
Here is my code I have tried for cart-totals.php
page..which save both product slug and its parent slug which is added at last to the cart. 这是我尝试过的代码,用于
cart-totals.php
页面。 cart-totals.php
同时保存了产品slug和其父slug,最后将其添加到购物车。
$items = $woocommerce->cart->get_cart();
foreach($_SESSION['items'] as $item => $values) {
$_product = $values['data']->post;
$cartproductid = $_product->ID;
}
$product_cats = wp_get_post_terms( $cartproductid, 'product_cat' );
$url=$product_cats[0]->slug;
$parenturl=$product_cats[0]->parent;
$terms = get_the_terms($cartproductid, "product_cat");
$cat = get_term_by("id", $parenturl, "product_cat");
$newurl = $cat->slug;
This code displays a message whenever a user removes an item from their cart. 每当用户从购物车中删除商品时,此代码就会显示一条消息。 It may not be exactly what you want but it gives you something to start off with.
它可能不完全是您想要的,但它为您提供了一些起点。 Add this code to your theme
functions.php
or custom plugin code: 将此代码添加到您的主题
functions.php
或自定义插件代码中:
function this_function( $title , $cart_item){
wc_add_notice( 'Visit deleted product <a href="' . get_permalink( $cart_item['product_id'] ) . '">' . $title . '</a>' );
return $title;
}
add_filter( 'woocommerce_cart_item_removed_title', 'this_function', 10, 2 );
EDIT - More accurate solution : This code displays a message on the empty cart page with a link to the last deleted product. 编辑-更准确的解决方案 :此代码在空购物车页面上显示一条消息,并带有指向最后删除的产品的链接。 It works for users who are logged in. If you want the code to work for non-logged in visitors you'll need to modify this to use cookies.
它适用于已登录的用户。如果您希望代码适用于未登录的访问者,则需要对其进行修改以使用Cookie。
class empty_cart
{
public $last_product;
protected static $_instance = null;
public static function instance() {
if ( is_null( self::$_instance ) ) {
self::$_instance = new self();
}
return self::$_instance;
}
function __construct()
{
add_filter( 'woocommerce_cart_item_removed_title', array($this, 'product_removed'), 10, 2 );
add_action( 'woocommerce_cart_is_empty', array($this, 'cart_is_empty'));
}
function product_removed( $title , $cart_item){
update_user_meta( get_current_user_id(), 'last_removed_product', $cart_item['product_id']);
$this->last_product = $cart_item['product_id'];
return $title;
}
function cart_is_empty(){
$last_product = $this->get_last_product();
echo 'Visit deleted product <a href="' . get_permalink( $last_product ) . '">' . get_the_title( $last_product ) . '</a>';
}
function get_last_product(){
if( isset($this->last_product) )
return $this->last_product;
else
return get_user_meta( get_current_user_id(), 'last_removed_product',true );
}
}
function empty_cart() {
return empty_cart::instance();
}
empty_cart();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.