Trying to build simple deposit code that hooks into woocommerce

Question

I’ve been attempting a to piece together a little code for a client website to include a simple automated deposit function. I’m super basic with PHP, im going into this with VB knowledge from like 2009 so cut me some slack. Trying to use this project to gain a little knowledge.

The Goal

Create a checkbox on the product page, if checked that product will also display a 50% deposit amount under the product price, having an item in the cart with deposit condition set will default the current order payment to 50% of the total. After an order with a deposit, create a new order with “Deposit Remainder”.

The Problem
It half works. The prices are updated across the site but I can’t get the checkbox to work. I have not yet tried to implement the new order with remainder.

I tried an if statement for the checkbox:

function custom_fields_save_seo( $post_id ){
$woocommerce_checkbox = ( $_POST['deposit_checkbox'] );
 if (isset ($woocommerce_checkbox))
     update_post_meta( $post_id, '_deposit_checkbox', true ) ;
}

Here is what I have so far, it successfully changes the product prices throughout the website.

function add_custom_fields_seo() {
     global $woocommerce, $post;
       // woocommerce_wp_text_input(
       //  array(
         //       'id'      => '_deposit_price',
          //      'label'   => __( 'Deposit price($)', 'woocommerce' ),
          //      'placeholder' => '15',
           //     'desc_tip' => 'true',
            //    'description' => __( 'Enter the custom deposit price here.', 'woocommerce' )
        // )

   //  );
        woocommerce_wp_checkbox( 
        array(
        'id'          => 'deposit_checkbox',
        'label'         => __('Agreegation Policy.'),
        'required'  => false,
    ));


  }
add_action( 'woocommerce_product_options_general_product_data', 'add_custom_fields_seo');

function custom_fields_save_seo( $post_id ){
$woocommerce_text_field = sanitize_text_field( $_POST['_deposit_price'] );
if( is_numeric( $woocommerce_text_field ) || empty( $woocommerce_text_field ) ){
     update_post_meta( $post_id, '_deposit_price', esc_attr( $woocommerce_text_field ) );
}
}
add_action( 'woocommerce_process_product_meta', 'custom_fields_save_seo' );

function filter_woocommerce_get_price( $price, $product ){
$product_id = $product->get_id();
$deposit_price = get_post_meta( $product_id,'_phive_book_price' / 2, true );
if( ! empty( $deposit_price )  ) { 
    return $deposit_price;
}
return 0;
}
add_filter( 'woocommerce_get_price', 'filter_woocommerce_get_price', 10, 2 );
function filter_woocommerce_get_price_html( $price, $product ){
$product_id = $product->get_id();
$product_price = get_post_meta( $product_id, '_phive_book_price', true );
$deposit_price = $product_price / 2;
if( ! empty( $deposit_price )  ) {
    return wc_price($product_price) . ' <i>Deposit: ' . wc_price($deposit_price) . '</i>';
}
return wc_price( $product_price );
}
add_filter( 'woocommerce_get_price_html', 'filter_woocommerce_get_price_html', 10, 2 );
add_filter( 'woocommerce_cart_product_price', 'filter_woocommerce_get_price_html', 10, 2 );


function filter_woocommerce_cart_product_subtotal( $product_subtotal, $product, $quantity ){
$product_id = $product->get_id();
$product_price = get_post_meta( $product_id, '_phive_book_price', true );
$deposit_price = $product_price / 2;
if( ! empty( $deposit_price )  ) {
     return wc_price( $product_price * $quantity ) . ' <i>Deposit: ' . wc_price( $deposit_price * $quantity ) . '</i>';
}
return wc_price( $product_price * $quantity );
}
add_filter( 'woocommerce_cart_product_subtotal', 'filter_woocommerce_cart_product_subtotal', 10, 3 );

Any help is appreciated!

--------------------------------------------------

>>> Share a link to this question, just copy and paste the code bellow on your web page <<<

<a href="https://www.wptricks.com/question/trying-to-build-simple-deposit-code-that-hooks-into-woocommerce/">

Trying to build simple deposit code that hooks into woocommerce</a>
0
, , , Thescriptkiddy25 2 weeks 0 Answers 44 views 0

Leave an answer