Skip to content

Instantly share code, notes, and snippets.

@RiodeJaneiroo
Last active April 14, 2019 11:28
Show Gist options
  • Save RiodeJaneiroo/5dabccf9b459578e06b8a7bd9c267201 to your computer and use it in GitHub Desktop.
Save RiodeJaneiroo/5dabccf9b459578e06b8a7bd9c267201 to your computer and use it in GitHub Desktop.
[Wordpress – woocommerce] Woocommerce functions #wordpress #woocommerce
<?php
/**
* Checkout billing information form
*
* This template can be overridden by copying it to yourtheme/woocommerce/checkout/form-billing.php. <--- HERE PATH
*
* HOWEVER, on occasion WooCommerce will need to update template files and you
* (the theme developer) will need to copy the new files to your theme to
* maintain compatibility. We try to do this as little as possible, but it does
* happen. When this occurs the version of the template file will be bumped and
* the readme will list any important changes.
*
* @see https://docs.woocommerce.com/document/template-structure/
* @author WooThemes
* @package WooCommerce/Templates
* @version 3.0.9
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/** @global WC_Checkout $checkout */
?>
<div class="woocommerce-billing-fields">
<?php //do_action( 'woocommerce_before_checkout_billing_form', $checkout ); ?>
<div class="woocommerce-billing-fields__field-wrapper">
<?php
$fields = $checkout->get_checkout_fields( 'billing' );
foreach ( $fields as $key => $field ) {
if ( isset( $field['country_field'], $fields[ $field['country_field'] ] ) ) {
$field['country'] = $checkout->get_value( $field['country_field'] );
}
woocommerce_form_field( $key, $field, $checkout->get_value( $key ) );
}
?>
</div>
<?php //do_action( 'woocommerce_after_checkout_billing_form', $checkout ); ?>
</div>
<?php
/**
* Checkout Form
*
* This template can be overridden by copying it to yourtheme/woocommerce/checkout/form-checkout.php. <--- HERE PATH
*
* HOWEVER, on occasion WooCommerce will need to update template files and you
* (the theme developer) will need to copy the new files to your theme to
* maintain compatibility. We try to do this as little as possible, but it does
* happen. When this occurs the version of the template file will be bumped and
* the readme will list any important changes.
*
* @see https://docs.woocommerce.com/document/template-structure/
* @author WooThemes
* @package WooCommerce/Templates
* @version 2.3.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// If checkout registration is disabled and not logged in, the user cannot checkout
if ( ! $checkout->is_registration_enabled() && $checkout->is_registration_required() && ! is_user_logged_in() ) {
echo apply_filters( 'woocommerce_checkout_must_be_logged_in_message', __( 'You must be logged in to checkout.', 'woocommerce' ) );
return;
}
?>
<form name="checkout" method="post" class="checkout woocommerce-checkout" action="<?php echo esc_url( wc_get_checkout_url() ); ?>" enctype="multipart/form-data">
<?php if ( $checkout->get_checkout_fields() ) : ?>
<div class="col2-set" id="customer_details">
<div class="col-1">
<?php do_action( 'woocommerce_checkout_billing' ); ?>
</div>
</div>
<?php endif; ?>
<?php do_action( 'woocommerce_checkout_before_order_review' ); ?>
<div id="order_review" class="woocommerce-checkout-review-order">
<?php do_action( 'woocommerce_checkout_order_review' ); ?>
</div>
</form>
<?php do_action( 'woocommerce_before_checkout_form', $checkout );?>
<?php do_action( 'woocommerce_after_checkout_form', $checkout ); ?>
// Disable the default stylesheet
add_filter( 'woocommerce_enqueue_styles', '__return_false' );
// Customizing checkout fields using actions and filters
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
unset($fields['order']['order_comments']);
unset($fields['billing']['billing_last_name']);
unset($fields['billing']['billing_company']);
unset($fields['billing']['billing_country']);
unset($fields['billing']['billing_address_1']);
unset($fields['billing']['billing_address_2']);
unset($fields['billing']['billing_city']);
unset($fields['billing']['billing_postcode']);
unset($fields['billing']['billing_state']);
$fields['billing']['billing_first_name']['label'] = "Ваше имя";
$fields['billing']['billing_first_name']['placeholder'] = "Имя";
$fields['billing']['billing_phone']['label'] = "Ведите ваш номер телефона для подтверждения платежа";
$fields['billing']['billing_phone']['placeholder'] = "Телефон";
$fields['billing']['billing_email']['label'] = "Ведите ваш e-mail на который хотите получать прогнозы";
$fields['billing']['billing_email']['placeholder'] = "E-mail";
return $fields;
}
// Outputs a checkout/address form field.
add_filter( 'woocommerce_form_field', 'filter_function_name_1525', 10, 4 );
function filter_function_name_1525( $field, $key, $args, $value ){
$field = '<div class="input__el"><label>'.$args['label'].': </label>
<input type="'.$args['type'].'" class="input-hidden" name="' . $key . '" id="' . $key . '" placeholder="' . $args['placeholder'] . '" value="'. $value.'" />
</div>';
return $field;
}
add_filter( 'woocommerce_order_button_html', 'custom_order_button_html');
function custom_order_button_html( $button ) {
// The text of the button
$order_button_text = "Купить";
// HERE your Javascript Event
$js_event = "fbq('track', 'AddPaymentInfo');";
// HERE you make changes (Replacing the code of the button):
$button = '<input type="submit" onClick="'.$js_event.'" class="button alt" name="woocommerce_checkout_place_order" id="place_order" value="' . esc_attr( $order_button_text ) . '" data-value="' . esc_attr( $order_button_text ) . '" />';
return $button;
}
add_action( 'init', 'woocommerce_clear_cart_url' );
function woocommerce_clear_cart_url() {
if ( isset( $_GET['clear-cart'] ) ) {
global $woocommerce;
$woocommerce->cart->empty_cart();
}
}
/// Functions for remove products from cart
add_action( 'wp_ajax_removeCart', 'removeCart_function' ); // wp_ajax_{ЗНАЧЕНИЕ ПАРАМЕТРА ACTION!!}
add_action( 'wp_ajax_nopriv_removeCart', 'removeCart_function' ); // wp_ajax_nopriv_{ЗНАЧЕНИЕ ACTION!!}
// первый хук для авторизованных, второй для не авторизованных пользователей
function removeCart_function(){
$productId = $_POST['productId'];
global $woocommerce;
$woocommerce->cart->empty_cart();
$woocommerce->cart->add_to_cart($productId);
echo do_shortcode('[woocommerce_checkout]');
die; // даём понять, что обработчик закончил выполнение
}
function implement_ajax_c() {
global $woocommerce;
$woocommerce->cart->remove_coupons();
$ret = $woocommerce->cart->add_discount( $_POST['coupon_code'] );
echo $ret;
die;
}
add_action('wp_ajax_implement_ajax_c', 'implement_ajax_c');
add_action('wp_ajax_nopriv_implement_ajax_c', 'implement_ajax_c');
// woocommerce
$('.product_item .btn').click(function () {
var productId = $(this).attr('data-productId');
var productName = $(this).attr('data-productName');
var productPrice = $(this).attr('data-productPrice');
console.log(productId, productName, productPrice);
$.ajax({
url: ajaxurl,
type: 'POST',
data: 'action=removeCart&productId=' + productId, // можно также передать в виде массива или объекта
success: function( data ) {
console.log('good woocomerrce request');
$('#order-popup .modal_content').html(data);
$('#order-popup .modal_desc').text('Вы покупаете пакет '+productName+' за ' + productPrice +' руб.');
}
});
});
<?php
/**
* Checkout Payment Section
*
* This template can be overridden by copying it to yourtheme/woocommerce/checkout/payment.php. <--- HERE PATH
*
* HOWEVER, on occasion WooCommerce will need to update template files and you
* (the theme developer) will need to copy the new files to your theme to
* maintain compatibility. We try to do this as little as possible, but it does
* happen. When this occurs the version of the template file will be bumped and
* the readme will list any important changes.
*
* @see https://docs.woocommerce.com/document/template-structure/
* @package WooCommerce/Templates
* @version 3.4.0
*/
defined( 'ABSPATH' ) || exit;
if ( ! is_ajax() ) {
do_action( 'woocommerce_review_order_before_payment' );
}
?>
<div id="payment" class="woocommerce-checkout-payment submit_">
<?php if ( WC()->cart->needs_payment() ) : ?>
<ul class="wc_payment_methods payment_methods methods" style="display: none;">
<?php
if ( ! empty( $available_gateways ) ) {
foreach ( $available_gateways as $gateway ) {
wc_get_template( 'checkout/payment-method.php', array( 'gateway' => $gateway ) );
}
} else {
echo '<li class="woocommerce-notice woocommerce-notice--info woocommerce-info">' . apply_filters( 'woocommerce_no_available_payment_methods_message', WC()->customer->get_billing_country() ? esc_html__( 'Sorry, it seems that there are no available payment methods for your state. Please contact us if you require assistance or wish to make alternate arrangements.', 'woocommerce' ) : esc_html__( 'Please fill in your details above to see available payment methods.', 'woocommerce' ) ) . '</li>'; // @codingStandardsIgnoreLine
}
?>
</ul>
<?php endif; ?>
<div class="form-row place-order">
<noscript>
<?php esc_html_e( 'Since your browser does not support JavaScript, or it is disabled, please ensure you click the <em>Update Totals</em> button before placing your order. You may be charged more than the amount stated above if you fail to do so.', 'woocommerce' ); ?>
<br/><button type="submit" class="button alt" name="woocommerce_checkout_update_totals" value="<?php esc_attr_e( 'Update totals', 'woocommerce' ); ?>"><?php esc_html_e( 'Update totals', 'woocommerce' ); ?></button>
</noscript>
<?php //wc_get_template( 'checkout/terms.php' ); ?>
<?php do_action( 'woocommerce_review_order_before_submit' ); ?>
<?php echo apply_filters( 'woocommerce_order_button_html', '<button type="submit" class="button alt" name="woocommerce_checkout_place_order" id="place_order" value="' . esc_attr( $order_button_text ) . '" data-value="' . esc_attr( $order_button_text ) . '">' . esc_html( $order_button_text ) . '12312</button>' ); // @codingStandardsIgnoreLine ?>
<?php do_action( 'woocommerce_review_order_after_submit' ); ?>
<?php wp_nonce_field( 'woocommerce-process_checkout', 'woocommerce-process-checkout-nonce' ); ?>
</div>
</div>
<?php
if ( ! is_ajax() ) {
do_action( 'woocommerce_review_order_after_payment' );
}
<?php
/**
* Review order table
*
* This template can be overridden by copying it to yourtheme/woocommerce/checkout/review-order.php. <--- HERE PATH
*
* HOWEVER, on occasion WooCommerce will need to update template files and you
* (the theme developer) will need to copy the new files to your theme to
* maintain compatibility. We try to do this as little as possible, but it does
* happen. When this occurs the version of the template file will be bumped and
* the readme will list any important changes.
*
* @see https://docs.woocommerce.com/document/template-structure/
* @author WooThemes
* @package WooCommerce/Templates
* @version 3.3.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
?>
<div class="shop_table woocommerce-checkout-review-order-table">
<?php foreach ( WC()->cart->get_coupons() as $code => $coupon ) : ?>
<div class="cart-discount coupon-<?php echo esc_attr( sanitize_title( $code ) ); ?>">
<div><?php wc_cart_totals_coupon_label( $coupon ); ?></div>
<div><?php wc_cart_totals_coupon_html( $coupon ); ?></div>
</div>
<?php endforeach; ?>
<?php do_action( 'woocommerce_review_order_before_order_total' ); ?>
<div class="order-total total__summ">
<span>Сумма к оплате:</span>
<b><?php wc_cart_totals_order_total_html(); ?></b>
</div>
<?php do_action( 'woocommerce_review_order_after_order_total' ); ?>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment