Skip to content

Instantly share code, notes, and snippets.

@helgatheviking
Created May 22, 2019 20:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save helgatheviking/88a967bb0da86b6ff415fce10c8c16ea to your computer and use it in GitHub Desktop.
Save helgatheviking/88a967bb0da86b6ff415fce10c8c16ea to your computer and use it in GitHub Desktop.
Add support for Variable NYP products to Product Bundles.
<?php
/*
* Plugin Name: WC Variable NYP + Product Bundles compat
* Plugin URI: https://gist.github.com/helgatheviking/88a967bb0da86b6ff415fce10c8c16ea
* Description: Add support for Variable NYP products to Product Bundles.
* Version: 1.0.0
* Author: Kathy Darling
* Author URI: http://kathyisawesome.com
* Requires at least: 5.0.0
* Tested up to: 5.1.1
* WC requires at least: 3.6.0
* WC tested up to: 3.6.3
*
*
* Copyright: © 2019 Kathy Darling.
* License: GNU General Public License v3.0
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*
*/
/**
* Support for bundled variable item NYP.
*
* @return void
*/
function kia_pb_variable_nyp_support_init(){
add_action( 'woocommerce_bundled_single_variation', 'kia_variable_nyp_price_input_support', 12, 2 );
add_filter( 'woocommerce_bundle_price_data', 'kia_bundle_price_data_has_nyp', 10, 2 );
}
add_action( 'wc_name_your_price_loaded', 'kia_pb_variable_nyp_support_init' );
/**
* Support for bundled variable item NYP.
*
* @param int $product_id
* @param WC_Bundled_Item $item
* @return void
*/
function kia_variable_nyp_price_input_support( $product_id, $item ) {
WC_PB_Compatibility::$nyp_prefix = $item->get_id();
WC_Name_Your_Price()->display->display_price_input( $product_id, WC_PB_NYP_Compatibility::nyp_cart_prefix( false, $product_id ) );
WC_PB_Compatibility::$nyp_prefix = '';
}
/**
* Modify bundle price data.
*
* @param array $bundle_price_data
* @param WC_Product_Bundle $bundle
* @return array
*/
function kia_bundle_price_data_has_nyp( $bundle_price_data, $bundle ) {
$bundled_items = $bundle->get_bundled_items();
if ( ! empty( $bundled_items ) ) {
foreach ( $bundled_items as $bundled_item ) {
// If NYP is NO, double check that it might not be a variable NYP.
if( isset( $bundle_price_data[ 'is_nyp' ][ $bundled_item->get_id() ] ) && 'no' == $bundle_price_data[ 'is_nyp' ][ $bundled_item->get_id() ] ) {
$bundle_price_data[ 'is_nyp' ][ $bundled_item->get_id() ] = WC_Name_Your_Price_Helpers::has_nyp( $bundled_item->get_product() ) ? 'yes' : 'no';
}
}
}
return $bundle_price_data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment