Skip to content

Instantly share code, notes, and snippets.

@willybahuaud
Created January 7, 2022 00:43
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 willybahuaud/f8e29262f88fb912b61e43023972f9e2 to your computer and use it in GitHub Desktop.
Save willybahuaud/f8e29262f88fb912b61e43023972f9e2 to your computer and use it in GitHub Desktop.
Yith Request a quote : add a button to transfer the cart content into the quote request form
<?php
add_action( 'admin_post_cart_to_quote', 'do_cart_to_quote' );
add_action( 'admin_post_nopriv_cart_to_quote', 'do_cart_to_quote' );
function w_cart_to_quote() {
$inst = YITH_Request_Quote::get_instance();
foreach( WC()->cart->get_cart() as $k => $data ) {
$product_id = $data['product_id'];
$obj = array(
'context' => 'frontend',
'action' => 'yith_ywraq_action',
'ywraq_action' => 'add_item',
'quantity' => intval( $data['quantity'] ),
'wp_nonce' => wp_create_nonce( 'add-request-quote-' . $product_id ),
'yith-add-to-cart' => $product_id,
'add-to-cart' => $product_id,
'product_id' => $product_id,
'yith_wapo_product_img' => $data['yith_wapo_product_img'],
'yith_wapo_is_single' => '1',
'yith_wapo_options' => ! empty( $data['yith_wapo_options'] ) ? $data['yith_wapo_options'] : array(),
'yith_wapo' => ! empty( $data['yith_wapo_options'] ) ? $data['yith_wapo_options'] : array(),
);
if ( $v_id = $data['variation_id'] ) {
$obj['variation_id'] = $v_id;
if ( ! empty( $data['variation'] ) ) {
foreach ( $data['variation'] as $k => $v ) {
$obj[ $k ] = $v;
}
}
}
$inst->add_item( $obj );
}
wp_redirect( YITH_Request_Quote()->get_redirect_page_url() );
die();
}
/**
* Add a button on the cart page
*/
add_action( 'woocommerce_proceed_to_checkout', 'w_cart_to_quote_button', 40 );
function w_cart_to_quote_button() {
echo '<a href="' . admin_url( 'admin-post.php?action=cart_to_quote' ) . '" class="checkout-button button alt wc-forward">Demander un devis pour ce panier</a>';
}
/**
* Add a button on thhe mini-cart
*/
add_action( 'woocommerce_widget_shopping_cart_buttons', 'w_quote_link', 40 );
function w_quote_link() {
echo '<a href="' . admin_url( 'admin-post.php?action=cart_to_quote' ) . '" class="button wc-forward">Demande de devis</a>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment