php WooCommerce -如何为定义的会话键获取结账车项目

vcirk6k6  于 5个月前  发布在  PHP
关注(0)|答案(1)|浏览(69)

我在数据库中有一个表:

wp_woocommerce_sessions

字符串
用柱子:

session_id  
session_key 
session_value   
session_expiry  
 
9
t_2ca6a9a0ec3a668971f8bdbebea4c3
a:7:{s:4:"cart";s:6:"a:0:{}";s:11:"cart_totals";s:...
1703763057


是否有任何解决方案,以便我可以定义要为已定义的会话获取的 checkout 项?示例我想定义为session_key t_2ca6a9a0ec3a668971f8bdbebea4c3获取 checkout 项
我发现:woocommerce/includes/class-wc-cart-session.php

public function get_cart_from_session() {
        do_action( 'woocommerce_load_cart_from_session' );
        $this->cart->set_totals( WC()->session->get( 'cart_totals', null ) );
        $this->cart->set_applied_coupons( WC()->session->get( 'applied_coupons', array() ) );
        $this->cart->set_coupon_discount_totals( WC()->session->get( 'coupon_discount_totals', array() ) );
        $this->cart->set_coupon_discount_tax_totals( WC()->session->get( 'coupon_discount_tax_totals', array() ) );
        $this->cart->set_removed_cart_contents( WC()->session->get( 'removed_cart_contents', array() ) );

        $update_cart_session = false; // Flag to indicate the stored cart should be updated.
        $order_again         = false; // Flag to indicate whether this is a re-order.
        $cart                = WC()->session->get( 'cart', null );
        $merge_saved_cart    = (bool) get_user_meta( get_current_user_id(), '_woocommerce_load_saved_cart_after_login', true );


现在我想如何自定义:woocommerce_load_cart_from_session

jdgnovmf

jdgnovmf1#

wp_woocommerce_sessions表中获取会话数据:

function get_woocommerce_session_data($session_key) {
    global $wpdb;
    $table_name = $wpdb->prefix . 'woocommerce_sessions';

    $session_data = $wpdb->get_var($wpdb->prepare(
        "SELECT session_value FROM $table_name WHERE session_key = %s",
        $session_key
    ));

    return maybe_unserialize($session_data);
}

字符串
使用上面的函数提取购物车项目:

function get_cart_items_from_session_data($session_data) {
    if (isset($session_data['cart']) && is_array($session_data['cart'])) {
        $cart_items = array();
        foreach ($session_data['cart'] as $item_key => $item_value) {
            $cart_items[$item_key] = maybe_unserialize($item_value);
        }
        return $cart_items;
    }
    return false;
}


使用示例:

$session_key = 't_2ca6a9a0ec3a668971f8bdbebea4c3'; // replace with your session key
$session_data = get_woocommerce_session_data($session_key);
$cart_items = get_cart_items_from_session_data($session_data);

if ($cart_items) {
    // Process the cart items
    foreach ($cart_items as $item) {
        // Your code here
    }
} else {
    echo 'No cart items found for this session.';
}

相关问题