Skip to content

Instantly share code, notes, and snippets.

@digisavvy
Created May 11, 2020 06:34
Show Gist options
  • Select an option

  • Save digisavvy/0ee0c0c4cd64b60c241268c37dfd0838 to your computer and use it in GitHub Desktop.

Select an option

Save digisavvy/0ee0c0c4cd64b60c241268c37dfd0838 to your computer and use it in GitHub Desktop.
Our WooCommerce file for WP Rig.
<?php
/**
* WP_Rig\WP_Rig\WooCommerce\Component class
*
* @package wp_rig
*
* @location
*/
namespace WP_Rig\WP_Rig\WooCommerce;
use WP_Rig\WP_Rig\Component_Interface;
use function WP_Rig\WP_Rig\wp_rig;
use function add_action;
use function add_filter;
use function apply_filters;
use function is_woocommerce_activated;
if ( ! function_exists( 'is_woocommerce_activated' ) ) {
/**
* Class for parsing blocks on a post and rendering a specific block elsewhere.
*/
class Component implements Component_Interface {
/**
* Gets the unique identifier for the theme component.
*
* @return string Component slug.
*/
public function get_slug() : string {
return 'woocommerce';
}
/**
* Adds the action and filter hooks to integrate with WordPress.
*/
public function initialize() {
add_action( 'woocommerce_thankyou', array( $this, 'dgs_woocommerce_auto_complete_order' ) );
add_action( 'after_setup_theme', array( $this, 'dgs_add_woocommerce_support' ) );
add_action( 'event_tickets_after_save_ticket', array( $this, 'tribe_disable_taxes_ticket_product' ), 10, 2 );
add_action( 'init', array( $this, 'remove_product_editor' ) );
add_filter( 'woocommerce_checkout_fields', array( $this, 'dgs_override_checkout_fields' ), 10, 2 );
add_filter( 'woocommerce_single_product_image_thumbnail_html', 'dgs_woocommerce_remove_featured_image', 10, 2 );
add_filter( 'woocommerce_product_tabs', array( $this, 'dgs_remove_product_tabs' ), 98 );
add_filter( 'woocommerce_continue_shopping_redirect', array( $this, 'dgs_changed_woocommerce_continue_shopping_redirect' ), 10, 1 );
add_filter( 'wc_add_to_cart_message_html', array( $this, 'dgs_changed_wc_add_to_cart_message_html' ), 10, 2 );
remove_action( 'woocommerce_before_main_content', 'woocommerce_breadcrumb', 20, 0 );
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 );
}
/**
* Declare WooCommerce Theme Support
*/
public function dgs_add_woocommerce_support() {
add_theme_support( 'woocommerce' );
}
/**
* Auto Complete all WooCommerce orders.
*/
public function dgs_woocommerce_auto_complete_order( $order_id ) {
if ( ! $order_id ) {
return;
}
$order = wc_get_order( $order_id );
$order->update_status( 'completed' );
}
/**
* Disable taxes on tickets
*/
public function tribe_disable_taxes_ticket_product( $post_id, $ticket ) {
update_post_meta( $ticket->ID, '_tax_status', 'none' );
update_post_meta( $ticket->ID, '_tax_class', 'zero-rate' );
}
/**
* Remove Featured Images on Single Product
*/
public function dgs_woocommerce_remove_featured_image( $html, $attachment_id ) {
global $post, $product;
// Get the IDs.
$attachment_ids = $product->get_gallery_image_ids();
// If there are none, go ahead and return early - with the featured image included in the gallery.
if ( ! $attachment_ids ) {
return $html;
}
// Look for the featured image.
$featured_image = get_post_thumbnail_id( $post->ID );
// If there is one, exclude it from the gallery.
if ( $attachment_id == $featured_image ) {
$html = '';
}
return $html;
}
/**
* Remove Description Tab from Single Products
*/
public function dgs_remove_product_tabs( $tabs ) {
unset( $tabs['description'] );
return $tabs;
}
/**
* Remove Billing Fields from Checkout
*/
public function dgs_override_checkout_fields( $fields ) {
unset( $fields['order']['order_comments'] );
return $fields;
}
/**
* Remove Main Product Editor Support
*/
public function remove_product_editor() {
remove_post_type_support( 'product', 'editor' );
}
/**
* Change Continue Shopping Redirect
*/
public function dgs_changed_woocommerce_continue_shopping_redirect( $return_to ) {
$return_to = wc_get_page_permalink( 'checkout' );
return $return_to;
}
/**
* Change Continue Shopping Text
*/
public function dgs_changed_wc_add_to_cart_message_html( $message, $products ) {
if ( strpos( $message, 'Continue shopping' ) !== false ) {
$message = str_replace( 'Continue shopping', 'Proceed to Checkout', $message );
}
return $message;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment