Skip to content

Commit 015be30

Browse files
committed
First Commit
0 parents  commit 015be30

File tree

64 files changed

+1649
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+1649
-0
lines changed

Diff for: readme.md

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# SkyVerge WooCommerce Plugins
2+
3+
Welcome to the `wc-plugins-snippets` repository! This repository stores code snippets related to [SkyVerge WooCommerce plugins](https://www.skyverge.com/shop/) to modify or add onto plugin behavior.
4+
5+
Code snippets are organized by plugin (folder), and each plugin's folder includes one file per snippet or code example. The file name describes the snippet functionality.
6+
7+
Some larger plugins, such as Memberships, may have sub-folders by category / snippet type to make it easier to find relevant snippets.
8+
9+
**Please ensure you know how to [add custom code to your website](https://www.skyverge.com/blog/add-custom-code-to-wordpress/) if you use any of these code snippets**. These are provided publicly as a courtesy, and are **not supported**, so you should be familiar with adding code to your site to use them.
10+
11+
## Issues and Contributions
12+
13+
If you notice a snippet is outdated, please submit an issue so we can update it, or we welcome pull requests :smile_cat:
14+
15+
We also welcome new snippets! Please first check to see if a similar snippet exists, then submit a PR adding your snippet to the appropriate plugin folder(s), and ensure the name of the file is descriptive of what the snippet does. Including a brief description at the top snippet file is also helpful.
16+
17+
**PR Guidelines**
18+
19+
- Please ensure that your PR follows WordPress coding standards and conventions.
20+
21+
- All snippets should be PHP 5.2 compatible and use best WooCommerce practices. Try to maintain compatibility at least as far back as WooCommerce 2.3, or otherwise note requirements in the snippet.
22+
23+
- Please use "poor man's namespacing" for function names by using this format:
24+
25+
```
26+
function sv_wc_{plugin_name}_my_function() {
27+
// the function code
28+
}
29+
```
30+
31+
- Files should include one "snippet" or example per file.
32+
33+
- If your snippet relates to multiple plugins, put it in both folders so it's easy to find.
34+
35+
## Licensing
36+
37+
All snippets / code in this repository is **GPL licensed** (GNU General Public License v3.0). If you plan to contribute your own snippets, please note that they will adopt this license.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
/**
3+
* Force all notices to open links in a new tab
4+
*/
5+
function sv_wc_cart_notices_set_target_blank( $notice_html ) {
6+
$notice_html = str_replace( '<a', '<a target="blank"', $notice_html );
7+
return $notice_html;
8+
}
9+
add_filter( 'woocommerce_cart_notice_minimum_amount_notice', 'sv_wc_cart_notices_set_target_blank' );
10+
add_filter( 'woocommerce_cart_notice_deadline_notice', 'sv_wc_cart_notices_set_target_blank' );
11+
add_filter( 'woocommerce_cart_notice_referer_notice', 'sv_wc_cart_notices_set_target_blank' );
12+
add_filter( 'woocommerce_cart_notice_products_notice', 'sv_wc_cart_notices_set_target_blank' );
13+
add_filter( 'woocommerce_cart_notice_categories_notice', 'sv_wc_cart_notices_set_target_blank' );
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
/**
3+
* Removes any Cart Notices from the checkout page
4+
*/
5+
function sv_wc_cart_notices_remove_on_checkout() {
6+
if ( function_exists( 'wc_cart_notices' ) ) {
7+
remove_action( 'woocommerce_before_checkout_form', array( wc_cart_notices(), 'add_cart_notice' ) );
8+
}
9+
}
10+
add_action( 'init', 'sv_wc_cart_notices_remove_on_checkout' );
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/**
3+
* Add attributes to Checkout Add-ons fields at checkout
4+
* Example: add maximum character counts, placeholders, and descriptions for add-ons
5+
*
6+
* @param array $checkout_fields the fields in the WooCommerce checkout
7+
* @return array $checkout_fields the updated fields array
8+
*/
9+
function sv_wc_checkout_add_ons_add_attributes( $checkout_fields ) {
10+
11+
$add_on_id = 3; // change 3 to your add-on id
12+
13+
// is our checkout add-on a field?
14+
if ( isset( $checkout_fields['add_ons'][ 'wc_checkout_add_ons_' . $add_on_id ] ) ) {
15+
16+
// Adds a maximum character length + description to this add on
17+
$checkout_fields['add_ons'][ 'wc_checkout_add_ons_' . $add_on_id ]['maxlength'] = "300";
18+
$checkout_fields['add_ons'][ 'wc_checkout_add_ons_' . $add_on_id ]['description'] = "Please add no more than 300 characters.";
19+
20+
}
21+
22+
// repeat for each add-on
23+
$add_on_id = 7; // change 7 to your add-on id
24+
25+
if ( isset( $checkout_fields['add_ons'][ 'wc_checkout_add_ons_' . $add_on_id ] ) ) {
26+
27+
// Add a maximum length and placeholder
28+
$checkout_fields['add_ons'][ 'wc_checkout_add_ons_' . $add_on_id ]['maxlength'] = "140";
29+
$checkout_fields['add_ons'][ 'wc_checkout_add_ons_' . $add_on_id ]['placeholder'] = "My custom placeholder";
30+
31+
}
32+
33+
return $checkout_fields;
34+
}
35+
add_filter( 'woocommerce_checkout_fields', 'sv_wc_checkout_add_ons_add_attributes', 20 );
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
/**
3+
* Conditionally show gift add-ons only if shipping address differs from billing
4+
* Note: be careful not to require fields if you do this!
5+
* they may be hidden and the customer can't complete checkout
6+
*/
7+
function sv_wc_checkout_add_ons_conditionally_show_gift_add_on() {
8+
9+
wc_enqueue_js( "
10+
$( 'input[name=ship_to_different_address]' ).change( function () {
11+
12+
if ( $( this ).is( ':checked' ) ) {
13+
14+
// show the gift checkout add-on -- replace '2' with the id of your add-on
15+
$( '#wc_checkout_add_ons_2_field' ).show();
16+
17+
} else {
18+
19+
// hide the gift checkout add-on -- replace '2' with the id of your add-on
20+
$( '#wc_checkout_add_ons_2_field' ).hide();
21+
22+
}
23+
24+
} ).change();
25+
" );
26+
}
27+
add_action( 'wp_enqueue_scripts', 'sv_wc_checkout_add_ons_conditionally_show_gift_add_on' );
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
/**
3+
* Remove add-ons if _all products_ in the cart are in a given category
4+
*
5+
* Example: remove add-ons if all products in cart are in the "Gift box" category
6+
* (and thus are gift-wrapped already)
7+
*/
8+
function sv_wc_checkout_add_ons_remove_add_ons_only_giftboxes() {
9+
10+
// bail if Checkout Add-ons isn't active
11+
if ( ! function_exists( 'wc_checkout_add_ons' ) ) {
12+
return;
13+
}
14+
15+
// holds checks for all products in cart to see if they're in our category
16+
$category_checks = array();
17+
18+
// check each cart item for our category
19+
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
20+
21+
$product = $cart_item['data'];
22+
23+
// replace 'gift_box' with your category's slug
24+
$product_in_cat = has_term( 'gift_box', 'product_cat', $product->id ) ? true : false;
25+
26+
array_push( $category_checks, $product_in_cat );
27+
}
28+
29+
// get the position of checkout add-ons so we can remove them properly
30+
$position = get_option( 'wc_checkout_add_ons_position', 'woocommerce_checkout_after_customer_details' );
31+
32+
// if all items are in this category, remove the checkout add-ons
33+
if ( ! in_array( false, $category_checks ) && $position ) {
34+
remove_action( $position, array( wc_checkout_add_ons()->frontend, 'render_add_ons' ), 20 );
35+
}
36+
37+
}
38+
add_action( 'woocommerce_before_checkout_form', 'sv_wc_checkout_add_ons_remove_add_ons_only_giftboxes' );
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/**
3+
* Remove add-ons if _any product_ in the cart is in a given category
4+
* Example: remove add-ons if any product is in the "Gift box" category
5+
*/
6+
function sv_wc_checkout_add_ons_remove_add_ons_for_giftboxes() {
7+
8+
// bail if Checkout Add-ons isn't active
9+
if ( ! function_exists( 'wc_checkout_add_ons' ) ) {
10+
return;
11+
}
12+
13+
// set a flag
14+
$cat_check = false;
15+
16+
// check each cart item for our category
17+
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
18+
19+
$product = $cart_item['data'];
20+
$product_in_cat = false;
21+
22+
// replace 'gift_box' with your category's slug
23+
if ( has_term( 'gift_box', 'product_cat', $product->id ) ) {
24+
// flag the cart as containing this category
25+
$cat_check = true;
26+
}
27+
}
28+
29+
// if a product in the cart is in our category, remove the add-ons
30+
if ( $cat_check ) {
31+
remove_action( 'woocommerce_checkout_after_customer_details', array( wc_checkout_add_ons()->frontend, 'render_add_ons' ) );
32+
}
33+
34+
}
35+
add_action( 'woocommerce_before_checkout_form', 'sv_wc_checkout_add_ons_remove_add_ons_for_giftboxes' );
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
/**
3+
* Add line item meta to the Order CSV Export in Default format
4+
* Example: add weight to the item meta data
5+
*/
6+
7+
8+
/**
9+
* Add weight to line item data
10+
*
11+
* @param array $line_item the original line item data
12+
* @param array $item the item's order data
13+
* @param object $product the \WC_Product object for the line
14+
* @param object $order the \WC_Order object being exported
15+
* @return array the updated line item data
16+
*/
17+
function sv_wc_csv_export_add_weight_to_line_item( $line_item, $item, $product, $order ) {
18+
19+
$line_item['weight'] = $product->get_weight();
20+
return $line_item;
21+
22+
}
23+
add_filter( 'wc_customer_order_csv_export_order_line_item', 'sv_wc_csv_export_add_weight_to_line_item', 10, 4 );
24+
25+
26+
/**
27+
* Add weight as line item meta in CSV export CSV Import format
28+
*
29+
* @param array $order_data the original order data
30+
* @param object $order the WC_Order object exported
31+
* @return array the updated order data
32+
*/
33+
function sv_wc_csv_export_add_weight_to_csv_export_import_format( $order_data, $order ) {
34+
35+
$count = 1;
36+
37+
// add line items
38+
foreach ( $order->get_items() as $item ) {
39+
40+
$product = $order->get_product_from_item( $item );
41+
42+
if ( ! is_object( $product ) ) {
43+
$product = new WC_Product( 0 );
44+
}
45+
46+
if ( $weight = $product->get_weight() ) {
47+
$order_data[ "order_item_{$count}" ] .= '|weight: ' . $weight;
48+
}
49+
50+
$count++;
51+
}
52+
53+
return $order_data;
54+
}
55+
add_filter( 'wc_customer_order_csv_export_order_row', 'sv_wc_csv_export_add_weight_to_csv_export_import_format', 20, 2 );
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
/**
3+
* Show the product price in the Default - One Row per Item order export format
4+
* Adds a column for item price
5+
*/
6+
7+
8+
/**
9+
* Add the product price to the individual line item entry
10+
*
11+
* @param array $line_item the original line item data
12+
* @param array $item WC order item data
13+
* @param WC_Product $product the product
14+
* @return array updated line item data
15+
*/
16+
function sv_wc_csv_export_order_line_item_price( $line_item, $item, $product ) {
17+
18+
$line_item['price'] = $product->get_price();
19+
20+
return $line_item;
21+
}
22+
add_filter( 'wc_customer_order_csv_export_order_line_item', 'sv_wc_csv_export_order_line_item_price', 10, 3 );
23+
24+
25+
/**
26+
* Add `item_price` column to the Default - One Row per Item export format
27+
*
28+
* @param array $column_headers the original column headers
29+
* @param WC_Customer_Order_CSV_Export_Generator $csv_generator the generator instance
30+
* @return array the updated column headers
31+
*/
32+
function sv_wc_csv_export_modify_column_headers_item_price( $column_headers, $csv_generator ) {
33+
34+
if ( 'default_one_row_per_item' === $csv_generator->order_format ) {
35+
36+
$new_headers = array(
37+
'item_price' => 'item_price',
38+
);
39+
40+
$column_headers = array_merge( $column_headers, $new_headers );
41+
}
42+
43+
return $column_headers;
44+
}
45+
add_filter( 'wc_customer_order_csv_export_order_headers', 'sv_wc_csv_export_modify_column_headers_item_price', 10, 2 );
46+
47+
48+
/**
49+
* Add the item_price column data for the Default - One Row per Item format
50+
*
51+
* @param array $order_data the original order data
52+
* @param array $item the item for this row
53+
* @return array the updated order data
54+
*/
55+
function sv_wc_csv_export_order_row_one_row_per_item_price( $order_data, $item ) {
56+
57+
$order_data['item_price'] = $item['price'];
58+
59+
return $order_data;
60+
}
61+
add_filter( 'wc_customer_order_csv_export_order_row_one_row_per_item', 'sv_wc_csv_export_order_row_one_row_per_item_price', 10, 2 );

0 commit comments

Comments
 (0)