Skip to content

Instantly share code, notes, and snippets.

<?php
/**
* Prevent products being purchased from archive
* @return Boolean
*/
function pewc_filter_is_purchasable( $is_purchasable, $product ) {
if( is_archive() ) {
return false;
}
return $is_purchasable;
@plugin-republic
plugin-republic / get-product-from-subscription.php
Created November 5, 2018 09:59
How to find the products associated with a WooCommerce subscription
/**
* How to get the product associated with a WooCommerce subscription
*/
function ct_checkout_subscription_created( $subscription, $order, $recurring_cart ) {
$id = $subscription->get_id(); // You can use this to set meta in the subscription
// Get the products in the order
$items = $order->get_items();
foreach( $items as $item ) {
$product = $item->get_product();
$product_id = $product->get_id();
<?php
function my_spaced_filter_amenities_icons( $icons ) {
$icons['spaced_cctv'] = array(
'id' => 'spaced_cctv',
'icon' => 'icon-cctv',
'label' => __( 'CCTV', 'spaced' )
);
return $icons;
}
add_filter( 'spaced_filter_amenities_icons', 'my_spaced_filter_amenities_icons' );
@plugin-republic
plugin-republic / woocommerce-payment-gateway-fee.php
Last active November 13, 2023 20:44
Add a charge to WooCommerce when the customer checks out via PayPal: https://pluginrepublic.com/woocommerce-payment-gateway-based-fees/
<?php
/**
* Add a fee when the user checks out with PayPal
*/
function wcfad_apply_payment_gateway_fee() {
$payment_method = WC()->session->get( 'chosen_payment_method' );
// Only apply the fee if the payment gateway is PayPal
// Note that you might need to check this slug, depending on the PayPal gateway you're using
if( $payment_method == 'ppec_paypal' ) {
$label = __( 'PayPal fee', 'wcfad' );
<?php
function prefix_update_existing_cart_item_meta() {
$cart = WC()->cart->cart_contents;
foreach( $cart as $cart_item_id=>$cart_item ) {
$cart_item['new_meta_data'] = 'Your stuff goes here';
WC()->cart->cart_contents[$cart_item_id] = $cart_item;
}
WC()->cart->set_session();
}
@plugin-republic
plugin-republic / prevent-widget.php
Created December 13, 2018 17:11
Programmatically prevent a widget from being rendered
<?php
/**
* This will hide widgets
* @param array $instance The current widget instance's settings.
* @param WP_Widget $this The current widget instance.
* @param array $args An array of default widget arguments.
*/
function wcmo_filter_widget_display_callback( $settings, $widget, $args ) {
$can_access = false; // You need to set your conditions here
if( $can_access || in_array( $widget->name, $whitelist ) ) {
<?php
/**
* All credit to Henrik Jacobsen for this: https://bitbucket.org/snippets/henjak/4eBAqR/product-extras-for-woocommerce-add
*
* Add countries as options to a selectbox.
*
* When creating the selectbox in admin set "countrylist" as the first option and
* this code will insert all the countries.
*
* @uses product-extras-for-woocommerce/inc/functions-single-product.php/pewc_filter_item_start_list
@plugin-republic
plugin-republic / allow-pdf-product-extras.php
Last active November 7, 2019 11:27
Allow the user to upload PDFs with the upload field
<?php
// Add PDFs to list of permitted mime types
function my_prefix_pewc_get_permitted_mimes( $permitted_mimes ) {
// Add PDF to the list of permitted mime types
$permitted_mimes['pdf'] = "application/pdf";
// Remove a mime type - uncomment the line below if you wish to prevent JPGs from being uploaded
// unset( $permitted_mimes['jpg|jpeg|jpe'] );
return $permitted_mimes;
}
add_filter( 'pewc_permitted_mimes', 'my_prefix_pewc_get_permitted_mimes' );
<?php
/**
* Add a text field to each cart item
*/
function prefix_after_cart_item_name( $cart_item, $cart_item_key ) {
$notes = isset( $cart_item['notes'] ) ? $cart_item['notes'] : '';
printf(
'<div><textarea class="%s" id="cart_notes_%s" data-cart-id="%s">%s</textarea></div>',
'prefix-cart-notes',
$cart_item_key,
(function($){
$(document).ready(function(){
$('.prefix-cart-notes').on('change keyup paste',function(){
$('.cart_totals').block({
message: null,
overlayCSS: {
background: '#fff',
opacity: 0.6
}
});