Snippets Collections
// Change the "Add to Cart" text to "Buy Now"
2
add_filter( 'woocommerce_product_single_add_to_cart_text', 'codewp_custom_cart_button_text' );
3
add_filter( 'woocommerce_product_add_to_cart_text', 'codewp_custom_cart_button_text' );
4
function codewp_custom_cart_button_text() {
5
    return __( 'Purchase', 'codewp' );
6
}
7
​
8
// Redirect users to the checkout page after clicking the "Buy Now" button
9
add_filter( 'woocommerce_add_to_cart_redirect', 'codewp_redirect_checkout_add_cart' );
10
function codewp_redirect_checkout_add_cart() {
11
    global $woocommerce;
12
    $checkout_url = wc_get_checkout_url();
13
    return $checkout_url;
14
}
15
​
16
// Remove the "Add to Cart" button and functionality
17
remove_action('woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10);
18
remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30);
19
​
20
// Limit to one product in the cart
21
function codewp_restrict_cart_to_one_item($cart_item_data, $product_id) {
22
    global $woocommerce;
23
​
24
    // If product is being added from the shop page, redirect to checkout
25
    if (is_shop() && isset($_REQUEST['add-to-cart'])) {
26
        $checkout_url = wc_get_checkout_url();
27
        wp_safe_redirect($checkout_url);
28
        exit;
29
    }
30
​
31
    // Empty the cart before adding a new item to ensure only one product is in the cart
32
    $woocommerce->cart->empty_cart();
33
    return $cart_item_data;
34
}
35
add_filter('woocommerce_add_cart_item_data', 'codewp_restrict_cart_to_one_item', 10, 2);
36
​
37
// Function.php
add_action( 'woocommerce_product_options_pricing', 'add_international_to_products' );      
function add_international_to_products() {          
    woocommerce_wp_text_input( array( 
        'id' => 'international_price', 
        'class' => 'short wc_input_price', 
        'label' => __( 'International Price', 'woocommerce' ) . ' (' . get_woocommerce_currency_symbol() . ')',
        'data_type' => 'price', 
    ));      
}

add_action( 'save_post_product', 'save_international' );  
function save_international( $product_id ) {
    global $typenow;
    if ( 'product' === $typenow ) {
        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
        if ( isset( $_POST['international_price'] ) ) {
            update_post_meta( $product_id, 'international_price', $_POST['international_price'] );
        }
    }
}
// Display Code hook throw
add_action( 'woocommerce_single_product_summary', 'display_international', 9 );  
function display_international() {
    global $product;
    if ( $product->get_type() <> 'variable' && $rrp = get_post_meta( $product->get_id(), 'international_price', true ) ) {
        echo '<div class="woocommerce_international_price">';
		echo '<span>' . wc_price( $rrp ) . '</span>';
        _e( '<p>for International Customer</p> ', 'woocommerce' );       
        echo '</div>';
    }
}

	<?php
// 	if ( $sent_to_admin ) {
// 		$before = '<a class="link" href="' . esc_url( $order->get_edit_order_url() ) . '">';
// 		$after  = '</a>';
// 	} else {
// 		$before = '';
// 		$after  = '';
// 	}
	
	$before = '<a class="link" href="' . esc_url( $order->get_edit_order_url() ) . '">';
	$after  = '</a>';
	
add_filter( 'woocommerce_package_rates', 'unset_shipping_when_free_is_available_all_zones', 9999, 2 );

function unset_shipping_when_free_is_available_all_zones( $rates, $package ) {

    $all_free_rates = array();

    foreach ( $rates as $rate_id => $rate ) {
        if ( 'free_shipping' === $rate->method_id ) {
            $all_free_rates[ $rate_id ] = $rate;
            break;
        }
    }

    if ( empty( $all_free_rates )) {
        return $rates;
    } else {
        return $all_free_rates;
    } 
}
$( ".variations_form" ).on( "woocommerce_variation_select_change", function () {
    // Fires whenever variation selects are changed
} );

$( ".single_variation_wrap" ).on( "show_variation", function ( event, variation ) {
    // Fired when the user selects all the required dropdowns / attributes
    // and a final variation is selected / shown
} );
    $_product = wc_get_product( $post_id );

    if( $_product->is_type( 'simple' ) ) {
    // do stuff for simple products
    } else {
    // do stuff for everything else
    }
add_filter( 'woocommerce_checkout_fields', 'alter_woocommerce_checkout_fields' );

function alter_woocommerce_checkout_fields( $fields ) {

  unset( $fields['billing']['billing_company'] ); // remove the option to enter in a company
  unset( $fields['billing']['billing_address_1'] ); // remove the first line of the address
  unset( $fields['billing']['billing_address_2'] ); // remove the second line of the address
  unset( $fields['billing']['billing_city'] ); // remove the billing city
  unset( $fields['billing']['billing_postcode'] ); // remove the ZIP / postal code field
  unset( $fields['billing']['billing_country'] ); // remove the billing country
  unset( $fields['billing']['billing_state'] ); // remove the billing state
  unset( $fields['billing']['billing_phone'] ); // remove the option to enter in a billing phone number
  unset( $fields['shipping']['shipping_first_name'] );

  unset( $fields['shipping']['shipping_last_name'] );

  unset( $fields['shipping']['shipping_company'] );

  unset( $fields['shipping']['shipping_address_1'] );

  unset( $fields['shipping']['shipping_address_2'] );

  unset( $fields['shipping']['shipping_city'] );

  unset( $fields['shipping']['shipping_postcode'] );

  unset( $fields['shipping']['shipping_country'] );

  unset( $fields['shipping']['shipping_state'] );

  unset( $fields['account']['account_username'] ); // removing this or the two fields below would prevent users from creating an account, which you can do via normal WordPress + Woocommerce capabilities anyway
  unset( $fields['account']['account_password'] );

  unset( $fields['account']['account_password-2'] );

  unset( $fields['order']['order_comments'] ); // removes the order comments / notes field

  return $fields;
}

;
// Get visible variations
$product = wc_get_product($product_id);
$variations = $product->get_available_variations();
$variations_id = wp_list_pluck( $variations, 'variation_id' );

// OR Get all varations regardless of visibility
$product = wc_get_product($product_id);
$current_products = $product->get_children();
sudo lsof -i:80; sudo lsof -tnP -i:80 | xargs -n 1 ps -p
kill <PID target>
<?php
/**
 * IMPORTANT!!!
 * Before anything else, make sure that you created a custom field for category and has a field called 'custom_order'
 * Then on each catogory, fill out the custom_order field.
 */

/**
* Separate file
* Add a $is_oos_last variable before the woof shortcode then set it to false after the shortcode
*/
?>

<?php $is_oos_last = true; ?>
 <?php echo do_shortcode( '[woof_products per_page="12" columns="3" is_ajax="1"]' ); ?>
<?php $is_oos_last = false; ?>

<?php
/**
* functions.php
* Modify the query to push out of stock at the bottom of the list
*/
add_filter('posts_clauses', 'tbd_order_by_stock_status', 9999);
function tbd_order_by_stock_status($posts_clauses)
{
    global $wpdb;
    global $is_oos_last;

    if ($is_oos_last == 1 || $_GET['is_oos_last'] == 1 || is_woocommerce() && (is_shop() || is_product_category() || is_product_tag() )) {
        $posts_clauses['join'] .= " INNER JOIN $wpdb->postmeta istockstatus ON ($wpdb->posts.ID = istockstatus.post_id) ";
        $posts_clauses['join'] .= " INNER JOIN {$wpdb->prefix}term_relationships term_rel ON {$wpdb->posts}.ID = term_rel.object_id INNER JOIN {$wpdb->prefix}termmeta termmeta1 ON termmeta1.term_id=term_rel.term_taxonomy_id ";
        $posts_clauses['orderby'] = " termmeta1.meta_value ASC, istockstatus.meta_value ASC, " . $posts_clauses['orderby'];
        $posts_clauses['where'] = " AND term_rel.term_taxonomy_id IN (SELECT term_id FROM {$wpdb->prefix}term_taxonomy WHERE taxonomy='product_cat') AND termmeta1.meta_key = 'custom_order' AND istockstatus.meta_key = '_stock_status' AND istockstatus.meta_value <> '' " . $posts_clauses['where'];
    }
    return $posts_clauses;
}
add_filter('woof_draw_products_get_args', 'tbd_woof_draw_products_get_args', 9999 );
function tbd_woof_draw_products_get_args( $args ) {
    $args['is_oos_last'] = 1;
    return $args;
}
<?php

/**
* Separate file
* Add a $is_oos_last variable before the woof shortcode then set it to false after the shortcode
*/
?>

<?php $is_oos_last = true; ?>
 <?php echo do_shortcode( '[woof_products per_page="12" columns="3" is_ajax="1"]' ); ?>
<?php $is_oos_last = false; ?>

<?php


/**
* functions.php
* Modify the query to push out of stock at the bottom of the list
*/
add_filter('posts_clauses', 'tbd_order_by_stock_status', 9999);
function tbd_order_by_stock_status($posts_clauses)
{
    global $wpdb;
    global $is_oos_last;

    if ($is_oos_last == 1 || $_GET['is_oos_last'] == 1 || is_woocommerce() && (is_shop() || is_product_category() || is_product_tag() )) {
        $posts_clauses['join'] .= " INNER JOIN $wpdb->postmeta istockstatus ON ($wpdb->posts.ID = istockstatus.post_id) ";
        $posts_clauses['orderby'] = " istockstatus.meta_value ASC, " . $posts_clauses['orderby'];
        $posts_clauses['where'] = " istockstatus.meta_key = '_stock_status' AND istockstatus.meta_value <> '' " . $posts_clauses['where'];
    }
    return $posts_clauses;
}
add_filter('woof_draw_products_get_args', 'tbd_woof_draw_products_get_args', 9999 );
function tbd_woof_draw_products_get_args( $args ) {
    $args['is_oos_last'] = 1;
    return $args;
}
function b2b_redirect_if_product_is_not_for_user() {
    if ( is_product() ) {
        $allowed_user = get_post_meta( get_the_ID(), '_allowed_user', true );
        if ( $allowed_user != get_current_user_id() ) {
            wp_redirect( site_url( '/shop' ), 301 );
        }
    }
}

add_action('template_redirect', 'b2b_redirect_if_product_is_not_for_user');
<?php

add_filter( 'woocommerce_product_query_meta_query', 'show_only_products_with_specific_metakey', 10, 2 );
function show_only_products_with_specific_metakey( $meta_query, $query ) {
    // Only on shop pages
    if( ! is_shop() ) return $meta_query;

    $meta_query[] = array(
        'key'     => '_allowed_user',
        'value'   => get_current_user_id(),
    );
    return $meta_query;
}
<?php

$args = array(
  'role'    => 'customer',
  'orderby' => 'user_nicename',   
  'order'   => 'ASC'
);
$users = get_users( $args );

echo '<ul>';
foreach ( $users as $user ) {
	echo '<li>' . esc_html( $user->display_name ) . '[' . esc_html( $user->user_email ) . ']</li>';
}
echo '</ul>';
<?php

add_filter( 'woocommerce_product_data_tabs', 'b2b_add_user_product_per_user_tab' ); 
function b2b_add_user_product_per_user_tab( $tabs ) {
    $tabs['product-per-user'] = array( 
        'label' => __( 'Assign User', 'woocommerce' ), 
        'target' => 'product_per_user', 
        'class' => array( 'show_if_simple', 'show_if_variable' ), 
    ); 
    return $tabs; 
}

add_action('woocommerce_product_data_panels', 'b2b_product_per_user_panel');
function b2b_product_per_user_panel() {
    global $post;
    // Note the 'id' attribute needs to match the 'target' parameter set above
	?>
    <div id='product_per_user' class='panel woocommerce_options_panel'>
        <?php $redeemable_stores = (array) get_post_meta( $post->ID, '_redeem_in_stores', true );?>
        <p class='form-field _redeem_in_stores'>
            <label for='_redeem_in_stores'><?php _e( 'Redeemable in stores', 'woocommerce' ); ?></label>
            <select name='_redeem_in_stores[]' class='wc-enhanced-select' multiple='multiple' style='width: 80%;'>
                <option <?php selected( in_array( 'AL', $redeemable_stores ) ); ?> value='AL'>Alabama</option>
                <option <?php selected( in_array( 'NY', $redeemable_stores ) ); ?> value='NY'>New York</option>
                <option <?php selected( in_array( 'TX', $redeemable_stores ) ); ?> value='TX'>Texas</option>
                <option <?php selected( in_array( 'WY', $redeemable_stores ) ); ?> value='WY'>Wyoming</option>
            </select>
            <img class='help_tip' data-tip="<?php _e( 'Select the stores where this gift card is redeemable.', 'woocommerce' ); ?>" src='<?php echo esc_url( WC()->plugin_url() ); ?>/assets/images/help.png' height='16' width='16'>
        </p>
    </div>
    <?php
}

/**
 * Save the custom fields.
 */
