Coder Social home page Coder Social logo

Comments (7)

MlKilderkin avatar MlKilderkin commented on June 16, 2024 1

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

from bigcommerce-for-wordpress.

ll-r-magner avatar ll-r-magner commented on June 16, 2024

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.

from bigcommerce-for-wordpress.

ll-r-magner avatar ll-r-magner commented on June 16, 2024

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

from bigcommerce-for-wordpress.

ll-r-magner avatar ll-r-magner commented on June 16, 2024

@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?

from bigcommerce-for-wordpress.

MlKilderkin avatar MlKilderkin commented on June 16, 2024

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

from bigcommerce-for-wordpress.

ll-r-magner avatar ll-r-magner commented on June 16, 2024

@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');
		}
	}

from bigcommerce-for-wordpress.

ll-r-magner avatar ll-r-magner commented on June 16, 2024

@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)?

from bigcommerce-for-wordpress.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.