Woocommerce - markhowellsmead/helpers GitHub Wiki
Disable all but standard products
add_filter('product_type_selector', function($product_types) {
unset($product_types['variable']);
unset($product_types['grouped']);
unset($product_types['external']);
return $product_types;
});
Add a custom options page to the Woocommerce backend
…use parent_slug => 'woocommerce'
.
Plugins/extensions
- Mailchimp for Woocommerce
- Shipping carriers (live courier costs)
- Multi currency
- Woocommerce PDF Invoices and Packing Slips
Rounding to the nearest 5 Rp. (Switzerland)
add_filter('woocommerce_calculated_total', [$this, 'roundPriceForSwitzerland'], 10, 1);
public function roundPriceForSwitzerland($price)
{
// Rounds total to closest 5 Rp.
return round(($price + 0.000001) * 20) / 20;
}
Snippets
Woocommerce PDF Invoices and Packing Slips
add_filter('wpo_wcpdf_custom_attachment_condition', [$this, 'pdfInvoiceCondition'], 10, 2);
/**
* Only attach a PDF invoice to the email if the payment type 'invoice' is selected
* @param boolean $condition The initial condition state
* @param WP_Post $order The order as a WordPress Post Object
* @return boolean
*/
public function pdfInvoiceCondition($condition, $order)
{
if ($condition) {
$payment_method = get_post_meta($order->id, '_payment_method', true);
if ($payment_method !== 'invoice') {
$condition = false;
}
}
return $condition;
}
Get protected order values
$order = wc_get_order( $order_id );
// 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();
$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 );
$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();
// 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();
Loop through items in order
// Getting an instance of the WC_Order object from a defined ORDER ID
$order = wc_get_order( $order_id );
// Iterating through each "line" items in the order
foreach ($order->get_items() as $item_id => $item_data) {
// Get an instance of corresponding the WC_Product object
$product = $item_data->get_product();
$active_price = $item_data->get_price(); // The product active raw price
$regular_price = $item_data->get_sale_price(); // The product raw sale price
$sale_price = $item_data->get_regular_price(); // The product raw regular price
$product_name = $item_data->get_name(); // Get the item name (product name)
$item_quantity = $item_data->get_quantity(); // Get the item quantity
$item_subtotal = $item_data->get_subtotal(); // Get the item line total non discounted
$item_subto_tax = $item_data->get_subtotal_tax(); // Get the item line total tax non discounted
$item_total = $item_data->get_total(); // Get the item line total discounted
$item_total_tax = $item_data->get_total_tax(); // Get the item line total tax discounted
$item_taxes = $item_data->get_taxes(); // Get the item taxes array
$item_tax_class = $item_data->get_tax_class(); // Get the item tax class
$item_tax_status= $item_data->get_tax_status(); // Get the item tax status
$item_downloads = $item_data->get_item_downloads(); // Get the item downloads
// Displaying this data (to check)
echo 'Product name: '.$product_name.' | Quantity: '.$item_quantity.' | Item total: '. number_format( $item_total, 2 );
}
Payment solutions
- http://mamedev.ch/downloads/datatrans-for-woocommerce/ (not yet trialled 27.6.2018)