Add_Item_View

Summary

Class Add_Item_View


Description

Adds the “Add to Wish List” template after the purchase form on the product single view


Source

File: src/BigCommerce/Accounts/Wishlists/Add_Item_View.php

class Add_Item_View {
	/** @var WishlistsApi */
	private $wishlists;

	public function __construct( WishlistsApi $wishlists ) {
		$this->wishlists = $wishlists;
	}

	/**
	 * @param array  $data
	 * @param string $template
	 * @param array  $options
	 *
	 * @return array
	 * @filter bigcommerce/template=components/products/product-single.php/data
	 */
	public function filter_product_single_template( $data, $template, $options ) {
		if ( ! get_option( Wishlist_Settings::ENABLED ) || ! is_user_logged_in() ) {
			return $data;
		}
		$customer    = new Customer( get_current_user_id() );
		$customer_id = $customer->get_customer_id();
		if ( empty( $customer_id ) ) {
			return $data;
		}
		$wishlists  = $this->get_wishlists( $customer_id );
		$controller = Wishlist_Add_Item::factory( [
			Wishlist_Add_Item::PRODUCT_ID => $data[ Product_Single::PRODUCT ]->bc_id(),
			Wishlist_Add_Item::WISHLISTS  => $wishlists,
		] );

		$data[ Product_Single::FORM ] .= $controller->render();

		return $data;
	}

	/**
	 * @param int $customer_id
	 *
	 * @return Account_Wishlist[]
	 */
	private function get_wishlists( $customer_id ) {
		try {
			return array_map( function ( Api_Wishlist $wishlist ) {
				return new Account_Wishlist( $wishlist );
			}, $this->wishlists->listWishlists( [ 'customer_id' => $customer_id ] )->getData() );
		} catch ( ApiException $e ) {
			return [];
		}
	}
}

Methods


User Contributed Notes

You must log in before being able to contribute a note or feedback.