Code xử lý giỏ hàng cart woocommerce. Vâng trong bài viết này mình sẽ sưu tập các copy xử lý giỏ hàng để tiện cho mục đích copy trong quá trình code theme hoặc plugin
1 2 3 4 5 6 |
//số lượng sản phẩm là 1 WC()->cart->add_to_cart( $product_id ); //thêm vào giở hàng với số lượng chỉ định WC()->cart->add_to_cart( $product_id, $quantity ); //thêm sản phẩm biến thể vào giỏ hàng WC()->cart->add_to_cart( $product_id, $quantity, $variation_id ); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
WC()->cart->add_to_cart( 14, 1, 0, array(), array( 'mrtung_custom_price' => 1000 ) ); add_action( 'woocommerce_before_calculate_totals', 'custom_price_refresh' ); function custom_price_refresh( $cart_object ) { foreach ( $cart_object->get_cart() as $item ) { if( array_key_exists( 'mrtung_custom_price', $item ) ) { $item[ 'data' ]->set_price( $item[ 'mrtung_custom_price' ] ); } } } |
1 2 3 4 5 6 |
$product_id = 15; $product_cart_id = WC()->cart->generate_cart_id( $product_id ); if( ! WC()->cart->find_product_in_cart( $product_cart_id ) ){ // Yep, the product with ID 15 is NOT in the cart, let's add it then! WC()->cart->add_to_cart( $product_id ); } |
1 |
WC()->cart->empty_cart(); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
function customAddToCart( product_id, quantity = 1 ) { // let's check is add-to-cart.min.js is enqueued and parameters are presented if ( 'undefined' === typeof wc_add_to_cart_params ) { return false; } jQuery.post( wc_add_to_cart_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'add_to_cart' ), { product_id: product_id, quantity: quantity, // quantity is hardcoced her }, function( response ) { if ( ! response ) { return; } // redirect is optional and it depends on what is set in WooCommerce configuration if ( response.error && response.product_url ) { window.location = response.product_url; return; } if ( 'yes' === wc_add_to_cart_params.cart_redirect_after_add ) { window.location = wc_add_to_cart_params.cart_url; return; } // refresh cart fragments etc jQuery( document.body ).trigger( 'added_to_cart', [ response.fragments, response.cart_hash ] ); } ); } //cách dùng customAddToCart( 500, 1 ); |
1 2 3 |
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) { //dữ liệu item cart tại đây } |
1 2 |
WC()->cart->cart_contents[$_POST['cart_item_key']]['main_domain_for_hosting'] = $_POST['main_domain_for_hosting']; WC()->cart->set_session(); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
WC()->cart->is_empty() WC()->cart->needs_payment() WC()->cart->show_shipping() WC()->cart->needs_shipping() WC()->cart->needs_shipping_address() WC()->cart->display_prices_including_tax() // Get $cart totals WC()->cart->get_cart_contents_count(); WC()->cart->get_cart_subtotal(); WC()->cart->subtotal_ex_tax; WC()->cart->subtotal; WC()->cart->get_displayed_subtotal(); WC()->cart->get_taxes_total(); WC()->cart->get_shipping_total(); WC()->cart->get_coupons(); WC()->cart->get_coupon_discount_amount( 'coupon_code' ); WC()->cart->get_fees(); WC()->cart->get_discount_total(); WC()->cart->get_total(); WC()->cart->total; WC()->cart->get_tax_totals(); WC()->cart->get_cart_contents_tax(); WC()->cart->get_fee_tax(); WC()->cart->get_discount_tax(); WC()->cart->get_shipping_total(); WC()->cart->get_shipping_taxes(); // Loop over $cart items foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) { $product = $cart_item['data']; $product_id = $cart_item['product_id']; $quantity = $cart_item['quantity']; $price = WC()->cart->get_product_price( $product ); $subtotal = WC()->cart->get_product_subtotal( $product, $cart_item['quantity'] ); $link = $product->get_permalink( $cart_item ); // Anything related to $product, check $product tutorial $attributes = $product->get_attributes(); $whatever_attribute = $product->get_attribute( 'whatever' ); $whatever_attribute_tax = $product->get_attribute( 'pa_whatever' ); $any_attribute = $cart_item['variation']['attribute_whatever']; $meta = wc_get_formatted_cart_item_data( $cart_item ); } // Get $cart customer billing / shipping WC()->cart->get_customer()->get_billing_first_name(); WC()->cart->get_customer()->get_billing_last_name(); WC()->cart->get_customer()->get_billing_company(); WC()->cart->get_customer()->get_billing_email(); WC()->cart->get_customer()->get_billing_phone(); WC()->cart->get_customer()->get_billing_country(); WC()->cart->get_customer()->get_billing_state(); WC()->cart->get_customer()->get_billing_postcode(); WC()->cart->get_customer()->get_billing_city(); WC()->cart->get_customer()->get_billing_address(); WC()->cart->get_customer()->get_billing_address_2(); WC()->cart->get_customer()->get_shipping_first_name(); WC()->cart->get_customer()->get_shipping_last_name(); WC()->cart->get_customer()->get_shipping_company(); WC()->cart->get_customer()->get_shipping_country(); WC()->cart->get_customer()->get_shipping_state(); WC()->cart->get_customer()->get_shipping_postcode(); WC()->cart->get_customer()->get_shipping_city(); WC()->cart->get_customer()->get_shipping_address(); WC()->cart->get_customer()->get_shipping_address_2(); // Other stuff WC()->cart->get_cross_sells(); WC()->cart->get_cart_item_tax_classes_for_shipping(); WC()->cart->get_cart_hash(); WC()->cart->get_customer(); |
Mọi sự sao chép xin ghi rõ nguồn là fcwordpress.net
Chuyên trang về wordpress: hướng dẫn thiết kế theme, plugin, thủ thuật wordpress