Content is user-generated and unverified.
<?php /** * Meta Shop to WooCommerce Cart Handler * Add this to your child theme's functions.php file */ // Handle Meta Shop checkout URL parameters add_action('template_redirect', 'handle_meta_shop_checkout'); function handle_meta_shop_checkout() { // Only run on checkout page and if Meta parameters are present if (!is_checkout() || !isset($_GET['meta_items'])) { return; } // Clear existing cart to start fresh WC()->cart->empty_cart(); // Meta typically sends parameters like: // ?meta_items=product_id:quantity,product_id:quantity // Adjust parsing based on Meta's actual parameter format $meta_items = sanitize_text_field($_GET['meta_items']); // Parse the items (adjust delimiter based on Meta's format) $items = explode(',', $meta_items); foreach ($items as $item) { // Split product_id and quantity $parts = explode(':', $item); if (count($parts) >= 2) { $product_id = intval($parts[0]); $quantity = intval($parts[1]); // Handle variation_id if present (format: product_id:quantity:variation_id) $variation_id = isset($parts[2]) ? intval($parts[2]) : 0; // Add to cart if ($variation_id > 0) { WC()->cart->add_to_cart($product_id, $quantity, $variation_id); } else { WC()->cart->add_to_cart($product_id, $quantity); } } } // Optional: Redirect to clean URL without parameters if (WC()->cart->get_cart_contents_count() > 0) { wp_safe_redirect(wc_get_checkout_url()); exit; } } /** * Alternative: If Meta uses separate parameters for each item * Format: ?item[]=123&qty[]=2&item[]=456&qty[]=1 */ function handle_meta_shop_checkout_array_format() { if (!is_checkout() || !isset($_GET['item'])) { return; } WC()->cart->empty_cart(); $items = isset($_GET['item']) ? $_GET['item'] : array(); $quantities = isset($_GET['qty']) ? $_GET['qty'] : array(); foreach ($items as $index => $product_id) { $product_id = intval($product_id); $quantity = isset($quantities[$index]) ? intval($quantities[$index]) : 1; if ($product_id > 0 && $quantity > 0) { WC()->cart->add_to_cart($product_id, $quantity); } } if (WC()->cart->get_cart_contents_count() > 0) { wp_safe_redirect(wc_get_checkout_url()); exit; } } /** * Generate your checkout URL for Meta Commerce Manager * Use this format when setting up your checkout URL in Meta */ function get_meta_checkout_url_example() { // Basic format with placeholder // https://yoursite.com/checkout/?meta_items={{product.id}}:{{quantity}} // Or if using array format: // https://yoursite.com/checkout/?item[]={{product.id}}&qty[]={{quantity}} return wc_get_checkout_url() . '?meta_items={product_id}:{quantity}'; } /** * Debug function to see what parameters Meta is sending * Remove this after testing */ add_action('template_redirect', 'debug_meta_parameters'); function debug_meta_parameters() { if (is_checkout() && !empty($_GET)) { error_log('Meta Shop Parameters: ' . print_r($_GET, true)); // You can also display on page temporarily for testing: // echo '<pre>'; print_r($_GET); echo '</pre>'; exit; } }
Content is user-generated and unverified.
    Meta Shop to WooCommerce Cart Handler | Claude