우커머스-카트 확인후 조건부 체크아웃

우커머스-카트 확인후 조건부 체크아웃

Product Specific Conditional

/** * Check if a specific product ID is in the cart */ function wc_ninja_product_is_in_the_cart() { // Add your special product IDs here $ids = array( ’45’, ’70’, ’75’ );; // Products currently in the cart $cart_ids = array(); // Find each product in the cart and add it to the $cart_ids array foreach( WC()->cart->get_cart() as $cart_item_key => $values ) { $cart_product = $values[‘data’]; $cart_ids[] = $cart_product->id; } // If one of the special products are in the cart, return true. if ( ! empty( array_intersect( $ids, $cart_ids ) ) ) { return true; } else { return false; } }

 

/**
* Conditionally remove a checkout field based on products in the cart
*/
function wc_ninja_remove_checkout_field( $fields ) {
if ( ! wc_ninja_product_is_in_the_cart() ) {
unset( $fields[‘billing’][‘billing_company’] );
}

return $fields;
}
add_filter( ‘woocommerce_checkout_fields’ , ‘wc_ninja_remove_checkout_field’ );

 

Product Category Conditional

 

/**
* Check if a specific product category is in the cart
*/
function wc_ninja_category_is_in_the_cart() {
// Add your special category slugs here
$categories = array( ‘clothing’, ‘posters’ );

// Products currently in the cart
$cart_ids = array();

// Categories currently in the cart
$cart_categories = array();

// Find each product in the cart and add it to the $cart_ids array
foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
$cart_product = $values[‘data’];
$cart_ids[] = $cart_product->id;
}

// Connect the products in the cart w/ their categories
foreach( $cart_ids as $id ) {
$products_categories = get_the_terms( $id, ‘product_cat’ );

// Loop through each product category and add it to our $cart_categories array
foreach ( $products_categories as $products_category ) {
$cart_categories[] = $products_category->slug;
}
}

// If one of the special categories are in the cart, return true.
if ( ! empty( array_intersect( $categories, $cart_categories ) ) ) {
return true;
} else {
return false;
}
}

 

Coupon Conditional

 

/**
* Check if a specific coupon was used
*/
function wc_ninja_coupon_was_used() {
// Add a list of coupons to check for here.
$coupons = array( ‘percent_coupon’, ‘free_shipping’ );

// Coupons Used
$coupons_used = WC()->cart->applied_coupons;

// If ones of our special coupons were used, return true.
if ( in_array( $coupons, $coupons_used ) ) {
return true;
} else {
return false;
}
}

우커머스-제품이 카트에 이미 담겨졌습니다.

제품이 카트에 이미 담겨졌는지 확인해서 카트명을 변경합니다.

비슷한 제품이 많은 쇼핑몰인경우 효율적인 기능입니다.

<?php /** * Change the add to cart text on single product pages */ add_filter(‘woocommerce_product_single_add_to_cart_text’, ‘woo_custom_cart_button_text’); function woo_custom_cart_button_text() { foreach( WC()->cart->get_cart() as $cart_item_key => $values ) { $_product = $values[‘data’]; if( get_the_ID() == $_product->id ) { return __(‘Already in cart – Add Again?’, ‘woocommerce’); } } return __(‘Add to cart’, ‘woocommerce’); } /** * Change the add to cart text on product archives */ add_filter( ‘woocommerce_product_add_to_cart_text’, ‘woo_archive_custom_cart_button_text’ ); function woo_archive_custom_cart_button_text() { foreach( WC()->cart->get_cart() as $cart_item_key => $values ) { $_product = $values[‘data’]; if( get_the_ID() == $_product->id ) { return __(‘Already in cart’, ‘woocommerce’); } } return __(‘Add to cart’, ‘woocommerce’); }

주문완료 페이지 만들기

add_filter( 'the_content', 'wc_custom_thankyou' );
function wc_custom_thankyou( $content ) {
	// Check if is the correct page
	if ( ! is_page( {PAGE_ID} ) ) {
		return $content;
	}

	// check if the order ID exists
	if ( ! isset( $_GET['order'] ) ) {
		return $content;
	}

	// intval() ensures that we use an integer value for the order ID
	$order = wc_get_order( intval( $_GET['order'] ) );

	ob_start();

	// Check that the order is valid
	if ( ! $order ) {
		// The order can't be returned by WooCommerce - Just say thank you
		?><p><?php echo apply_filters( 'woocommerce_thankyou_order_received_text', __( 'Thank you. Your order has been received.', 'woocommerce' ), null ); ?></p><?php
	} else {
		if ( $order->has_status( 'failed' ) ) {
			// Order failed - Print error messages and ask to pay again

			/**
			 * @hooked wc_custom_thankyou_failed - 10
			 */
			do_action( 'wc_custom_thankyou_failed', $order );
		} else {
			// The order is successfull - print the complete order review

			/**
			 * @hooked wc_custom_thankyou_header - 10
			 * @hooked wc_custom_thankyou_table - 20
			 * @hooked wc_custom_thankyou_customer_details - 30
			 */
			do_action( 'wc_custom_thankyou_successful', $order );
		}
	}

	$content .= ob_get_contents();
	ob_end_clean();

	return $content;
}

Woocommerce Grouped Product Images

그룹상품 제품 이미지


[php]
add_action</span><span class="pun">(</span> <span class="str">'woocommerce_grouped_product_list_before_price'</span><span class="pun">,</span> <span class="str">'woocommerce_grouped_product_thumbnail'</span> <span class="pun">);</span>

<span class="kwd">function</span><span class="pln"> woocommerce_grouped_product_thumbnail</span><span class="pun">(</span><span class="pln"> $product </span><span class="pun">)</span> <span class="pun">{</span><span class="pln">
    $image_size </span><span class="pun">=</span><span class="pln"> array</span><span class="pun">(</span> <span class="lit">20</span><span class="pun">,</span> <span class="lit">20</span> <span class="pun">);</span>  <span class="com">// array( width, height ) image size in pixel </span><span class="pln">
    $attachment_id </span><span class="pun">=</span><span class="pln"> get_post_meta</span><span class="pun">(</span><span class="pln"> $product</span><span class="pun">-></span><span class="pln">id</span><span class="pun">,</span> <span class="str">'_thumbnail_id'</span><span class="pun">,</span> <span class="kwd">true</span> <span class="pun">);</span>
    <span class="pun">?></span>
    <span class="pun"><</span><span class="pln">td </span><span class="kwd">class</span><span class="pun">=</span><span class="str">"label"</span><span class="pun">></span>
        <span class="pun"><?</span><span class="pln">php echo wp_get_attachment_image</span><span class="pun">(</span><span class="pln"> $attachment_id</span><span class="pun">,</span><span class="pln"> $image_size </span><span class="pun">);</span> <span class="pun">?></span>
    <span class="pun"></</span><span class="pln">td</span><span class="pun">></span>
    <span class="pun"><?</span><span class="pln">php>

}

[/php]