function save_product_per_user_field( $post_id ) {
	if ( isset( $_POST['_redeem_in_stores'] ) ) :
		update_post_meta( $post_id, '_redeem_in_stores', (array) $_POST['_redeem_in_stores'] );
	endif;
}
add_action( 'woocommerce_process_product_meta_simple', 'save_product_per_user_field' );
add_action( 'woocommerce_process_product_meta_variable', 'save_product_per_user_field' );
function custom_price_shortcode_callback( $atts ) {

    $atts = shortcode_atts( array(
        'id' => null,
    ), $atts, 'product_price' );

    $html = '';

    if( intval( $atts['id'] ) > 0 && function_exists( 'wc_get_product' ) ){
        // Get an instance of the WC_Product object
        $product = wc_get_product( intval( $atts['id'] ) );

        // Get the product prices
        $price         = wc_get_price_to_display( $product, array( 'price' => $product->get_price() ) ); // Get the active price
        $regular_price = wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ); // Get the regular price
        $sale_price    = wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) ); // Get the sale price

        // Your price CSS styles
        $style1 = 'style="font-size:40px;color:#e79a99;font-weight:bold;"';
        $style2 = 'style="font-size:25px;color:#e79a99"';

        // Formatting price settings (for the wc_price() function)
        $args = array(
            'ex_tax_label'       => false,
            'currency'           => 'EUR',
            'decimal_separator'  => '.',
            'thousand_separator' => ' ',
            'decimals'           => 2,
            'price_format'       => '%2$s&nbsp;%1$s',
        );

        // Formatting html output
        if( ! empty( $sale_price ) && $sale_price != 0 && $sale_price < $regular_price )
            $html = "<del $style2>" . wc_price( $regular_price, $args ) . "</del> <ins $style1>" . wc_price( $sale_price, $args ) . "</ins>"; // Sale price is set
        else
            $html = "<ins $style1>" . wc_price( $price, $args ) . "</ins>"; // No sale price set
    }
    return $html;
 }
 add_shortcode( 'product_price', 'custom_price_shortcode_callback' );
add_filter( 'woocommerce_email_attachments', 'attach_downloadable_files_to_customer_completed_email', 10, 3 );
function attach_downloadable_files_to_customer_completed_email( $attachments, $email_id, $order ) {
    if( isset( $email_id ) && $email_id === 'customer_completed_order' ){
        // Loop through order items
        foreach( $order->get_items() as $item ) {
            $product = $item->get_product(); // The product Object

            if ( $product->is_downloadable() && ( $downloads = $product->get_downloads() ) ) {
                // Loop through product downloads
                foreach( $downloads as $download ) {
                    $attachments[] = $download->get_file();
                }
            }
        }
    }
    return $attachments;
}
star

Sun Feb 11 2024 21:16:44 GMT+0000 (Coordinated Universal Time)

#woocommerce #buynowbutton
star

Fri Jan 19 2024 06:43:01 GMT+0000 (Coordinated Universal Time)

#woocommerce #wordpress
star

Mon Nov 21 2022 14:44:09 GMT+0000 (Coordinated Universal Time)

#php #woocommerce
star

Thu Jun 02 2022 12:30:50 GMT+0000 (Coordinated Universal Time) https://wordpress.stackexchange.com/questions/348870/how-to-access-custom-order-item-meta-data-from-a-meta-key-in-woocommerce

#wordpress #woocommerce #order #item #meta #key
star

Tue May 31 2022 09:47:25 GMT+0000 (Coordinated Universal Time)

#woocommerce #product #type #php
star

Tue May 31 2022 04:36:39 GMT+0000 (Coordinated Universal Time)

#woocommerce #wordpress
star

Thu Apr 28 2022 09:15:28 GMT+0000 (Coordinated Universal Time) https://squelchdesign.com/web-design-newbury/woocommerce-detecting-order-complete-on-order-completion/

#woocommerce #wordpress #orders #hook
star

Wed Apr 27 2022 11:09:33 GMT+0000 (Coordinated Universal Time)

#woocommerce #ids #variations
star

Sat Apr 23 2022 20:08:48 GMT+0000 (Coordinated Universal Time) https://pluginrepublic.com/add-custom-cart-item-data-in-woocommerce/

#woocommerce #wordpress #filters
star

Wed Mar 16 2022 10:57:46 GMT+0000 (Coordinated Universal Time)

#wordpress #woocommerce #woof
star

Wed Mar 16 2022 05:42:29 GMT+0000 (Coordinated Universal Time)

#wordpress #woocommerce #woof
star

Tue Mar 15 2022 14:15:16 GMT+0000 (Coordinated Universal Time)

#wordpress #php #woocommerce
star

Tue Mar 15 2022 14:03:42 GMT+0000 (Coordinated Universal Time)

#wordpress #php #woocommerce
star

Tue Mar 15 2022 12:48:38 GMT+0000 (Coordinated Universal Time)

#wordpress #php #woocommerce
star

Tue Mar 15 2022 12:44:34 GMT+0000 (Coordinated Universal Time)

#wordpress #php #woocommerce
star

Sat Jan 15 2022 00:14:29 GMT+0000 (Coordinated Universal Time) https://stackoverflow.com/questions/47224791/display-product-prices-with-a-shortcode-by-product-id-in-woocommerce

#php #woocommerce
star

Fri Feb 26 2021 06:08:43 GMT+0000 (Coordinated Universal Time)

#woocommerce #downloads #email

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension