在Admin Woocommerce产品列表页面中显示的自定义价格计算

时间:2018-09-17 05:43:09

标签: php wordpress woocommerce product price

这是尝试为“购买价格”创建自定义字段,目标是计算产品价格与购买价格之间的差,并以此方式提供“利润”,在这种情况下称为“净额” “。

这给了我一个错误:“警告:遇到了一个非数字值”,它指的是第115行:

$netwin = $product_standard_cost - $product_purchase_price;

代码如下:

if (in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))) {

    if (is_admin()) {
        $url = plugin_dir_url(__FILE__);
        add_action('woocommerce_product_options_general_product_data', 'wppp_add_product_purchase_price_field');
        add_action('woocommerce_process_product_meta', 'wppp_save_product_purchase_price_field', 10, 2);
        add_action('woocommerce_product_after_variable_attributes', 'wppp_add_variable_product_purchase_price_field', 10, 3);
        add_action('woocommerce_save_product_variation', 'wppp_save_variable_product_purchase_price_field', 10, 2);
        add_action('plugins_loaded', 'wppp_load_product_purchase_price_textdomain');
    }
}


function wppp_load_product_purchase_price_textdomain() {
    load_plugin_textdomain('products-purchase-price-for-woocommerce', false, plugin_basename(dirname(__FILE__)) . '/languages/');
}

function wppp_add_product_purchase_price_field() {
    $currency = get_woocommerce_currency_symbol();
    woocommerce_wp_text_input(
          array(
              'id' => '_purchase_price',
              'class' => '',
              'wrapper_class' => 'pricing show_if_simple show_if_external',
              'label' => __("Purchase Price", 'products-purchase-price-for-woocommerce') . " ($currency)",
              'data_type' => 'price',
              'desc_tip' => true,
              'description' => __('Submit the total product purchase cost.', 'products-purchase-price-for-woocommerce'),
          )
    );
}

function wppp_save_product_purchase_price_field($post_id, $post) {
    if (isset($_POST['_purchase_price'])) {
        $purchase_price = ($_POST['_purchase_price'] === '' ) ? '' : wc_format_decimal($_POST['_purchase_price']);
        update_post_meta($post_id, '_purchase_price', $purchase_price);
    }
}


function wppp_add_variable_product_purchase_price_field($loop, $variation_data, $variation) {
    $currency = get_woocommerce_currency_symbol();
    woocommerce_wp_text_input(array(
        'id' => 'variable_purchase_price[' . $loop . ']',
        'wrapper_class' => 'form-row form-row-first',
        'label' => __("Purchase Price", 'products-purchase-price-for-woocommerce') . " ($currency)",
        'placeholder' => 'Submit the product purchase price.',
        'data_type' => 'price',
        'desc_tip' => false,
        'value' => get_post_meta($variation->ID, '_purchase_price', true)
    ));
}

function wppp_save_variable_product_purchase_price_field($variation_id, $i) {
    if (isset($_POST['variable_purchase_price'][$i])) {
        $purchase_price = ($_POST['variable_purchase_price'][$i] === '' ) ? '' : wc_format_decimal($_POST['variable_purchase_price'][$i]);
        update_post_meta($variation_id, '_purchase_price', $purchase_price);
    }
}


add_filter( 'manage_edit-product_columns', 'wppp_add_purchase_price_product_column', 11);
function wppp_add_purchase_price_product_column( $columns )
{
    $columns['_purchase_price'] = __( 'Buy-in Cost','woocommerce');
    return $columns;
}

add_action( 'manage_product_posts_custom_column' , 'wppp_display_purchase_price_in_column', 10, 2 );
function wppp_display_purchase_price_in_column( $column, $product_id )
{
    global $post;
    $purchase_price = get_post_meta( $product_id, '_purchase_price', true );
    switch ( $column )
    {
        case '_purchase_price' :
            echo get_woocommerce_currency_symbol() . $purchase_price;
            break;
    }
}

add_filter( 'manage_edit-product_columns', 'wppp_add_net_winnings_product_column', 11);
function wppp_add_net_winnings_product_column($columns)
{
   $columns['net_winnings'] = __( 'Net','woocommerce');
   return $columns;
}



add_action( 'manage_product_posts_custom_column' , 'wppp_net_winnings_display', 10, 2 );
function wppp_net_winnings_display( $column, $product_id )
{
    global $product;
    $product_purchase_price = get_post_meta( $product_id, '_purchase_price', true );
    $product_standard_cost = $product->get_regular_price();
    $netwin = $product_standard_cost - $product_purchase_price;
    switch ( $column )
    {
        case 'net_winnings' :
            echo get_woocommerce_currency_symbol() . $netwin;
            break;
    }
}


add_filter( "manage_edit-product_sortable_columns", 'wppp_make_purchase_price_column_sortable' );
function wppp_make_purchase_price_column_sortable( $columns )
{
    $custom = array(
        '_purchase_price'    => 'Purchase Price',
        'net_winnings'    => 'Net',
    );
    return wp_parse_args( $custom, $columns );
}

我们非常感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您需要用以下内容替换wppp_net_winnings_display()函数:

add_action( 'manage_product_posts_custom_column' , 'wppp_net_winnings_display', 10, 2 );
function wppp_net_winnings_display( $column, $product_id ){
    if( $column  == 'net_winnings' ) {
        global $product;

        if( ! $product && ! is_object($product) ){
            $product = wc_get_product( $product_id );
        }

        $purchase_price = (float) $product->get_meta('_purchase_price' );
        $regular_price  = (float) $product->get_regular_price();

        $net_winning    = $purchase_price != 0 ? $regular_price - $purchase_price : 0;

        echo wc_price($net_winning);
    }
}

代码进入您的活动子主题(或活动主题)的function.php文件中。经过测试,可以正常工作。