Last active
July 26, 2016 21:41
-
-
Save amallory/1aa942a65ead187ae386f542c799e93c to your computer and use it in GitHub Desktop.
WooCommerce Buy Button Customizer (Product basis)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: WooCommerce Buy Button Customizer | |
Plugin URI: http://amallory.com | |
Description: Customize Button Labels for WooCommerce Buy buttons on product by product basis | |
Version: 0.1.5 | |
Author: AJ Mallory | |
Author URI: http://amallory.com | |
License: GNU General Public License v2.0 | |
License URI: http://www.gnu.org/licenses/gpl-2.0.html | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; // Exit if accessed directly | |
} | |
/** | |
* WBBC_Meta_Box_Buy Button Class. | |
*/ | |
class WBBC_Meta_Box_Buy_Button { | |
static private $buy_button_text; | |
/** | |
* Output the metabox. | |
* | |
* @param WP_Post $post | |
*/ | |
public static function output( $post ) { | |
woocommerce_wp_text_input( | |
array( | |
'id' => 'single_product_buy_button_text', | |
'label' => __( 'Procuct Detail Button Text', 'woocommerce' ), | |
'placeholder' => 'Add to Cart', | |
'description' => __( 'Enter label for the product detail buy button', 'woocommerce' ) | |
) | |
); | |
woocommerce_wp_text_input( | |
array( | |
'id' => 'product_buy_button_text', | |
'label' => __( 'Product List Button Text', 'woocommerce' ), | |
'placeholder' => 'Add to Cart', | |
'description' => __( 'Enter label for the product list buy button', 'woocommerce' ) | |
) | |
); | |
} | |
/** | |
* Save meta box data. | |
* | |
* @param int $post_id | |
* @param WP_Post $post | |
*/ | |
public static function save( $post_id, $post ) { | |
$single_product_buy_button_text = $_POST['single_product_buy_button_text']; | |
if( !empty( $single_product_buy_button_text ) ) | |
update_post_meta( $post_id, 'single_product_buy_button_text', esc_attr( $single_product_buy_button_text ) ); | |
$product_buy_button_text = $_POST['product_buy_button_text']; | |
if( !empty( $single_product_buy_button_text ) ) | |
update_post_meta( $post_id, 'product_buy_button_text', esc_attr( $product_buy_button_text ) ); | |
} | |
/** | |
* Add Metabox for Buy Button lables on woocommerce product screens | |
*/ | |
public static function construct() { | |
add_action( 'woocommerce_process_product_meta', 'WBBC_Meta_Box_Buy_Button::save', 30, 2 ); | |
add_meta_box( 'wbbc-product-buy-button', __( 'Buy Button', 'woocommerce' ), 'WBBC_Meta_Box_Buy_Button::output', 'product', 'side', 'low' ); | |
} | |
/** | |
* Output Buy Button lables single product page | |
*/ | |
public static function wbbc_product_single_add_to_cart_text( $button_text, $product ) { | |
$product_single_button_text = get_post_meta( $product->id, 'single_product_buy_button_text', true ); | |
if( !empty( $product_single_button_text ) ) return esc_attr( $product_single_button_text ); | |
else return $button_text; | |
} | |
/** | |
* Output Buy Button lables product page | |
*/ | |
public static function wbbc_product_add_to_cart_text( $button_text, $product ) { | |
$product_buy_button_text = get_post_meta( $product->id, 'product_buy_button_text', true ); | |
if( !empty( $product_buy_button_text ) ) return esc_attr( $product_buy_button_text ); | |
else return $button_text; | |
} | |
} | |
add_action( 'admin_init', 'WBBC_Meta_Box_Buy_Button::construct', 80 ); | |
add_filter( 'woocommerce_product_single_add_to_cart_text', 'WBBC_Meta_Box_Buy_Button::wbbc_product_single_add_to_cart_text', 30, 2 ); | |
add_filter( 'woocommerce_product_add_to_cart_text', 'WBBC_Meta_Box_Buy_Button::wbbc_product_add_to_cart_text', 10, 2 ); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment