Woocommerce javascript события (events)

13

Еще одна шпаргалка вновь :). Теперь по js событиям, которые можно использовать в определенных модулях woocommerce. Дело в том, что как бы ни хотелось или как бы ни не хотелось влезать скрипты checkout.js, cart.js, порой необходимо дописать некоторую логику, которая возникает после обновления корзины или обновления страницы checkout и тд.

//Woocommerce Checkout JS events
$( document.body ).trigger( 'init_checkout' );
$( document.body ).trigger( 'payment_method_selected' );
$( document.body ).trigger( 'update_checkout' );
$( document.body ).trigger( 'updated_checkout' );
$( document.body ).trigger( 'checkout_error' );
$( document.body ).trigger( 'applied_coupon_in_checkout' );
$( document.body ).trigger( 'removed_coupon_in_checkout' );

//Woocommerce cart page JS events
$( document.body ).trigger( 'wc_cart_emptied' );
$( document.body ).trigger( 'update_checkout' );
$( document.body ).trigger( 'updated_wc_div' );
$( document.body ).trigger( 'updated_cart_totals' );
$( document.body ).trigger( 'country_to_state_changed' );
$( document.body ).trigger( 'updated_shipping_method' );
$( document.body ).trigger( 'applied_coupon', [ coupon_code ] );
$( document.body ).trigger( 'removed_coupon', [ coupon ] );

//Woocommerce Add to cart JS events
$( document.body ).trigger( 'adding_to_cart', [ $thisbutton, data ] );
$( document.body ).trigger( 'added_to_cart', [ response.fragments, response.cart_hash, $thisbutton ] );
$( document.body ).trigger( 'removed_from_cart', [ response.fragments, response.cart_hash, $thisbutton ] );
$( document.body ).trigger( 'wc_cart_button_updated', [ $button ] );
$( document.body ).trigger( 'cart_page_refreshed' );
$( document.body ).trigger( 'cart_totals_refreshed' );
$( document.body ).trigger( 'wc_fragments_loaded' );

$( document.body ).trigger( 'init_add_payment_method' );

//Использование
jQuery('<event_target>').on('<event_name>', function(){
    console.log('<event_name> triggered');
});

//Пример
jQuery('body').on('init_checkout', function(){
    console.log('init_checkout triggered');
    // now.do.whatever();
});



0
 

Woocommerce шпараглка по корзине. Get cart data

31

Еще одна шпаргалочка, только уже по корзине

// $cart conditionals (if)
 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();
0
 

Woocommerce шпаргалка по Orders. Get orders data.

31

В некоторых задачах требуется получить объект заказа.
Вот некоторая шпаргалка.

// Get Order ID and Key
 $order->get_id();
 $order->get_order_key();
 // Get Order Totals $0.00
 $order->get_formatted_order_total();
 $order->get_cart_tax();
 $order->get_currency();
 $order->get_discount_tax();
 $order->get_discount_to_display();
 $order->get_discount_total();
 $order->get_fees();
 $order->get_formatted_line_subtotal();
 $order->get_shipping_tax();
 $order->get_shipping_total();
 $order->get_subtotal();
 $order->get_subtotal_to_display();
 $order->get_tax_location();
 $order->get_tax_totals();
 $order->get_taxes();
 $order->get_total();
 $order->get_total_discount();
 $order->get_total_tax();
 $order->get_total_refunded();
 $order->get_total_tax_refunded();
 $order->get_total_shipping_refunded();
 $order->get_item_count_refunded();
 $order->get_total_qty_refunded();
 $order->get_qty_refunded_for_item();
 $order->get_total_refunded_for_item();
 $order->get_tax_refunded_for_item();
 $order->get_total_tax_refunded_by_rate_id();
 $order->get_remaining_refund_amount();
 // Get and Loop Over Order Items
 foreach ( $order->get_items() as $item_id => $item ) {
    $product_id = $item->get_product_id();
    $variation_id = $item->get_variation_id();
    $product = $item->get_product();
    $product_name = $item->get_name();
    $quantity = $item->get_quantity();
    $subtotal = $item->get_subtotal();
    $total = $item->get_total();
    $tax = $item->get_subtotal_tax();
    $taxclass = $item->get_tax_class();
    $taxstat = $item->get_tax_status();
    $allmeta = $item->get_meta_data();
    $somemeta = $item->get_meta( '_whatever', true );
    $product_type = $item->get_type();
 }
 // Other Secondary Items Stuff
 $order->get_items_key();
 $order->get_items_tax_classes();
 $order->get_item_count();
 $order->get_item_total();
 $order->get_downloadable_items();
 $order->get_coupon_codes();
 // Get Order Lines
 $order->get_line_subtotal();
 $order->get_line_tax();
 $order->get_line_total();
 // Get Order Shipping
 $order->get_shipping_method();
 $order->get_shipping_methods();
 $order->get_shipping_to_display();
 // Get Order Dates
 $order->get_date_created();
 $order->get_date_modified();
 $order->get_date_completed();
 $order->get_date_paid();
 // Get Order User, Billing & Shipping Addresses
 $order->get_customer_id();
 $order->get_user_id();
 $order->get_user();
 $order->get_customer_ip_address();
 $order->get_customer_user_agent();
 $order->get_created_via();
 $order->get_customer_note();
 $order->get_address_prop();
 $order->get_billing_first_name();
 $order->get_billing_last_name();
 $order->get_billing_company();
 $order->get_billing_address_1();
 $order->get_billing_address_2();
 $order->get_billing_city();
 $order->get_billing_state();
 $order->get_billing_postcode();
 $order->get_billing_country();
 $order->get_billing_email();
 $order->get_billing_phone();
 $order->get_shipping_first_name();
 $order->get_shipping_last_name();
 $order->get_shipping_company();
 $order->get_shipping_address_1();
 $order->get_shipping_address_2();
 $order->get_shipping_city();
 $order->get_shipping_state();
 $order->get_shipping_postcode();
 $order->get_shipping_country();
 $order->get_address();
 $order->get_shipping_address_map_url();
 $order->get_formatted_billing_full_name();
 $order->get_formatted_shipping_full_name();
 $order->get_formatted_billing_address();
 $order->get_formatted_shipping_address();
 // Get Order Payment Details
 $order->get_payment_method();
 $order->get_payment_method_title();
 $order->get_transaction_id();
 // Get Order URLs
 $order->get_checkout_payment_url();
 $order->get_checkout_order_received_url();
 $order->get_cancel_order_url();
 $order->get_cancel_order_url_raw();
 $order->get_cancel_endpoint();
 $order->get_view_order_url();
 $order->get_edit_order_url();
 // Get Order Status
 $order->get_status();
 // Get Thank You Page URL
 $order->get_checkout_order_received_url();
0
 

Woocommerce. Как получить данныe товаров (заголовок, описание, картинку, цену и тд).

337

Для этого нам потребуется woocommerce класс WC_Product{}(ссылка на документацию), а точнее его объект с использованием id продукта.

$wc_product = wc_get_product( $post->ID );

А дальше стоит переместиться на страницу документации и ознакомиться с методами. Я приведу пример вывода базовой информации.

Используя объект $wc_product, выводим заголовок (title):

 $title = $wc_product->get_name()

ссылку (get_permalink):

 $permalink = $wc_product->get_permalink()

короткое описание (short_description):

 $short_description = $wc_product->get_short_description()

цена (price):

 $price_html = $wc_product->get_price_html()
+1