@LastGeneral

Как сделать так что бы кнопка работала и с вариативными товарами?

Кнопка работает только с простыми товарами:
628e03e3815ae556855811.png
Когда добавляю id вариативного товара просто переходит на страницу
628e0430cc5e8833010594.png
А когда id одного из вариантов, то кнопка не отображается
628e04560412a403917872.png

Как сделать так что бы работало и с вариативным товаром?

<?php echo do_shortcode( "[ajax_add_to_cart id='4739' text='Buy']" ); ?>

if( ! function_exists('custom_ajax_add_to_cart_button') && class_exists('WooCommerce') ) {
	function custom_ajax_add_to_cart_button( $atts ) {
		// Shortcode attributes
		$atts = shortcode_atts( array(
			'id' => '0', // Product ID
			'qty' => '1', // Product quantity
			'text' => '', // Text of the button
			'class' => '', // Additional classes
		), $atts, 'ajax_add_to_cart' );

		if( esc_attr( $atts['id'] ) == 0 ) return; // Exit when no Product ID

		if( get_post_type( esc_attr( $atts['id'] ) ) != 'product' ) return; // Exit if not a Product

		$product = wc_get_product( esc_attr( $atts['id'] ) );

		if ( ! $product ) return; // Exit when if not a valid Product

		$classes = implode( ' ', array_filter( array(
			'button',
			'product_type_' . $product->get_type(),
			$product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
			$product->supports( 'ajax_add_to_cart' ) ? 'ajax_add_to_cart' : '',
		) ) ).' '.$atts['class'];

		$add_to_cart_button = sprintf( '<a rel="nofollow" href="%s" %s %s %s class="%s">%s</a>',
									  esc_url( $product->add_to_cart_url() ),
									  'data-quantity="' . esc_attr( $atts['qty'] ) .'"',
									  'data-product_id="' . esc_attr( $atts['id'] ) .'"',
									  'data-product_sku="' . esc_attr( $product->get_sku() ) .'"',
									  esc_attr( isset( $classes ) ? $classes : 'button' ),
									  esc_html( empty( esc_attr( $atts['text'] ) ) ? $product->add_to_cart_text() : esc_attr( $atts['text'] ) )
									 );

		return $add_to_cart_button;
	}
	add_shortcode('ajax_add_to_cart', 'custom_ajax_add_to_cart_button');
}
  • Вопрос задан
  • 37 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы