model->product_medias->data ) || is_wp_error( $this->model->product_medias->data ) ) {
return;
}
$product_media = $this->model->product_medias->data[0];
?>
isEnabled() ) {
return;
}
if ( empty( $this->model->id ) ) {
return;
}
$is_bundle = ! empty( $this->model->bundle );
$url_key = $is_bundle ? 'bundle' : 'product';
$wp_admin_bar->add_node(
[
'id' => 'edit',
'title' => $is_bundle ? __( 'Edit Bundle', 'surecart' ) : __( 'Edit Product', 'surecart' ),
'href' => esc_url( \SureCart::getUrl()->edit( $url_key, $this->model->id ) ),
]
);
}
/**
* Show the product page
*
* @param \SureCartCore\Requests\RequestInterface $request Request.
* @param \SureCartCore\View $view View.
* @param string $id The id of the product.
* @return function
*/
public function show( $request, $view, $id ) {
// The route always passes a product slug here.
$id = get_query_var( 'sc_checkout_product_id' );
// fetch the product by id/slug. Bundle expands carry live component
// variant stock, so the buy page renders fresh availability in one fetch.
$this->model = \SureCart\Models\Product::with(
\SureCart\Models\Product::storefrontExpands()
)->find( $id );
if ( is_wp_error( $this->model ) ) {
return $this->handleError( $this->model );
}
// if this buy page is not enabled, check read permissions.
if ( ! $this->model->buyLink()->isEnabled() && ! current_user_can( 'read_sc_products' ) ) {
return $this->notFound();
}
// slug changed or we are using the id, redirect.
if ( $this->model->slug !== $id ) {
return \SureCart::redirect()->to( esc_url_raw( \SureCart::routeUrl( 'product', [ 'id' => $this->model->slug ] ) ) );
}
// get active prices.
$active_prices = $this->model->active_prices;
// must have at least one active price.
if ( empty( $active_prices[0] ) ) {
return $this->notFound();
}
// prevent 404 redirects by 3rd party plugins.
$_SERVER['REQUEST_URI'] = $request->getUrl();
// add the filters.
$this->filters();
// prepare data.
$this->model = $this->model->withActivePrices()->withSortedPrices();
$first_variant_with_stock = $this->model->first_variant_with_stock;
if ( ! empty( $this->model->prices->data[0]->id ) ) {
$line_item = array_merge(
[
'price_id' => $this->model->prices->data[0]->id,
'quantity' => 1,
],
! empty( $first_variant_with_stock->id ) ? [ 'variant_id' => $first_variant_with_stock->id ] : []
);
// Bundles require a variant selection per variable component on checkout creation.
if ( ! empty( $this->model->bundle ) ) {
$bundle_component_variants = (array) ( new \SureCart\Models\Blocks\ProductPageBlock() )
->getInitialBundleComponentVariants( $this->model );
if ( ! empty( $bundle_component_variants ) ) {
$line_item['bundle_component_variants'] = $bundle_component_variants;
}
}
sc_initial_state(
[
'checkout' => [
'initialLineItems' => sc_initial_line_items( [ $line_item ] ),
],
]
);
}
// render the view.
return \SureCart::view( 'web/buy' )->with(
[
'product' => $this->model,
'terms_text' => $this->termsText(),
'mode' => $this->model->buyLink()->getMode(),
'store_name' => \SureCart::account()->name ?? get_bloginfo(),
'logo_url' => \SureCart::theme()->logoUrl(),
'logo_width' => \SureCart::settings()->get( 'buy_link_logo_width', '180px' ),
'user' => wp_get_current_user(),
'logout_link' => wp_logout_url( $request->getUrl() ),
'dashboard_link' => \SureCart::pages()->url( 'dashboard' ),
'enabled' => $this->model->buyLink()->isEnabled(),
'show_logo' => $this->model->buyLink()->templatePartEnabled( 'logo' ),
'show_terms' => $this->model->buyLink()->templatePartEnabled( 'terms' ),
'show_image' => $this->model->buyLink()->templatePartEnabled( 'image' ),
'show_description' => $this->model->buyLink()->templatePartEnabled( 'description' ),
'show_coupon' => $this->model->buyLink()->templatePartEnabled( 'coupon' ),
'success_url' => $this->model->buyLink()->getSuccessUrl(),
]
);
}
/**
* Enqueue styles.
*
* @return void
*/
public function styles() {
wp_enqueue_style(
'surecart/instant-checkout',
trailingslashit( \SureCart::core()->assets()->getUrl() ) . 'dist/templates/instant-checkout.css',
[],
filemtime( trailingslashit( plugin_dir_path( SURECART_PLUGIN_FILE ) ) . 'dist/templates/instant-checkout.css' ),
);
// add recaptcha if enabled.
if ( \SureCart::settings()->recaptcha()->isEnabled() ) {
wp_enqueue_script( 'surecart-google-recaptcha' );
}
}
/**
* Generate the terms html.
*
* @return string
*/
public function termsText() {
$terms_url = \SureCart::account()->customer_portal_protocol->terms_url;
$privacy_url = \SureCart::account()->customer_portal_protocol->privacy_url;
if ( ! empty( $terms_url ) && ! empty( $privacy_url ) ) {
return sprintf(
// translators: %1$1s is the store name, %2$2s is the opening anchor tag, %3$3s is the closing anchor tag, %4$4s is the opening anchor tag, %5$5s is the closing anchor tag.
__( "I agree to %1$1s's %2$2sTerms%3$3s and %4$4sPrivacy Policy%5$5s", 'surecart' ),
esc_html( \SureCart::account()->name ),
'',
'',
'',
''
);
}
if ( $terms_url ) {
return sprintf(
// translators: %1$1s is the store name, %2$2s is the opening anchor tag, %3$3s is the closing anchor tag.
__( "I agree to %1$1s's %2$2sTerms%3$3s", 'surecart' ),
esc_html( \SureCart::account()->name ),
'',
''
);
}
if ( $privacy_url ) {
return sprintf(
// translators: %1$1s is the store name, %2$2s is the opening anchor tag, %3$3s is the closing anchor tag.
__( "I agree to %1$1s's %2$2sPrivacy Policy%3$3s", 'surecart' ),
esc_html( \SureCart::account()->name ),
'',
''
);
}
return '';
}
/**
* Add the SEO meta data.
*
* @return void
*/
public function addSeoMetaData(): void {
$product = $this->model;
if ( empty( $product ) ) {
return;
}
$display_seo_meta = apply_filters( 'sc_display_instant_checkout_seo_meta', true, $product );
if ( ! $display_seo_meta ) {
return;
}
// render the seo meta. og:url must be the buy link — the mirrored post permalink can 404 for buy-link-only products.
\SureCart::productSeo()->renderProductSeoMeta( $product, $product->checkout_permalink );
}
/**
* Add the JSON-LD schema for the product.
*
* @return void
*/
public function addProductJsonSchema(): void {
$product = $this->model;
if ( empty( $product ) ) {
return;
}
$display_schema = apply_filters( 'sc_display_instant_checkout_json_ld_schema', true, $product );
if ( ! $display_schema ) {
return;
}
\SureCart::productSeo()->renderJsonSchema( $product );
}
}