bigcommerce / bigcommerce-for-wordpress

A headless commerce integration for WordPress, powered by BigCommerce
https://www.bigcommerce.com/wordpress/
GNU General Public License v2.0
109 stars 49 forks source link

PHP8 Fatal error when using Cart::get_cart_id() #406

Closed ll-r-magner closed 1 year ago

ll-r-magner commented 1 year ago

Expected behavior

Used to work in 7.4.

Actual behavior

Used to be a warning. I am a Javascript Dev so im trying to understand the issue here. Going down the rabbit hole suggestions have been to reformat to: $temp = new Cart(); $bc_cart_id = $temp->get_cart_id();

Steps to reproduce behavior

  1. Syntax im using is $bc_cart_id = Cart::get_cart_id();

Screenshot/Video (if applicable)

Example Image

Workaround or possible solution

I have no idea currently. Even after I tried refactoring to suggestions it throws errors saying no arguments were passed.

ll-r-magner commented 1 year ago

Fatal error: Uncaught ArgumentCountError: Too few arguments to function BigCommerce\Cart\Cart::__construct(), 0 passed in /www/ledgeclone121222_223/public/wp-content/themes/ll-ecommerce/page-templates/ll-umbrellas-pdp.php on line 317 and exactly 1 expected in /www/ledgeclone121222_223/public/wp-content/plugins/bigcommerce/src/BigCommerce/Cart/Cart.php:34 Stack trace: #0 /www/ledgeclone121222_223/public/wp-content/themes/ll-ecommerce/page-templates/ll-umbrellas-pdp.php(317): BigCommerce\Cart\Cart->__construct() #1 /www/ledgeclone121222_223/public/wp-includes/template.php(783): require_once('/www/ledgeclone...') #2 /www/ledgeclone121222_223/public/wp-content/themes/ll-ecommerce/bigcommerce/single-bigcommerce_product.php(48): load_template('/www/ledgeclone...') #3 /www/ledgeclone121222_223/public/wp-includes/template-loader.php(106): include('/www/ledgeclone...') #4 /www/ledgeclone121222_223/public/wp-blog-header.php(19): require_once('/www/ledgeclone...') #5 /www/ledgeclone121222_223/public/index.php(17): require('/www/ledgeclone...') #6 {main} thrown in /www/ledgeclone121222_223/public/wp-content/plugins/bigcommerce/src/BigCommerce/Cart/Cart.php on line 34 There has been a critical error on this website.

ll-r-magner commented 1 year ago

Also have tried syntax $cart = bigcommerce()->cart; then calling it via $bc_cart_id = $cart->get_cart_id();

MlKilderkin commented 1 year ago

Hello @ll-r-magner The $temp = new Cart(); should throw an error. It is not an issue. PHP 8.0 has more strict rules and class constructor expecting 1 argument provided. In order to achieve what you want use next code

use BigCommerce\Cart\Cart;
use BigCommerce\Container\Api;

$container = bigcommerce()->container();
$cart_api  = $container[ Api::FACTORY ]->cart();
$cart = new Cart( $cart_api );
$cart->get_cart_id()

First you get a global container. Than you get cart api entity and only after that you will be able to use Cart class.

$cart = bigcommerce()->cart; won't work for you because it provides cart container and not a card itself.

Please let me know if this helps or you have additional question

ll-r-magner commented 1 year ago

@MlKilderkin sorry im late to this been super busy. So that code works to create a new cart correct? What if a cart already exists and im just trying to add a new item to cart?

MlKilderkin commented 1 year ago

Hello @ll-r-magner no problem. Code above creates an object that allows to retrieve card id stored in cookies.

ll-r-magner commented 1 year ago

@MlKilderkin Ok, while I was waiting for a reply just now I think I was able to get it. If you dont mind checking out what im doing here. The "add_to_cart" function was created by another dev which I can share if need be. Our products are a little over engineered so some extra logic is required. As far as dealing with the Cart() object does this look right?

UPDATE: Well adding this product to cart didnt throw a PHP error so thats progress. I am getting a 400 response when adding to cart though.

UPDATE V2: So, because the add_to_cart function relies on redirecting to the cart page it does work as intended however, we use the BC4WP setting AJAX Add to Cart which doesn't redirect. Is there a way to make certain template files redirect while allowing the majority to use the global setting? @MlKilderkin

    if (!empty($_POST)) {
        // cart id from bigcommerce cookie
    $container = bigcommerce()->container();
    $cart_api  = $container[ Api::FACTORY ]->cart();
    $cart = new Cart( $cart_api );
    $bc_cart_id = $cart->get_cart_id();
    // $cart->get_cart_id()
    // $cart = bigcommerce()->cart;
        // $bc_cart_id = $cart->get_cart_id();

        $post_product = \BigCommerce\Post_Types\Product\Product::by_product_id( $_POST['product'] );

        if (isset($bc_cart_id) && $bc_cart_id !== '') {
            // cart has been created already
            add_to_cart($post_product, $_POST, $bc_cart_id);
            header('Location: /cart');

        } else {
            // cart has not been created
      $container = bigcommerce()->container();
      $cart_api  = $container[ Api::FACTORY ]->cart();
      $cart = new Cart( $cart_api );
      // $cart->get_cart_id();

            $new_cart = add_to_cart($post_product, $_POST, null);
            $cart->set_cart_id($new_cart['data']['id']);

            header('Location: /cart');
        }
    }
ll-r-magner commented 1 year ago

@MlKilderkin Is there a way to set a single page to redirect to /cart on add to cart and have the rest of the pages do the AJAX add to cart function (not redirecting to cart)?