Reputation: 241
So I'm developing an e-commerce with WooCommerce, I'm adding a product manualy via add_to_cart
function. I get the return value of add_to_cart (the order_item_id) and store at a custom table with some other values.
The problem is that in the payment gateways I'm trying to add a logic, and need to get these informations back from the DB, I tried to get the WC()->cart->cart_contents, do a foreach and use the key to do a select in my custom table, but this key is a hash and the value returned by the add_to_cart function is an integer.
Someone can give me a help?
Upvotes: 1
Views: 3876
Reputation: 3809
WooCommerce add_to_cart
will return a 'hash key' and not integer. Check like this.
global $woocommerce;
$addkey = $woocommerce->cart->add_to_cart('product ID', 'quantity');
foreach ( $woocommerce->cart->cart_contents as $cart_item_key => $cart_item ) {
$cartkey = $cart_item['key'];
}
You can see both $addkey
& $cartkey
return the same value.
Upvotes: 3
Reputation: 254251
Normally if you look in the source code of the
WC_Cart
add_to_cart()
method, on a successful add to cart, the right needed cart item key is returned, like:$cart_item_key = add_to_cart( $product_id );
Now you could use a custom function hooked in woocommerce_add_to_cart
to get the necessary cart_item_key and store all necessary related information in your custom database.
There is all the necessary arguments in this hooked function that you can use for it:
add_action( 'woocommerce_add_to_cart', 'custom_action_add_to_cart', 20, 6 );
function custom_action_add_to_cart( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ){
// HERE come your code
}
This hook is triggered each time add_to_cart()
function is used.
Upvotes: 1