우커머스 | 바로구매 버튼 생성하기

|

바로구매 버튼 생성

// Add buy now button to WooCommerce
function add_content_after_addtocart() {

	$current_product_id = get_the_ID();
	
	$product = wc_get_product( $current_product_id );
	
	$checkout_url = wc_get_checkout_url();
	
	if( $product->is_type( 'simple' ) ){
	echo '<a href="'.$checkout_url.'?add-to-cart='.$current_product_id.'" class="buy-now button">Buy Now</a>';
	}
	}
	add_action( 'woocommerce_after_add_to_cart_button', 'add_content_after_addtocart' );

바로그매 버튼 생성 2

<?php
add_action( 'woocommerce_after_shop_loop_item', 'cssigniter_buy_now_button', 15 );
add_action( 'woocommerce_after_add_to_cart_button', 'cssigniter_buy_now_button' );
function cssigniter_buy_now_button() {
	global $product;

	if ( 'simple' !== $product->get_type()
	|| ! $product->is_purchasable()
	|| ! $product->is_in_stock() ) {
		return;
	}

	$id = $product->get_ID();

	$classes = implode(
		' ',
		array_filter(
			array(
				'button',
				'product_type_' . $product->get_type(),
				'add_to_cart_button',
			)
		)
	);

	ob_start();


?>
	<a
	href="<?php echo esc_url( wc_get_checkout_url() ); ?>?add-to-cart=<?php echo absint( $id ); ?>"
	class="<?php echo esc_attr( $classes ); ?>"
	rel="nofollow">
	<?php echo esc_html_e( 'Buy Now', 'your-text-domain' ); ?>
	</a>

	<?php

	echo ob_get_clean();
}

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다

클릭하여 복사