__( 'Your session has expired. Please reload the page.', 'eazign' ) ), 403 );
}
$design_id = isset( $_POST['design_id'] ) ? sanitize_key( wp_unslash( $_POST['design_id'] ) ) : '';
$quote = $design_id ? Eazign_Client::post( 'cart', array( 'designId' => $design_id ) ) : new WP_Error( 'eazign_design', __( 'Design missing.', 'eazign' ) );
if ( is_wp_error( $quote ) ) {
wp_send_json_error( array( 'message' => $quote->get_error_message() ) );
}
$product = wc_get_product( (int) $quote['productId'] );
$variation = wc_get_product( (int) $quote['variationId'] );
if ( ! Eazign_Frontend::is_designer_product( $product ) || ! $variation || $variation->get_parent_id() !== $product->get_id() ) {
wp_send_json_error( array( 'message' => __( 'This product cannot be designed.', 'eazign' ) ) );
}
self::$adding = true;
$key = WC()->cart->add_to_cart(
$product->get_id(),
1,
$variation->get_id(),
wc_get_product_variation_attributes( $variation->get_id() ),
array(
'eazign' => array(
'design_id' => sanitize_key( $quote['designId'] ),
'preview' => esc_url_raw( (string) $quote['previewUrl'] ),
'sides' => array_map( 'sanitize_text_field', (array) $quote['sides'] ),
'size' => sanitize_text_field( (string) $quote['size'] ),
'surcharge' => (float) $quote['surcharge'],
'lines' => array_map(
function ( $line ) {
return array(
'label' => sanitize_text_field( $line['label'] ),
'amount' => (float) $line['amount'],
);
},
(array) $quote['lines']
),
),
)
);
self::$adding = false;
if ( ! $key ) {
$notices = wc_get_notices( 'error' );
wc_clear_notices();
$message = $notices ? wp_strip_all_tags( $notices[0]['notice'] ) : __( 'Could not add the item to the cart.', 'eazign' );
wp_send_json_error( array( 'message' => $message ) );
}
wp_send_json_success( array( 'redirect' => wc_get_cart_url() ) );
}
/**
* Designer products can only be bought with a design.
*
* @param bool $valid Valid so far.
* @param int $product_id Product id.
* @return bool
*/
public static function require_design( $valid, $product_id ) {
if ( $valid && ! self::$adding && Eazign_Frontend::is_designer_product( wc_get_product( $product_id ) ) ) {
wc_add_notice( __( 'Please design your product first: click "Design now".', 'eazign' ), 'error' );
return false;
}
return $valid;
}
/**
* Variation price plus the quoted surcharges.
*
* @param WC_Cart $cart Cart.
*/
public static function apply_prices( $cart ) {
if ( is_admin() && ! wp_doing_ajax() ) {
return;
}
foreach ( $cart->get_cart() as $item ) {
if ( empty( $item['eazign']['surcharge'] ) ) {
continue;
}
// Fresh product: the cart object may already carry a raised price.
$base = wc_get_product( $item['variation_id'] ? $item['variation_id'] : $item['product_id'] );
if ( $base ) {
$item['data']->set_price( (float) $base->get_price( 'edit' ) + (float) $item['eazign']['surcharge'] );
}
}
}
/**
* Design details under the item name (cart, checkout, mini cart, blocks).
*
* @param array $data Item data.
* @param array $item Cart item.
* @return array
*/
public static function item_data( $data, $item ) {
if ( empty( $item['eazign'] ) ) {
return $data;
}
$e = $item['eazign'];
$data[] = array(
'key' => __( 'Print sides', 'eazign' ),
'value' => implode( ', ', $e['sides'] ),
);
if ( ! empty( $e['size'] ) ) {
$data[] = array(
'key' => __( 'Size', 'eazign' ),
'value' => $e['size'],
);
}
foreach ( $e['lines'] as $line ) {
$data[] = array(
'key' => $line['label'],
'value' => wp_strip_all_tags( wc_price( $line['amount'] ) ),
'display' => wc_price( $line['amount'] ),
);
}
return $data;
}
/**
* Design preview as cart thumbnail.
*
* @param string $html Thumbnail.
* @param array $item Cart item.
* @return string
*/
public static function thumbnail( $html, $item ) {
if ( empty( $item['eazign']['preview'] ) ) {
return $html;
}
return sprintf( '
', esc_url( $item['eazign']['preview'] ), esc_attr__( 'Your design', 'eazign' ) );
}
/**
* Design preview in the block cart and checkout.
*
* @param array $images Images.
* @param array $item Cart item.
* @return array
*/
public static function store_api_images( $images, $item ) {
if ( empty( $item['eazign']['preview'] ) ) {
return $images;
}
$url = esc_url( $item['eazign']['preview'] );
return array(
(object) array(
'id' => 0,
'src' => $url,
'thumbnail' => $url,
'srcset' => '',
'sizes' => '',
'name' => __( 'Your design', 'eazign' ),
'alt' => __( 'Your design', 'eazign' ),
),
);
}
/**
* Design data on the order line item. Keys with a leading underscore
* are hidden from the customer.
*
* @param WC_Order_Item_Product $item Line item.
* @param string $cart_key Cart item key.
* @param array $values Cart item.
*/
public static function order_item( $item, $cart_key, $values ) {
if ( empty( $values['eazign'] ) ) {
return;
}
$e = $values['eazign'];
$item->add_meta_data( '_eazign_design_id', $e['design_id'], true );
$item->add_meta_data( '_eazign_preview', $e['preview'], true );
$item->add_meta_data( __( 'Print sides', 'eazign' ), implode( ', ', $e['sides'] ), true );
if ( ! empty( $e['size'] ) ) {
$item->add_meta_data( __( 'Size', 'eazign' ), $e['size'], true );
}
foreach ( $e['lines'] as $line ) {
$item->add_meta_data( $line['label'], wp_strip_all_tags( wc_price( $line['amount'] ) ), true );
}
}
/**
* Design preview in order e-mails and the account area.
*
* @param string $html Thumbnail.
* @param WC_Order_Item_Product $item Line item.
* @return string
*/
public static function order_thumbnail( $html, $item ) {
$preview = $item->get_meta( '_eazign_preview' );
return $preview ? sprintf( '
', esc_url( $preview ), esc_attr__( 'Your design', 'eazign' ) ) : $html;
}
}