简体   繁体   English

如何在空购物车页面中保存访客在woocommerce中添加到购物车的第一个商品的产品ID?

[英]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.

相关问题 在 Woocommerce 购物车页面中的购物车项目名称后添加产品 ID - Add the product ID after the cart item name in Woocommerce cart page 在 WooCommerce 购物车中获取购物车项目的产品 ID - Get in WooCommerce cart the product ID of a cart item 如何计算 WooCommerce 购物车中添加的产品 ID 的额外费用 - How to sum the additional fees of added product ID's in WooCommerce cart 刷新 WooCommerce 产品页面添加到购物车 - Refresh WooCommerce product page on added to cart 在 WooCommerce 购物车页面的购物车商品名称后添加产品 ID 用于特定产品 - Add the product ID after the cart item name in WooCommerce cart page for specific products 如何将购物车商品产品重定向到Woocommerce中的特定页面? - How can i redirect cart item product to specific page in Woocommerce? 如何检查可变产品 ID 是否在 woocommerce 购物车中? - How to check if variable product ID is in the woocommerce cart? 根据Woocommerce中的产品类别将最大商品数量添加到购物车 - Max item quantity added to cart based on product category in Woocommerce woocommerce产品附加组件-转到产品页面时的购物车为空 - woocommerce product addon - empty cart upon going to product page Wordpress Woocommerce产品正在页面刷新时添加到购物车 - Wordpress Woocommerce product is being added to cart on page refresh
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM