Skip to content

Instantly share code, notes, and snippets.

@tradesouthwest
Last active June 30, 2020 01:51
Show Gist options
  • Save tradesouthwest/cec68670a4cf0eecb72a182a25dba82e to your computer and use it in GitHub Desktop.
Save tradesouthwest/cec68670a4cf0eecb72a182a25dba82e to your computer and use it in GitHub Desktop.
yith request a quote view to get attribute values into quote table aka woocomerce cart
<?php
/**
* Get product attributes to display on yith-quote table
* @param string $attribute_name Taxonomy name
* @param string $product_id Global value
* @return array Attribute value
*/
function betheme_child_get_product_terms_carton( $attribute_name, $product_id )
{
$object_id = wc_attribute_taxonomy_id_by_name($attribute_name);
$attribute_key = wc_attribute_taxonomy_name($attribute_name);
$product_terms = wc_get_product_terms($product_id, $attribute_key, array('fields' => 'names'));
// check if not empty, then display
if (!empty($product_terms)) {
$attribute = array_shift($product_terms);
return $attribute;
} else {
// no attribute under this name
return 'na';
}
}
// Validate (optional)
function betheme_child_valid_attribute_name( $attribute_name ) {
if ( strlen( $attribute_name ) >= 28 ) {
return new WP_Error( 'error', sprintf( __( 'Slug "%s" is too long (28 characters max). Shorten it, please.', 'woocommerce' ), sanitize_title( $attribute_name ) ) );
} elseif ( wc_check_if_attribute_name_is_reserved( $attribute_name ) ) {
return new WP_Error( 'error', sprintf( __( 'Slug "%s" is not allowed because it is a reserved term. Change it, please.', 'woocommerce' ), sanitize_title( $attribute_name ) ) );
}
return true;
}
/**
* Get product attributes to display on yith-quote table
* ********************************************************************************
* (These lines go in the "request-quote-view.php" template in a woocommerce folder
* after td class="product-name" first 'endif'. Approx. line 208.)
* ********************************************************************************
*/
$first_carton_cnt_label = wc_attribute_label('pa_carton-count');
$first_cartons_pallet_label = wc_attribute_label('pa_cartons-per-pallet');
$first_carton_value = betheme_child_get_product_terms_carton( 'carton-count', $product_id );
$first_pallet_value = betheme_child_get_product_terms_carton( 'cartons-per-pallet', $product_id );
if ( $first_carton_value > 0 ) {
echo '<p>' . esc_html($first_carton_cnt_label) . ': </span><span>' . absint($first_carton_value) . '</span>
<span class="delim"> | </span>
<span>' . esc_html($first_cartons_pallet_label) . ': </span><span>' . esc_html($first_pallet_value) . '</span></p>';
echo '
<input type="hidden" name="carton_cnt_label_'. esc_attr($product_id) .'"
value="' . intval($first_carton_cnt_label) . '" id="first_carton_cnt_label_'. esc_attr($product_id) .'">
<input type="hidden" name="cartons_pallet_label_'. esc_attr($product_id) .'"
value="' . intval($first_pallet_label) . '" id="first_cartons_pallet_label_'. esc_attr($product_id) .'">
<input type="hidden" name="carton_cnt_value_'. esc_attr($product_id) .'"
value="' . intval($first_carton_val) . '" id="first_carton_cnt_value_'. esc_attr($product_id) .'">
<input type="hidden" name="cartons_pallet_value_'. esc_attr($product_id) .'"
value="' . intval($first_pallet_val) . '" id="first_cartons_pallet_value_'. esc_attr($product_id) .'">';
}else{
print("");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment