is_sold_individually() && $product->is_purchasable() && $product->is_in_stock();
}
public function registerBlockModule() {
if ( function_exists( 'wp_register_script_module' ) ) {
wp_register_script_module( self::BLOCK_MODULE,
ServiceContainer::getInstance()->getFileManager()->locateJSAsset( 'frontend/catalog-quantity' ),
array( '@wordpress/interactivity' ), TierPricingTablePlugin::VERSION );
}
}
/**
* The quantity box next to the Product Button block's button, on one line. The block keeps the
* quantity it adds in its Interactivity API context; the box's module writes the typed quantity there.
*
* @param string $content
* @param array $block
* @param WP_Block|mixed $instance
*
* @return string
*/
public function renderInProductButtonBlock( $content, $block, $instance ) {
if ( ! function_exists( 'wp_enqueue_script_module' ) || ! ( $instance instanceof WP_Block ) || empty( $instance->context['postId'] ) ) {
return $content;
}
// only the button of a product list: inside the add-to-cart-with-options form the block has its own quantity selector
if ( false === strpos( $content, 'actions.addCartItem' ) ) {
return $content;
}
$product = wc_get_product( (int) $instance->context['postId'] );
if ( ! $this->wantsQuantityField( $product ) ) {
return $content;
}
$quantity = woocommerce_quantity_input( array(
'min_value' => 1,
'max_value' => $product->backorders_allowed() ? '' : $product->get_stock_quantity(),
), $product, false );
if ( ! $quantity ) {
return $content;
}
// the box is its own interactive region; the input feeds the button block's context
$quantity = preg_replace( '/
]*\sstyle="([^"]*)"/', $content, $m ) && preg_match( '/margin-top:[^;"]+/', $m[1], $marginTop ) ) {
$rowStyle = ' style="' . esc_attr( $marginTop[0] ) . ';"';
$content = preg_replace_callback( '/(