Skip to content

Instantly share code, notes, and snippets.

<?php
/**
* Insert custom css/javascript from custom fields named custom_js and custom_css
* into a blog post or a page
* from http://blue-anvil.com/archives/including-css-javascript-in-wordpress-posts-using-custom-fields/
*/
function sv_add_post_custom_scripts() {
if ( ! is_singular() ) {
return;
@bekarice
bekarice / wc-memberships-sample-member-area-template.php
Last active December 13, 2017 19:34
Sample template for a custom section of the WC Memberships members area
<?php
/**
* WooCommerce Memberships: "Welcome" section
* Custom template added to the "Member Area"
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
/**
* Renders the welcome section for the membership in the my account area.
@bekarice
bekarice / wc-memberships-change-restricted-price.php
Last active February 4, 2019 22:42
WooCommerce Memberships: Change the price for purchase-restricted products that non-members will see
<?php
/**
* Modify membership product pricing display for non-members
* changes pricing display if purchasing is restricted to members;
* active members will see the price instead of a message
*
* @param string $price the WC price HTML
* @return string $price the updated price HTML
*/
function sv_change_member_product_price_display( $price ) {
<?php // only copy opening php if needed
/**
* move social login buttons on account form from "login" to "register
*/
function wc_social_login_move_register_buttons() {
if ( function_exists( 'wc_social_login' ) && ! is_admin() ) {
remove_action( 'woocommerce_login_form_end', array( wc_social_login()->get_frontend_instance(), 'render_social_login_buttons' ) );
add_action( 'woocommerce_register_form_end', array( wc_social_login()->get_frontend_instance(), 'render_social_login_buttons' ) );
}
@bekarice
bekarice / wc-order-xml-export-add-costs.php
Created January 19, 2016 20:57
Adds item costs to order XML Export
<?php
/**
* Adds item costs from Cost of Goods to Order XML Export
*
* @param array $items the data included per line item
* @param \WC_Order $order the order object
* @param array $item line item data from the order
* @return array the updated line item xml data
*/
function sv_add_cost_to_xml_export_items( $items, $order, $item ) {
<?php
/**
* Adds a registration prompt to the WC thank you page
* Only added for guest purchases
*
* @param int $order_id the ID of the order just placed
*/
function sv_thankyou_registration_prompt( $order_id ) {
$my_account_url = get_permalink( get_option( 'woocommerce_myaccount_page_id' ) );
@bekarice
bekarice / wc-my-account-prefill-email.php
Created January 18, 2016 03:27
Sets the email $_POST value so WC will prefill the "email" field in the My Account registration form
<?php
// get the email param if it's set so the registration form can use it
function sv_get_account_email_param() {
$_POST['email'] = isset( $_GET['email'] ) ? urldecode( $_GET['email'] ) : false;
}
add_action( 'woocommerce_register_form_start', 'sv_get_account_email_param' );
@bekarice
bekarice / wc-email-create-account-section.php
Last active December 14, 2019 17:59
Adds a "Create an account" section to WooCommerce guest customer emails
<?php
/**
* Adds a "Create an account?" section to customer emails
* Only added for emails sent to guest purchases
*
* @param \WC_Order $order the order object
* @param bool $sent_to_admin false if emails are sent to customers
* @param bool $plain_text false for HTML emails
*/
function sv_add_email_register_section( $order, $sent_to_admin, $plain_text ) {
@bekarice
bekarice / wc-memberships-remove-my-products-excerpt-column.php
Created January 14, 2016 05:21
WooCommerce Memberships: Remove the "Description" / Excerpt column from "My Products" In the Member Area
<?php
// Only copy opening php if needed
// Removes the product short description / excerpt column from "My Products"
function sv_members_area_products_table_columns( $columns ) {
if ( isset( $columns['membership-product-excerpt'] ) ) {
unset( $columns['membership-product-excerpt'] );
}
return $columns;
}
add_filter('wc_memberships_members_area_my_membership_products_column_names', 'sv_members_area_products_table_columns', 10, 1 );
@bekarice
bekarice / wc-change-price-display-custom-field.php
Created January 4, 2016 00:48
Change WooCommerce price display with a custom field
<?php
// only copy the opening php tag if needed
// Change the shop / product prices if a unit_price is set
function sv_change_product_html( $price_html, $product ) {
$unit_price = get_post_meta( $product->id, 'unit_price', true );
if ( ! empty( $unit_price ) ) {
$price_html = '<span class="amount">' . wc_price( $unit_price ) . ' per kg</span>';
}